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
}
})
});
@plant-to-flower
Copy link

Fake

You have to change the role1 ("RoleId")

@TheMutedPlayer
Copy link

It wont work this is the code i have const Discord = require("discord.js");

module.exports = {
name: "Coolperson",
aliases: ["cool", "Cool"],
args: true, // or false
cooldown: 1, // in seconds
description: "description",
execute(message, args, client) {
// command code

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("763134135262511114");

// 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
    }
})

});
}
}

@TheMutedPlayer
Copy link

and this is my error (node:7984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'includes' of undefined
at C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\src\index.js:88:51
at Map.find (C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\node_modules@discordjs\collection\dist\index.js:161:17) at Client. (C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\src\index.js:88:25)
(Use node --trace-warnings ... to show where the warning was created)
(node:7984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:7984) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:7984) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'includes' of undefined
at C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\src\index.js:88:51
at Map.find (C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\node_modules@discordjs\collection\dist\index.js:161:17) at Client. (C:\Users\Owner\OneDrive\Desktop\Dibble bot Version s2\src\index.js:88:25)
(node:7984) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

@r-rajaneesh
Copy link

All This is fine But i wanted it to happen in a specific channel and how do i do that?

@JakyeRU
Copy link
Author

JakyeRU commented Dec 24, 2020

All This is fine But i wanted it to happen in a specific channel and how do i do that?

You can add a simple check to your code like this:

if (message.channel.id !== "12345678901234567") return false;

@Quinncoder123
Copy link

Fake

you just dont know any discord.js

@AkabaneSEN
Copy link

Debugger attached.
(node:12492) UnhandledPromiseRejectionWarning: ReferenceError: reaction is not defined
at Client. (c:\Users\lense\OneDrive\Bureau\bot-verifictation\index.js:106:5)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\ws\lib\websocket.js:825:20)
(node:12492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12492) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:12492) UnhandledPromiseRejectionWarning: ReferenceError: reaction is not defined
at Client. (c:\Users\lense\OneDrive\Bureau\bot-verifictation\index.js:106:5)
at Client.emit (events.js:327:22)
at MessageCreateAction.handle (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (c:\Users\lense\OneDrive\Bureau\bot-verifictation\node_modules\ws\lib\websocket.js:825:20)
(node:12492) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Process exited with code 1

help

@12495
Copy link

12495 commented Jan 9, 2021

es and embed messa

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

@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