How to check multiple roles discord.js v12 - discord

I'm very new/bad at discord js. But I want to make a command with "allowed roles" so only admin, owner and mod can use it. But I don't know how.
I have tried this:
client.on('message', message => {
if(message.content === 'test') {
let allowedRole = message.guild.roles.cache.find(r => r.name === "owner");
let allowedRole1 = message.guild.roles.cache.find(r => r.name === "mod");
let allowedRole2 = message.guild.roles.cache.find(r => r.name === "admin");
if (message.member.roles.cache.has(allowedRole.id) && message.member.roles.cache.has(allowedRole1.id) && message.member.roles.cache.has(allowedRole2.id)) {
message.channel.send('hello');
} else {
message.channel.send('bad');
}
}});

in your example, only users with all three roles are accepted (due to the logical AND operator).
Replace && with || and it should work as expected.
client.on('message', async message => {
if (message.content === 'test') {
let allowedRole = await message.guild.roles.cache.find(
r => r.name === 'owner'
);
let allowedRole1 = await message.guild.roles.cache.find(
r => r.name === 'mod'
);
let allowedRole2 = await message.guild.roles.cache.find(
r => r.name === 'admin'
);
if (
message.member.roles.cache.has(allowedRole.id) ||
message.member.roles.cache.has(allowedRole1.id) ||
message.member.roles.cache.has(allowedRole2.id)
) {
message.channel.send('hello');
} else {
message.channel.send('bad');
}
}
});

As Christoph said, you can use their code but I'd recommend to use role IDs intead because role name can be changed at any time:
if (!message.member.roles.cache.has(<1st_role_id>) || !message.member.roles.cache.has(<2nd_role_id>) || !message.member.roles.cache.has(<3rd_role_id>))
return message.reply("You can't execute this command!")
// your code
WARNING
It's only a solution if you are running private bot and not public, which is for a lot of servers

Related

How do i put command work for my id and my friend id eg. when i tried this it worked with my friend id only

The code:
client.on("message", message => {
if (message.author.id !== "my id") || message.author.id !== "my friend id") return message.delete();
Firstly, you should format your code, secondly, here is the code you should use:
client.on('message', msg => {
if(msg.author.id === 'id' || msg.author.id === 'id') {
//code to run
} else {
return msg.delete();
}
})
Use array:
let users = ['your id', 'your friend's id'];
if(users.includes(message.author.id)){
// your code goes here
}
else {
return msg.delete();
}

Get roles from reaction

I am trying to create a message with reactions for my bot, if a user clicks on a specific reaction he is automatically given a role chosen by me via code. I don't understand where the problem is in what I've done.
await msg.react('🤜');
if (reaction.emoji.name === '🤛') {
if (reaction.message.channel.id === "793239655331004448");
let role = reaction.guild.roles.cache.find(role => role.id === '689880327782400203');
member.roles.add(role.id);
console.log('Ruolo Aggiunto'); //role added
} else {
console.log("Errore"); //Error
}```
await msg.react('🤜');
if (reaction.emoji.name === '🤛') {
if (reaction.message.channel.id === "793239655331004448");
console.log('lol');
let role = reaction.guild.roles.cache.find(role => role.id === '689880327782400203');
console.log('lol');
member.roles.add(role);
console.log('Ruolo Aggiunto'); //role added
} else {
console.log("Errore"); //Error
}```

Cannot read property 'members' of undefined (discord.js)

I'm trying to create a DM verification system where if a member sends DM to the bot, he gets accepted.
....
const respectrumServer = client.guilds.cache.get('782347082362912768');
client.on('message', message => {
if (message.author.equals(client.user)) return;
var authorid = message.author.id;
if (message.toString().toLowerCase() === "lonely guy is the best") {
if (message.channel.type == "dm") {
if(respectrumServer.members.cache.has(authorid)){
//error is here ^
var verifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '782348009362161714');
var notverifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '797218539563122688');
member.roles.add(verifiedrole);
member.roles.remove(notverifiedrole);
authorid.send('You have been verified');
}
else{
authorid.send('You are not a member of Respectrum');
}
}
else{
message.channel.send('I know, right?');
}
}
else{
return;
}
});
I changed many things like message.author to message.author.id and members.cache.find(authorid) to members.cache.has(authorid)
I don't know what the problem is.
This is because you're trying to get the respectrumServer server before the bot is ready.
Use the following code instead to define it when you receive a new message:
client.on('message', async message => {
if (message.author.equals(client.user)) return;
const respectrumServer = client.guilds.cache.get('782347082362912768');
var authorid = message.author.id;
if (message.toString().toLowerCase() === "lonely guy is the best") {
if (message.channel.type == "dm") {
const member = await respectrumServer.members.fetch(authorid).catch(() => {});
if(member){
//error is here ^
var verifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '782348009362161714');
var notverifiedrole = member.respectrumServer.roles.cache.find(role => role.id === '797218539563122688');
member.roles.add(verifiedrole);
member.roles.remove(notverifiedrole);
authorid.send('You have been verified');
}
else{
authorid.send('You are not a member of Respectrum');
}
}
else{
message.channel.send('I know, right?');
}
}
else{
return;
}
});

Role reactions Discord.js v11

i would like to create a reaction role function to my bot (there IS a message, then if any member reacts to the message, he gets a role. Like in MEE6).
I've tried this...
client.on('messageReactionAdd', (messageReaction, user) => { if(user.bot) return; const { message, emoji } = messageReaction; if(emoji.name === "✅") { if(message.id === "713071578174193745") {
var role = message.guild.roles.find(role => role.name === "Nem hitelesített 2")
user.addRole(role)
var role = message.guild.roles.find(role => role.name === "Nem hitelesített 1")
user.removeRole(role)
}}},)
client.on("messageReactionAdd", (reaction, user) => {
console.log("1");
if (reaction.message.channel.id !== "713066051969220631" && reaction.emoji.name !== '✅') {
return;
}
client.guilds.get(yourguildid).member(user).addRole('713065834771120172')
}
but i didn't reach anything. Can you help me please?
Note: I'm on v11. By another man, it worked, he was on v11 too. The ÍTRIGGER isn't working, if i put console.log before if, and i'm reacting, no answer.

TypeError: message.guild.roles.find is not a function

Code:
client.on('message', message => {
if (message.content.startsWith(`${prefix2}red`)){
if (message.member.roles.cache.some(role => role.name === 'Red')) return message.channel.send(`You already have the role!`)
let role = message.guild.roles.find(r => r.name === "Red");(r => r.name === "Red");
let member = message.member;
message.delete(1)
member.addRole(role).catch(console.error)
}
})
I am trying to make a command that will give the user that role.
Error:
let role = message.guild.roles.find(r => r.name === "Red");
^
TypeError: message.guild.roles.find is not a function
Use message.guild.roles.cache.find.

Resources