TMD
April 2, 2020, 10:51pm
1
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(‘ ’);
}
});
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!
TMD
April 3, 2020, 2:19am
3
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")
TMD
April 3, 2020, 2:29am
5
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?
TMD
April 3, 2020, 2:43am
7
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?
TMD
April 3, 2020, 3:10am
10
@MrDiamond64 Main bot file? I have sub files like for commands, what do you mean by a main bot file?
TMD
April 3, 2020, 3:11am
11
@youngchief_btw I update the bot every time so the commands can be used instantly.
Your index.js or where the message handler is
TMD
April 3, 2020, 3:15am
13
@MrDiamond64 like in my package.json folder?
These are my folders.
The file where your ready event is. the place where it loads commands
TMD
April 3, 2020, 3:22am
15
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
TMD
April 3, 2020, 3:35am
17
i just invite it, what you mean?
where is your client.login()
located at?
TMD
April 3, 2020, 3:36am
19
i dont have one
I don’t have one for my other bot that someone made for me. And it works fine
How does your bot work then?