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
}
})
});
@JakyeRU
Copy link
Author

JakyeRU commented Jan 12, 2021

es and embed messa

can you tell me Which command do i put in the channel to make it run the command?!?

If you haven't changed anything it should send an embed on every message from GuildMembers.

@abbabaa
Copy link

abbabaa commented Feb 26, 2021

whats the prefix

@JakyeRU
Copy link
Author

JakyeRU commented Feb 26, 2021

whats the prefix

There is no prefix. The code will run on each message. You should change that.

@StoneMcYT
Copy link

How do i even set this up

@StoneMcYT
Copy link

I get this error when running the code
(node:168076) UnhandledPromiseRejectionWarning: ReferenceError: discord is not defined

@JakyeRU
Copy link
Author

JakyeRU commented Mar 14, 2021

I get this error when running the code
(node:168076) UnhandledPromiseRejectionWarning: ReferenceError: discord is not defined

As the error suggests, discord is not defined within your code. You can fix that by defining it.

const discord = require("discord.js");

@SbleitZ
Copy link

SbleitZ commented Mar 17, 2021

How do I get others to also assign a role, and not just me :(

@gamingtube9880
Copy link

what do i need to type for this code to work

@Dakurod0
Copy link

Thanks a lot bro It works fine!

@StianWiu
Copy link

StianWiu commented May 7, 2021

Might want to capitalize discord in const Embed = new discord.MessageEmbed() it didn't work without it for me.

@JakyeRU
Copy link
Author

JakyeRU commented May 7, 2021

Might want to capitalize discord in const Embed = new discord.MessageEmbed() it didn't work without it for me.

It depends on how you required discord.js in your file.

@StianWiu
Copy link

StianWiu commented May 7, 2021

Might want to capitalize discord in const Embed = new discord.MessageEmbed() it didn't work without it for me.

It depends on how you required discord.js in your file.

You're right my bad. Nice command though anyways.

@JakyeRU
Copy link
Author

JakyeRU commented May 7, 2021

Might want to capitalize discord in const Embed = new discord.MessageEmbed() it didn't work without it for me.

It depends on how you required discord.js in your file.

You're right my bad. Nice command though anyways.

No worries! Thanks for the support.

@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 :/

@JakyeRU
Copy link
Author

JakyeRU commented Jul 31, 2021

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.

@phil-not-funny
Copy link

Hey, I was wondering what the max uses are for it. I know it says 1 but will it also work if you set it to (idunno sth like) 5?

@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