How to check to see if a reaction is posted in discord.js - node.js

I have a discord.js bot that I would like to have a gateway entry on. It works that when they react to a reaction on a message, they get a role. How would I be able to accomplish that?

Hope you find this useful :)
flutter.on('guildMemberAdd', async member => {
let welcomeRole = member.guild.roles.find(role => {return role.id==="ROLE ID"});
await member.addRole(welcomeRole);
Promise.resolve(flutter.channels.get("CHANNEL ID")).then(async welcome => {
const msg = `Welcome to the community. :emojiName: We are **please** that you have joined us!`;
Promise.resolve(welcome.send(msg)).then(async message => {
flutter.on('messageReactionAdd', async (reaction, user) => {
if(user === message.author.bot) return;
if(reaction.emoji.name === "👍") {
let role = member.guild.roles.find(role => {return role.id==="ROLE TO REMOVE ID"});
await member.removeRole(role);
let roleTwo = member.guild.roles.find(role => {return role.id==="ROLE TO ADD ID"});
await member.addRole(roleTwo);
}
});
});
});
});
Wherever you see the word flutter just change it with your bots client name.
Example:
const Discord = require("discord.js");
const client = new Discord.Client();
Best of luck~

Related

Reactionrole does not work with custom emoji but will work using default ones

Hi everyone I have been trying to make my reactionrole work for a bit now and have run into a problem. So far my bot doesnt seem to work using a custom emoji on my own discord server but seems to run fine with default ones I used a default and custom one to check. I don't get a log for 2 so it seems to go wrong there. I also tried changing reaction.emoji.name to id but with no result. Any help would be appreciated.
const yellowTeamRole = message.guild.roles.cache.find(role => role.name === "Iron");
const blueTeamRole = message.guild.roles.cache.find(role => role.name === "Gold");
const yellowTeamEmoji = '<:Iron:1006186923917852712>';
const blueTeamEmoji = '🔴';
let embed = new Discord.MessageEmbed()
.setColor('#e42643')
.setTitle('Choose a team to play on!')
.setDescription('Choosing a team will allow you to interact with your teammates!\n\n'
+ `${yellowTeamEmoji} for yellow team\n`
+ `${blueTeamEmoji} for blue team`);
let messageEmbed = await message.channel.send({embeds: [embed]});
messageEmbed.react(yellowTeamEmoji);
messageEmbed.react(blueTeamEmoji);
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id == channel) {
console.log('1');
if (reaction.emoji.name === yellowTeamEmoji) {
console.log('2');
await reaction.message.guild.members.cache.get(user.id).roles.add(yellowTeamRole);
}
if (reaction.emoji.name === blueTeamEmoji) {
await reaction.message.guild.members.cache.get(user.id).roles.add(blueTeamRole);
}
} else {
return;
}
});
You don't get a log because in your if statement you're comparing the whole emote to only its name.
You're basically doing this:
if("Iron" === "<:Iron:1006186923917852712>")
What you should do is get the emote in your client or guild and access the name property through .name
// the id of your emote
const foundEmote = client.emojis.cache.find(e => e.id === "1006186923917852712");
if(reaction.emoji.name === foundEmote.name) {
// your code
}

Discord Reaction Roles not working after the bot restarts

