Discord.js check for member getting a role - discord.js

I am currently trying to find a way to execute some code whenever a user is granted a specific role
However sadly I havent found any good resources on it
I imagine there could be a way of using the AUDIT log ,however Id like to avoid using the audit log due to security concerns

client.on('guildMemberUpdate', async(before, after) => {
const role = before.guild.roles.cache.get('ROLE_ID');
if(!before.roles.has(role) && after.roles.has(role)){
//code
}
})
Pretty simple, guildMemberUpdate event is fired on a guild member update, ie a role update.
#guildMemberUpdate

Related

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

Discord.js How to add roles when a user joins if the bot is in multiple servers

My bot is currently part of multiple server and Im trying to get it to add a role to a user when they join one of the servers, I have done stuff like
if(bot.guild.id === [SERVER ID]) and I've tried different forms of the bot.on command to no avail.
the current code is
bot.on('guildMemberAdd', guildMember => {id
let welcomerole = guildMember.guild.roles.find(role => role.name === 'Nomad');
guildMember.roles.add(welcomerole);
guildMember.guild.channels.get([SERVER_ID]).send("Welcome to the server")
})
But as per usual it does not work.
I'm not sure if there is a function im missing in the bot.on section of the block, or if the issue is something else. Note I am running Discord.js 11.6, so these functions do or should work with the version. (there is a reason).
Is there is a way to have it so the bot only adds the role to one of the servers that its a part of.
Just update to Discord.js V13, v11 is many years out of date and new docs solutions won't work with it.

Checking if message author has specific permissions in Discord.js v16?

I’m making a command that should only be ran by people that can kick or ban, assuming that the role they have is some sort of mod or admin. I’ve only seen answers for previous versions, like .hasPermissions.
what do i do?
As said in this question (self-answered by me), .hasPermission() has been removed. Use .permissions.has() instead
if (message.member.permissions.has("KICK_MEMBERS")) {
// member HAS kick members permissions
}
You can also check multiple permissions at the same time. This is just an example that would probably not be used
if (message.member.permissions.has(["MANAGE_GUILD", "SEND_MESSAGES"])) {
// member HAS *BOTH* permissions to send messages and manage server
}
You can check all the permission flags at the discord.js docs for Permissions.FLAGS
It's really simple
if (message.member.permissions.has('KICK_MEMBERS') || message.member.permissions.has('BAN_MEMBERS')) {
//your code here
}

How to detect when user receives a role?

I just began trying to learn how to write my first Discord bot this morning so I am very inexperienced with discord.js, but I am familiar with JavaScript. However I have been searching for a couple of hours trying to find a way to call a function whenever a user in my server receives or loses a role.
In my server I have added the Patreon bot which assigns a role to users who become patrons. And I would like to create a custom bot that posts "hooray username" in my general channel when a user receive the patron role.
I can not find any example that shows how to detect when a user gains or loses a role. Is it possible to do this simply using an event? Or would I possibly need to periodically iterate over all users and maintain a list of their current roles while checking for changes?
I apologize that my question doesn't include any code or examples but I haven't made any progress and am reaching out to the SO community for guidance.
You want to use the event guildMemberUpdate.
You can compare the oldMember state to the newMember state and see what roles have changed.
This is not the most elegent solution but will get the job done.
client.on('guildMemberUpdate', (oldMember, newMember) => {
// Roles
const oldRoles = oldMember.roles.cache,
newRoles = newMember.roles.cache;
// Has Role?
const oldHas = oldRoles.has('role-id'),
newHas = newRoles.has('role-id');
// Check if removed or added
if (oldHas && !newHas) {
// Role has been removed
} else if (!oldHas && newHas) {
// Role has been added
}
});
https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-guildMemberUpdate
This is simple with Client#guildMemberUpdate. Here’s some simple code that may help you
(This is the shortest code I could come up with)
client.on('guildMemberUpdate', async (oldMember, newMember) => {
if(oldMember.roles.cache.has('patreonRoleId')) return;
if(newMember.roles.cache.has('patreonRoleId')) {
//code here to run if member received role
//Warning: I didn’t test it as I had no time.
}
})
To see the removed role, just put the logical NOT operator (!) in front of both of the if statements like this:
if(!oldMember.roles.cache.has('patreonRoleId'))
if(!newMember.roles.cache.has('patreonRoleId'))
Note: make sure you have guildMembers intent enabled from the developers portal

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!`));

Resources