Discord bot issuing roles based on member usernames - discord

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);
});

Related

Global Ban excepts certain servers Discord.JS

so I found a code to ban a member from every server the bot is in, also known as, global ban. I like this feature, however, I want it to ignore a certain server. For example, I have a Complaint or Ban Appeal Discord, and when I ban a member, it will also ban the member from the Complaint/Ban Appeals server. Now, I don't want that. I want it to ban from all of the bot's servers, but not that exact server. Is there are a way I can ignore a certain server from banning a member?
client.guilds.cache.forEach(a => a.members.ban(member.id))
I think you can ignore a certain server doing something like this:
client.guilds.cache.forEach(a => {
if(a.id != 'YOUR_SUPPORT_SERVER_ID') {
a.members.ban(member.id)
}
})
Sobstitute the string with the guild id of the server you want to ignore.

How to remove roles from a user in every server? Discord.py

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

How do I check if anyone in a guild has a role? discord.js

I want to be able to check if anyone in the whole server has a specific role. Is this possible?
Role.members returns a collection of every cached member who has the given role. Note: you will need to update the required intents to use this correctly.
// get the role
const role = guild.roles.cache.get('role-id');
role.members.each((member) => console.log(`${member.username} has the role!`));

How to get #everyone role ID?

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.

Using guilds and channels without needing to read a message

I'm hosting my bot on glitch.com, but it restarts every night.
When my bot gets started, it reads a message from a specific channel and then writes in the others. The problem with this is that I can't use any guild or channel until the bot receives a message.
How can I avoid doing this?
You can use guild and channel IDs. Every element in Discord has a unique ID (called Snowflake) that identifies it.
If you want to get a specific guild or channel, you can save its ID and use it in the code. IDs are public, so there's no risk using them (they're not like passwords or tokens).
Guilds and channels are stored in Collections and mapped by their IDs, so you can use them like this:
let guild = client.guilds.get('guild id as a string');
let channel = guild.channels.get('channel id as a string');
To get your guild's ID (or the ID of pretty much any element in Discord) you can enable the Developer mode in the settings, then right-click on the guild and select "Copy ID".

Resources