I need to get #everyone role ID to create a private chat for to server members (I'll restrict others except for those 2 users from reading and sending messages). How?
I tried typing
\#Admin
to get Admin's role ID as an example.
But then I typed
\#everyone
and it creates a regular message mentioning everyone on the server (like typing #everyone in chat). No ID.
How do I get #everyone role ID?
The ID for the everyone role is the Guild ID.
If you try to do <#&123123> replacing 123123 with your guild id, it will almost tag everyone
Update: 10th April 2020
Now to mention everyone, all you need is to send the string #everyone: message.channel.send("#everyone");
To obtain the ID for the everyone role, you need to have the guild, which you get on some events like: message, guildCreate, and others.
From there: <something>.guild.roles.everyone, and with that you should be able to get the ID with <something>.guild.roles.everyone.id.
If you don't have access to the guild on a event, you can get the guild by it's ID with something like:
client.guilds.cache.get('guildID').roles.everyone.id, if you know that it will be in cache. If you want to be safe, you can do it like this:
client.guilds.fetch('guildID').then(guild => {
let id = guild.roles.everyone.id;
// do something with id
}
or with await:
let guild = await client.guilds.fetch('guildID');
let id = guild.roles.everyone.id;
Need to get #everyone Role?
<guild>.roles.everyone
It will get a role object. You can check that in Role Class
Discord.js
V12
The guild class has a roles property, which is a RoleManager.
This manager has the property everyone:
The #everyone role of the guild
You can get its id with the id property of the role class:
client.on('message', (msg) => {
console.log(msg.guild.roles.everyone.id);
});
V11
In V11 the guild class has a defaultRole property.
So the code is slightly different:
client.on('message', (msg) => {
console.log(msg.guild.defaultRole.id);
});
Discord
On Discord side, with dev option activated, you can right click in almost everything to grab it ID. For example go in the server settings, in role and right click on a role, you should have a copy ID option.
Related
I'm creating a bot for the purpose of Discord moderation, one of the components of this bot is the following task:
"When every new user comes to server, his role by default will be #NotMember, but when he changes his server nickname to the "Nickname | (Real name)", for example, CoolPerson (Alex), then his role is automatically changing to the #Member".
The one way of doing this that I can see is to check if the username contains the brackets, if not then his role is old #NotMember.
Is there any another way to detect if server members have changed their name to a nickname? And is this actually possible?
I'm making this bot in JavaScript, but Python is also welcome here.
You can use the guildMemberUpdate event that is triggered whenever a member changes their name, with this you will have access to the guild member and their old/new username. Note that your bot will need to have the Server Members Intent
enabled on the developer portal.
Example in Discord.js:
client.on("guildMemberUpdate", function(oldMember, newMember) {
// oldMember is the old guild member object before the change, you
// can use it to fetch their old username.
// Add a role to the newMember object.
newMember.roles.add(YOUR_ROLE_HERE);
});
When my Discord bot joins a guild, I want to send a private message to the member inviting the bot. I am using Discord.js.
Now, i know there is a guildCreate event i can use on the client, that fires when the bot joins a guild.
This event however, only provides the Guild the bot has been invited in.
I've seen multiple bots sending me private messages this way, so i know there must be a way. One could get the guild owner and send him private message, but there is a chance that the owner is not the one inviting the bot. And unfortunately, since i was always the owner when it happened, i can't say if it was this or not.
Is there a way to retrieve the GuildMember, or the User that invited the bot ?
This requires view audit log permissions for the bot:
let logs = await guild.fetchAuditLogs()
logs = logs.entries.filter(e => e.action === "BOT_ADD")
let user = logs.find(l => l.target?.id === client.user.id)?.executor
//user is the inviter
I am making a verification bot and I want to do a command where if somebody unlinks their Roblox account then it removes the verified role from them in all the servers they are in. I know how to do it in a single server like this:
role1 = discord.utils.get(ctx.guild.roles, name='Verified')
await ctx.author.remove_roles(role1)
But how would I do it across all of the servers the member is in with the bot. Thanks!!!
A role is unique per guild, you'd need to iterate through every guild, get the member and role, and remove it.
for guild in bot.guilds:
member = guild.get_member(member_id) # Change the ID accordingly
if member is not None:
role = discord.utils.get(guild.roles, name='Verified')
await member.remove_roles(role)
This code loops through every guild the bot is in, tried to get the member, if it's not a nonetype (cause the member doesn't have to share that specific guild with the bot), it gets the role obj and removes it.
Reference:
Bot.guilds
Guild.get_member
Also you need intents.guilds and intents.members
I searched something for a code that will make the bot message on the owner after it got invited and it's like this:
client.on('guildCreate', guild => {
guild.owner.send('Thanks for invting me to the server!');
}):
But suddenly, it's giving me an error that says, cannot read propert send() of null, am I using old way to do this? if so, how do I make it work?
Does this means it can't find the owner of the server?
client.on('guildCreate', guild => {
guild.owner.user.send('Thanks for invting me to the server!');
}):
If you were to console log guild.owner it would give you a json of the guild owner's id, username etc all within the user object, and to access this we need to do guild.owner.user
In my discord server, as a verification method, I want my bot to have all users react to the message and then get given the verified role, and remove the old role. The current code I have doesn't grant or remove roles, but doesn't error.
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("name", setup.verify));
users.removeRole(users.guild.roles.find("name", setup.default));
});
There is a problem in the current code. The messageReactionAdd event returns a User object, which does not contain the property .guilds which you attempt to access: users.guilds.r- .
Instead of this perhaps you should the id of the User object and plug that into message.guild.members.get("id here"). Then use the returned Member object with your current code. That should work!
References:
Guild Member
, Message Reaction Event, User Object
Would something like this work?
I'm not sure it will I haven't tested it. This should add the role if the message they reacted to consists of the word "role1" and if they react to a different message it will remove the role. I don't know if this is what you're going for but in theory this should work.
client.on("MessageReactionAdd", function(users) {
if (message.content === "role1") {
users.addRole(users.guild.roles.find("name", setup.verify))
} else if (!message.content === "role1") {
user.removeRole(users.guild.role.find("name", setup.default))
}
});
Are the names of the roles; setup.verify & setup.default?
If not, that's why it is not working.
Try:
client.on("messageReactionAdd", function(users) {
users.addRole(users.guild.roles.find("id", ROLEIDHERE));
users.removeRole(users.guild.roles.find("id", ROLEIDHERE));
});
where ROLEIDHERE put the role id to find the role id just tag the role and before you hit send put \ before the tag and it will supply the id