Role reactions Discord.js v11 - discord.js

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.

Related

How to check multiple roles discord.js v12

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

Discord.JS - Find if a user has a role

I'm doing the most basic mute command ever, and I need to find if the user has a role called 'Muted'
I've added the code below, but I keep getting an error.
if (command === "mute") {
const { member, mentions } = message
const mutee = message.mentions.users.first();
const muter = message.author
console.log(mutee)
if(member.hasPermission('ADMINISTRATOR')) {
let muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (message.guild.members.get(mutee.id).roles.cache.has(muteRole.id)) {
message.channel.send(new MessageEmbed() .setTitle(`Uh Oh!`) .setDescription(`${mutee.user} already has been Muted!`))
}
else {
}
}
}
First, I recommend you to get the mentioned member directly instead of the user with const mutee = message.mentions.members.first();. (If you are using the command under a Guild Channel)
Then, you can simply check if he has the role with:
if (mutee.roles.cache.has(muteRole.id))

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
}```

How to get reaction collector on embed sent by the bot

I was using something like this but i want it to look for reactions in the embed sent! not in the message
const collector = message.createReactionCollector((reaction, user) =>
user.id === message.author.id &&
reaction.emoji.name === "◀" ||
reaction.emoji.name === "▶" ||
reaction.emoji.name === "❌"
).once("collect", reaction => {
const chosen = reaction.emoji.name;
if(chosen === "◀"){
// Prev page
}else if(chosen === "▶"){
// Next page
}else{
// Stop navigating pages
}
collector.stop();
});
A RichEmbed is sent as part of a message. Therefore, when you add a reaction, it's on the message, not the embed.
See the example below which gives the appearance of reactions on an embed.
const { RichEmbed } = require('discord.js');
var embed = new RichEmbed()
.setTitle('**Test**')
.setDescription('React with the emojis.');
message.channel.send(embed)
.then(async msg => {
// Establish reaction collector
for (emoji of ['◀', '▶', '❌']) await msg.react(emoji);
})
.catch(console.error);

How to fix overwritePermissions to user not working

I want the bot to change member permissions and set "send messages" to false if someone uses $done #user.
client.on('message', async message => {
if (message.author.bot) return;
if (message.content === prefix + "done") {
var user = message.guild.member(message.mentions.users.first());
message.channel.overwritePermissions(user, {
SEND_MESSAGES: false
});
}
});
You should either use:
const user = message.mentions.users.first();
or
const user = message.mentions.members.first();
and then add a
if(!user) return messsage.channel.send('You need to mention a user!'); below the `const user` line.

Resources