Recently I decided to update my Discord Bot from V12 to V13 and I encountered a problem with my Reaction Roles.
It only works if the bot doesn't restart. It worked perfectly on discord.js 12.5.3 and now I'm using discord.js 13.5.0. Could you guys help me with the problem?
Here is my code:
const { MessageEmbed } = require("discord.js");
const Discord = require('discord.js');
const { Client } = require('discord.js');
const client = new Client({
intents: 32767,
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
const config2 = require("../config2");
module.exports = async (client, message, args) => {
client.on('messageCreate', async message => {
if (message.content.startsWith("22ar")) {
const embed = new MessageEmbed()
.setColor('#FFA500')
.setTitle(`Pick the roles`)
.setDescription(`🔓 **- You unlock yourself.**
✅ **- You get access to the server.**
💜 **- You get notified for X things.**
[🔓, ✅] - obligatory.
[💜] - optional.
\`Wait at least 30 seconds and try again if you don't get the roles.\``)
const msg = await message.channel.send({ embeds: [ embed ] })
msg.react('🔓')
msg.react('✅')
msg.react('💜')
}
})
const CHANNEL = config2.ROLECHANNEL;
const UROLE = config2.UNLOCKROLE;
const MROLE = config2.MEMBERROLE;
const NROLE = config2.NOTIFYROLE;
const unlockEmoji = '🔓';
const checkEmoji = '✅';
const notifyEmoji = '💜';
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Fetching message failed: ', error);
return;
}
}
if (!user.bot) {
if(reaction.message.channel.id === CHANNEL) {
if (reaction.emoji.name == checkEmoji) {
const memberRole = reaction.message.guild.roles.cache.find(r => r.id === MROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.add(MROLE);
}
if (reaction.emoji.name == unlockEmoji) {
const unlockRole = reaction.message.guild.roles.cache.find(r => r.id === UROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.remove(UROLE)
}
if (reaction.emoji.name == notifyEmoji) {
const notifyRole = reaction.message.guild.roles.cache.find(r => r.id === NROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.add(NROLE)
}
}
client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.partial) {
try {
await reaction.fetch();
} catch (error) {
console.error('Fetching message failed: ', error);
return;
}
}
if (!user.bot) {
if(reaction.message.channel.id === CHANNEL) {
if (reaction.emoji.name == checkEmoji) {
const memberRole = reaction.message.guild.roles.cache.find(r => r.id === MROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.remove(memberRole);
}
if (reaction.emoji.name == unlockEmoji) {
const unlockRole = reaction.message.guild.roles.cache.find(r => r.id === UROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.remove(unlockRole)
}
if (reaction.emoji.name == notifyEmoji) {
const notifyRole = reaction.message.guild.roles.cache.find(r => r.id === NROLE);
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.remove(notifyRole)
}
}
}
})
}
})
}
I am not getting any error in the console and the bot continues to work as usual.
Thank you for your attention.
Messages sent before your bot started are uncached unless you fetch them first. By default, the library does not emit client events if the data received and cached is not sufficient to build fully functional objects. Since version 12, you can change this behavior by activating partials.
Official guide about this here (Listening for reactions on old messages).

Discord.js Multiple Commands

