Skip to content

Instantly share code, notes, and snippets.

@JakyeRU
Last active September 24, 2022 14:59
Show Gist options
  • Save JakyeRU/fde26f3d89d7382b2f8bf659cecb30a4 to your computer and use it in GitHub Desktop.
Save JakyeRU/fde26f3d89d7382b2f8bf659cecb30a4 to your computer and use it in GitHub Desktop.
Discord.js V12 - ReactionRole | Add a role using reactions.
/*
NOTE: This gist is no longer supported and won't get updated.
Please refer to https://github.com/JakyeRU/discord.js-examples for the latest version.
If you have any issues/suggestions you can open an issue/pull request there.
*/
client.on("message", async message => {
// Checking if the message author is a bot.
if (message.author.bot) return false;
// Getting the role by ID.
const Role1 = message.guild.roles.cache.get("RoleID");
// Creating a filter.
const Filter = (reaction, user) => user.id == message.author.id;
// Creating the embed message.
const Embed = new discord.MessageEmbed()
.setDescription(`Choose a role: ${Role1.name}`)
// Awaiting for the embed message to be sent.
const reactionMessage = await message.channel.send(Embed);
// Reacting to the embed message.
await reactionMessage.react("😎");
// Awaiting a reaction to the embed message. Time is measured in ms. (30000 ms - 30 seconds)
reactionMessage.awaitReactions(Filter, {max: 1, time: 30000, errors: ["time"]}).then(collected => {
// Getting the first reaction in the collection.
const reaction = collected.first();
// Creating a switch statement for reaction.emoji.name.
switch (reaction.emoji.name) {
case "😎":
// Checking if the member already has the role.
if (message.member.roles.cache.has(Role1.id)) {return message.channel.send("You already have the role.")};
// Adding the role.
message.member.roles.add(Role1).then(message.channel.send("Role added!"));
// Breaking the switch statement to make sure no other cases are executed.
break
}
})
});
@NzoSifou
Copy link

Hi!
When I do the command that I have defined (to send the embed etc), the embed work fine, and when I click on the reaction, nothing... My role is set with the good ID and the variable ${Role.name} work too in the embed but I didn't get the role, no error message :/

Hello.

Please open a an issue here with the full code.

I'm very sorry I just have made a mistake by editing the emoji (I forgot to edit the emoji in the "case" part), I don't have problems anymore, sorry for the disturb ^^

@Nonosh35
Copy link

Nonosh35 commented Jun 15, 2022

Heyy
all this is nice, thanks soo much !
but i don't understand how to send the embed in my discord server, can someone help me please ? ^^'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment