Realman
Realman

Reputation: 53

Discord bot issuing roles based on member usernames

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.

Upvotes: 1

Views: 474

Answers (1)

Skully
Skully

Reputation: 3116

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

Upvotes: 1

Related Questions