how can i make multiple commands with the discord.js module?
code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', msg => {
if (msg.content === '!hello') {
msg.reply('Hello!!');
}
});
client.login('token');
So, how do i make it so I can use multiple commands? Like !hi and !whatsup.
I hope someone can help.
Thanks
You can resume your if statement with else if()
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', msg => {
if (msg.content === '!hello') {
msg.reply('Hello!!');
} else if(msg.content === "!hi") {
msg.reply("Hi there!!");
}
});
client.login('token');
as a temporary solution, you can use "else if()" like so:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', message => {
if(message.content === '!command1') {
message.channel.send('working!');
} else if(message.content === '!command2') {
message.channel.send('working!');
} else if(message.content === '!command3') {
message.channel.send('working');
}
});
client.login('Your Token goes here');
a better solution is to set up a command handler. there are a lot on youtube, one is: https://youtu.be/AUOb9_aAk7U
hope this helped!
its really easy
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', async message => {
if(message.content.toLowerCase().startsWith("hello")) {
message.channel.send("hola")
}
if(message.content.startsWith("hi there")) {
message.channel.send("hello")
}
})
You can use if() as a temporary solution but it would be better to setup a command handler.
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('message', async message => {
if(message.content.toLowerCase().startsWith("command1")) {
message.channel.send("...")
}
if(message.content.toLowerCase().startsWith("command2")) {
message.channel.send("...)
}
})

Delete message after a user reacts

I am trying to make a function in my bot that when I say $addlist it sends a message to a to-do channel in my discord server. I have acheived the sending and reacting to the message, but I don't know how to delete the todo when I react to it.
Here is the code:
exports.run = (client, message, args) => {
var Discord = require("discord.js");
let msg = message.content.slice(9);
if (message.author.id !== "502823349110571008") return;
const embed = new Discord.MessageEmbed()
.setTitle("New To-Do incoming.")
.setDescription("```" + msg + "```")
.setFooter("Sent")
.setTimestamp()
.setColor("YELLOW");
client.channels.cache.get(`817477111509024819`).send(embed).then(m => {
m.react("❌")
})
}
EDIT:
Listen for all added reactions and filter:
client.on("messageReactionAdd", (reaction, user) => {
if (user === client.user) return;
if (reaction.message.channel.id === "817477111509024819") {
if (reaction.emoji.name === "❌") {
reaction.message.delete();
}
}
});
Old (not working in this case):
After you reacted to the message you can do the following:
// ...
client.channels.cache.get(`817477111509024819`).send(embed).then(m => {
m.react("❌").then(() => { // React to message
m.awaitReactions((r, u) => r.emoji.name === "❌").then(() => { // Await reaction with emoji name "❌"
m.delete(); // Delete the message
));
});
});

How would I get my discord.js bot to add a role when someone reacts to a message?

Right now, I'm trying to make a reaction roles bot, but I keep on getting an error. Here is my code.
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message => {
if (message.author !== null && message.author.bot) return;
if (message.toString().toLowerCase().includes('start reaction roles')) {
message.delete()
const embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('React to get notified')
.setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> 🖐 | React with this emoji to get <#&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <#&727633811709362187>.\nYou can chose one of the emojis, or both of them.`)
message.channel.send(embed).then(sentMessage => {
sentMessage.react("🖐")
.then(() => sentMessage.react("⚠️"))
.catch(() => console.error('One of the emojis failed to react.'));
})
const collector = sentMessage.createReactionCollector();
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '🖐'){
role = message.guild.roles.find(role => role.name === "ne1 here");
message.member.addRole(role);
}
else if (reaction.emoji.name === '⚠️'){
role = message.guild.roles.find(role => role.name === "HR notifications");
message.member.addRole(role);
}
})
}
});
keepAlive();
client.login(process.env.TOKEN);
The part where it says const collector = sentMessage.createReactionCollector(); is not working. If someone could please help me with this, that would be great.
I just figured out how to make it run. Here is the code if you want to use it (this is my entire code. Remove some parts if needed).
const Discord = require('discord.js');
const keepAlive = require('./server');
const client = new Discord.Client();
client.on('message', message => {
if (message.author !== null && message.author.bot) return;
if (message.toString().toLowerCase().includes('start reaction roles')) {
message.delete()
const embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('React to get notified')
.setDescription(`Hello! Please react to this message to get a role. Here are the roles that you can get by reacting:\n> 🖐 | React with this emoji to get <#&727665971443269635> notifications.\n> ⚠️ | React with this emoji to get <#&727633811709362187>.\nYou can chose one of the emojis, or both of them.\n\n**NOTE:**\nCurrently, this command does not support removing your roles. If you want to remove your role, click on the emoji. Then, click on your profile, and click the X next to the role you want to remove. You can always get it back by reacting.`)
message.channel.send(embed).then(sentMessage => {
sentMessage.react("🖐").then(() => sentMessage.react("⚠️")).then(() => {
const filter = (reaction, user) => {
return true;
}
const collector = sentMessage.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '🖐'){
role = message.guild.roles.cache.find(role => role.name === "ne1 here");
message.member.roles.add(role);
}
else if (reaction.emoji.name === '⚠️'){
role = message.guild.roles.cache.find(role => role.name === "HR notifications");
message.member.roles.add(role);
}
})
})
.catch(() => console.error('One of the emojis failed to react.'));
})
}
});
keepAlive();
client.login(process.env.TOKEN);
I hope this is useful.

Resources