Message Reactions

I am making a discord bot using js. I have followed code on Discord.js guide. I am attempting to create a reaction bot that gives you a role or send you a message when you react to a certain message. Here is my code so far:

const Discord = require(‘discord.js’);
const client = new Discord.Client();

client.once(‘ready’, () => {
console.log(‘Ready!’);
});

client.on(‘message’, message => {

if (message.content === ‘!react’) {
message.react(‘:+1:’);
}

});
client.login(‘Token-Here’);

Hey @TMD,

All you need to do is add a reactionAdd event!

Add this to your code

client.on("messageReactionAdd", async (reaction, user) => {
  if (user.bot) return;
  var message = reaction.message;
});

The first thing it does it prevent bots from getting roles and declares message as reaction.message;

then you need to check what emoji is reacted so use this:

if (reaction.emoji == "🔒") { // replace the lock with any emoji

}

then you want to give the author of the message the role by using this:

message.author.addRole("role-id-goes-here", "Reaction Roles")

replace the "role-id-goes-here with the role id you want to give and Reaction Roles for the reason.

To send a message user message.author.send("message content")

Then your code should look like this:

client.on("messageReactionAdd", async (reaction, user) => {
  if (user.bot) return;
  var message = reaction.message;
    if (reaction.emoji == "🔒") {
      message.member.addRole("role-id", "reason")
message.author.send("You reacted with 🔒 and got the [role-name] role!")
   }
});

Hope this helps!

Thank you @MrDiamond64 I added this to the command code! My bot doesn’t seem to send the command to a channel in my server. Is this a problem on my end?

Oh! i thought you meant to DM the user! well you can also do that! All you have to do is use this:

client.channels.find(cn => cn.name == "channel.name").send("Message Content")

Thank you! @MrDiamond64

The command still isn’t sending in the channel. It doesn’t even acknowledge that a command was typed. Is there something wrong with the code to not allow the bot to see the command input?

Have you replaced the lock wit the emoji you want? is there any logs you can show?

This is all it shows.

But then when I go to the link it says the web is down
And when I ping the bot and the bot that used to work 100%, it just says the prefix instead of what it is coded to say.
And on top of that the code for the presence is no longer working.

If you try your changes right after you make them, they might have not gone through yet (because of establishing of websockets, etc)

is the reactionAdd handler in your main bot file?

@MrDiamond64 Main bot file? I have sub files like for commands, what do you mean by a main bot file?

@youngchief_btw I update the bot every time so the commands can be used instantly.

Your index.js or where the message handler is

@MrDiamond64 like in my package.json folder?
These are my folders.Screenshot 2020-04-02 at 11.16.04 PM

The file where your ready event is. the place where it loads commands

it loads commands in everything, or it used to.

So what exactly do you mean by the file where the “ready” event is.

the file where your bot login to discord

i just invite it, what you mean?

where is your client.login() located at?

i dont have one :eyes:

I don’t have one for my other bot that someone made for me. And it works fine

How does your bot work then?

idk, it just does.

var Spark = require(“sparkbots”)
const Command = Spark.command(“invite”)
Command.setLevel(10)
Command.allowDms(true)
module.exports = Command;
Command.code = (client, message) => {

That is the code for my invite command, does this explain how it works?

Try connecting with the same session id so it would count as resuming socket connection.

you speaking english bro?

yes… wdym

resuming socket connection? wdym by that?

socket means websockets, which is what many things use for a connection (including discord.js)

instead of long polling

Okay, NVM. Now, I think it should’ve been like this:

const Discord = require(‘discord.js’);
const client = new Discord.Client();

client.once(‘ready’, () => {
console.log(‘Ready!’);
});

client.on('messageReactionAdd', (messageReaction, user) => {
  if(user.bot)  return;
  const { message, emoji } = messageReaction;
   if(emoji.name === "the emoji here") {
     if(message.id === "message id here") {
        message.author.addRole("role-id-goes-here", "Reaction Roles")
      }
    } 
});

Though it’d be useful to check for earlier questions before adding one, such as this one here.

He’s using sparkbots instead of discord.js

Yeah @easrng , would that make a difference in the code?

Wouldn’t it be close to the same? If not can you help?
@MrDiamond64
@youngchief
@FTGxShadow

It shouldn’t, since sparkbots basically use discord.js as well, but i don’t know if they replaced some of the commands.

I’ve tried using that code and it doesn’t work. I’m trying to have the bot react to a specific message (embed) (I haven’t been able to do this so far)

…and then once it does that - I want to be able to connect those reactions to Unbelievaboat and make a store out of them. But the

That code was discord.js v11, check out discord.js.org to learn how to migrate that code to v12

discord.js v11 is also now broken.

also… welcome to glitch!

1 Like

Yes, that code is from 7 months ago, back when discord.js was still v11, you can check and refer to the docs for the new v12 docs.

1 Like