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).
const optionsEmbed = new Discord.MessageEmbed()
.setTitle("What would you like to setup?")
.setDescription(":one: Prefix\n:two: Mod Log Channel")
.setColor('RANDOM');
message.channel.send(optionsEmbed)
.then(async (msg) => {
await msg.react('1οΈβ£');
await msg.react('2οΈβ£');
const filter = (reaction, user) => {
return reaction.emoji.name === '1οΈβ£' || reaction.emoji.name === '2οΈβ£' && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, {
time: 30000
});
let collected = 0;
collector.on('collect', (reaction, user) => {
collected++;
if (reaction.emoji.name === "one") return managePrefix();
if (reaction.emoji.name === "two") return manageModLogChannel();
msg.delete();
collector.end();
});
collector.on('end', collected => {
if (collected === 0) return message.channel.send("You took too long to react, cancelled setup.");
});
});
async function managePrefix() {
const getCurrentPrefix = await message.getServerDatabase(message.guild.id, "prefix")
const currentPrefixEmbed = new Discord.MessageEmbed()
.setTitle("Info")
.setDescription("Current Prefix: " + getCurrentPrefix + "\n\nPlease reply with the new prefix or *cancel* to cancel.")
.setColor('RED');
message.channel.send(currentPrefixEmbed)
.then(async (msg) => {
const filter = (msgg, user) => {
return user.id == message.author.id;
}
const collector = message.channel.createMessageCollector(filter, {
time: 60000
});
let collected = 0;
collector.on('collect', async (m) => {
collected++;
msg.delete()
if (m.content.toLowerCase() === "cancel") {
m.delete();
return collector.end()
}
await message.setServerDatabase(message.guild.id, "prefix", m.content)
await m.reply("Set prefix to `" + m.content + "`");
collector.end()
});
collector.on('end', collected => {
if (collected === 0) return message.channel.send("You took too long to respond, cancelled setup.")
});
});
}
async function manageModLogChannel() {
const getCurrentPrefix = await message.getServerDatabase(message.guild.id, "modLogChannel")
const currentPrefixEmbed = new Discord.MessageEmbed()
.setTitle("Info")
.setDescription("Current ModLogChannel: <#" + getCurrentPrefix + ">\n\nPlease reply with the new channel or *cancel* to cancel.")
.setColor('RED');
message.channel.send(currentPrefixEmbed)
.then(async (msg) => {
const filter = (msgg, user) => {
return user.id == message.author.id;
}
const collector = message.channel.createMessageCollector(filter, {
time: 60000
});
let collected = 0;
collector.on('collect', async (m) => {
collected++;
msg.delete()
if (m.content.toLowerCase() === "cancel") {
m.delete();
return collector.end()
}
if (!m.mentions.channels.first()) {
m.delete()
message.channel.send("Not a valid channel, cancelled setup.");
return collector.end();
}
const channel = m.mentions.channels.first();
await message.setServerDatabase(message.guild.id, "modLogChannel", channel.id)
await m.reply("Set channel to `<#" + m.content + ">`");
collector.end()
});
collector.on('end', collected => {
if (collected === 0) return message.channel.send("You took too long to respond, cancelled setup.")
});
});
}
I am trying to make a config command, upon reacting to the first embed.. nothing happens.
If that first listener is not working, then I doubt the message collectors are working either.
The user should be able to react to a main embed which asks them if they want to change the prefix or mod log channel, from there it will then ask them what they want to change it to with the cancel option, it then saves it in the database using the custom functions (message.getServerDatabase and message.setServerDatabase())
Does anyone know what I'm doing wrong?
The reason it is not working as you want it to is that you are calling the .createReactionCollector() method on the wrong Message object. You should create the reaction collector on msg instead of message.
Like this:
const collector = msg.createReactionCollector(filter, {
time: 30000
});
Also collector.end() is not a function, you probably want to use collector.stop() instead.
So far I got the bot to add the message to a specific channel when the β reaction is triggered. I am stuck now... I'm trying to add a threshold where it takes a certain amount of β reactions to trigger the push the specific channel. Any help will be greatly apperciated.
const handleStarboard = async () => {
const starboard = bot.channels.cache.find(channel => channel.name.toLowerCase() === 'starboard');
const msgs = await starboard.messages.fetch({ limit: 100 });
const existingMsg = msgs.find(msg =>
msg.embeds.length === 1 ?
(msg.embeds[0].footer.text.startsWith(reaction.message.id) ? true : false) : false);
if(existingMsg) existingMsg.edit(`${reaction.count} - β`);
else {
const embed = new MessageEmbed()
.setAuthor(reaction.message.author.tag, reaction.message.author.displayAvatarURL())
.addField('Url', reaction.message.url)
.setDescription(reaction.message.content)
.setFooter(reaction.message.id + ' - ' + new Date(reaction.message.createdTimestamp));
if(starboard)
starboard.send('1 - β', embed);
}
}
if(reaction.emoji.name === 'β') {
if(reaction.message.channel.name.toLowerCase() === 'starboard') return;
if(reaction.message.partial) {
await reaction.fetch();
await reaction.message.fetch();
handleStarboard();
}
else
handleStarboard();
}
});
bot.on('messageReactionRemove', async (reaction, user) => {
const handleStarboard = async () => {
const starboard = bot.channels.cache.find(channel => channel.name.toLowerCase() === 'starboard');
const msgs = await starboard.messages.fetch({ limit: 100 });
const existingMsg = msgs.find(msg =>
msg.embeds.length === 1 ?
(msg.embeds[0].footer.text.startsWith(reaction.message.id) ? true : false) : false);
if(existingMsg) {
if(reaction.count === 0)
existingMsg.delete({ timeout: 2500 });
else
existingMsg.edit(`${reaction.count} - β`)
};
}
if(reaction.emoji.name === 'β') {
if(reaction.message.channel.name.toLowerCase() === 'starboard') return;
if(reaction.message.partial) {
await reaction.fetch();
await reaction.message.fetch();
handleStarboard();
}
else
handleStarboard();
}
});
Trying to make a bot that when the users click on the reaction there discord id goes into an embed Field and if they un click or click another emoji they end up in the field. This is gonna be used for a voting bot that once a certain number of users click yes or no a decision will be made to accept a user or deny a user. Any help?
exports.run = async (client, message, args) => {
message.delete({ timeout: 100 });
if (!args[0]) return message.reply('You need to supply the question');
let embed = new Discord.MessageEmbed()
.setTitle(args.join(' '))
.setDescription('Poll created by ' + message.author.tag)
.addField('Status', 'Voting is currently open.')
.setColor('#ffd700')
.attachFiles(new Discord.MessageAttachment('https://i.imgur.com/QUmbq9o.png', 'thumbnail.png'))
.setThumbnail('attachment://thumbnail.png')
.setFooter('Bot created by James (Rock)βββ');
message.channel.send(embed).then(async msg => {
await msg.react('π');
await msg.react('π');
await msg.react('π€·');
await msg.react('ποΈ');
const threshold = 6;
async function stop(result) {
collector.stop();
const newEmbed = new Discord.MessageEmbed(msg.embeds[0]);
newEmbed.title = newEmbed.title + ' [CLOSED]';
newEmbed.fields[0] = { name: 'Status', value: 'Voting is now closed.\n' + result };
newEmbed.setThumbnail('attachment://thumbnail.png');
await msg.edit(newEmbed);
msg.reactions.removeAll();
}
async function update() {
const newEmbed = new Discord.MessageEmbed(embed);
const userYes = (votes['π'].size === 0)? '-' : [...votes['π']];
const userNo = (votes['π'].size === 0)? '-' : [...votes['π']];
const userUnsure = (votes['π€·'].size === 0)? '-' : [...votes['π€·']];
newEmbed.addFields(
{ name: `User Yes (${votes['π'].size}/${threshold})`, value: userYes, inline: true },
{ name: `User No (${votes['π'].size}/${threshold})`, value: userNo, inline: true },
{ name: 'User Unsure', value: userUnsure, inline: true }
);
await msg.edit(newEmbed);
if (votes['π'].size >= threshold) {
await stop('This answer is good enough to get accepted and an upvote.');
// do something
} else if (votes['π'].size >= threshold) {
await stop('This answer is not good enough to get accepted and an upvote.');
// do something
}
}
const votes = {
'π': new Set(),
'π': new Set(),
'π€·': new Set(),
'ποΈ': new Set()
};
update();
const collector = msg.createReactionCollector((reaction, user) => !user.bot , { dispose: true });
collector.on('collect', async (reaction, user) => {
if (['π', 'π', 'π€·', 'ποΈ'].includes(reaction.emoji.name)) {
const userReactions = msg.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
for (const userReaction of userReactions.values()) {
if (userReaction.emoji.name !== reaction.emoji.name || reaction.emoji.name === 'ποΈ') {
userReaction.users.remove(user.id);
votes[userReaction.emoji.name].delete(user);
}
}
votes[reaction.emoji.name].add(user);
} else {
reaction.remove();
}
update();
});
collector.on('remove', (reaction, user) => {
votes[reaction.emoji.name].delete(user);
update();
});
});
};
module.exports.help = {
name: "poll"
}
You can read this page from the discord.js guide about reaction collectors, it tells you everything you need to know. You can read this for more info about the .createReactionCollector() method.
There are multiple ways to acheive what you want once you make the reaction collector but I beleive the easiest would look something like this:
message.channel.send('your_message_here')
.then(async function(message) {
await message.react('π');
await message.react('π');
await message.react('π€·β');
const filter = (reaction, user) => {
return ['π', 'π', 'π€·β'].includes(reaction.emoji.name) && user.id === ogauthor
}
const collector = message.createReactionCollector(filter)
collector.on('collect', (reaction, user) => {
async function collect() {
if (!user.bot) {
if (reaction.emoji.name === 'π') {
//code here
}
//repeat this for the rest of your reactions
reaction.users.remove(user.id) //you can remove the reaction once they react to it and their name is added.
}
}
collect()
});
})
One problem is that this will run forever so you should add a timer to it.
You can use Reaction Collectors for this case to listen to reactions and list them depending on it.
I've made the code below, it works as expected:
message.delete({ timeout: 100 });
if (!args[0]) return message.reply('You need to supply the question');
let embed = new Discord.MessageEmbed()
.setTitle(args.join(' '))
.setDescription('Poll created by ' + message.author.tag)
.setColor('#ffd700')
.setThumbnail("https://i.imgur.com/QUmbq9o.png")
.addFields({name: "User Yes", value: 'None'}, {name: "User No", value: 'None'}, {name: "User Hum", value: 'None'})
.setFooter("Bot created by James (Rock)βββ");
message.channel.send(embed).then(async msg => {
await msg.react('π');
await msg.react('π');
await msg.react('π€·');
const filter = (reaction, user) => {
return ["π", "π", "π€·"].includes(reaction.emoji.name);
};
const collector = await msg.createReactionCollector(filter);
collector.on('collect', (reaction, user) => {
const reactionsList = ["π", "π", "π€·"];
const fieldTitle = ["User Yes", "User No", "User Hum"];
var reactions = reaction.message.reactions.cache.array();
for(var reactionID in reactions) {
for (var i = 0; i < reactionsList.length; i++) {
if(reactionsList[i] === reaction.emoji.name){
let fieldDescription = user.id + "\n";
var users = reactions[reactionID].users.cache.array();
for(var userID in users){
if(users[userID].id === client.user.id || users[userID].id === user.id) continue;
fieldDescription += users[userID].id + "\n";
}
embed.spliceFields(i, 1, {name: fieldTitle[i], value: fieldDescription})
}
}
}
msg.edit(embed);
});
})
I have slightly modified your code and added code to track votes, edit the embed and check when the votes reach the threshold.
Demonstration
Code
message.delete({ timeout: 100 });
if (!args[0]) return message.reply('You need to supply the question');
let embed = new Discord.MessageEmbed()
.setTitle(args.join(' '))
.setDescription('Poll created by ' + message.author.tag)
.addField('Status', 'Voting is currently open.')
.setColor('#ffd700')
.attachFiles(new Discord.MessageAttachment('https://i.imgur.com/QUmbq9o.png', 'thumbnail.png'))
.setThumbnail('attachment://thumbnail.png')
.setFooter('Bot created by James (Rock)βββ');
message.channel.send(embed).then(async msg => {
await msg.react('π');
await msg.react('π');
await msg.react('π€·');
await msg.react('ποΈ');
const threshold = 1;
async function stop(result) {
collector.stop();
const newEmbed = new Discord.MessageEmbed(msg.embeds[0]);
newEmbed.title = newEmbed.title + ' [CLOSED]';
newEmbed.fields[0] = { name: 'Status', value: 'Voting is now closed.\n' + result };
newEmbed.setThumbnail('attachment://thumbnail.png');
await msg.edit(newEmbed);
msg.reactions.removeAll();
}
async function update() {
const newEmbed = new Discord.MessageEmbed(embed);
const userYes = (votes['π'].size === 0)? '-' : [...votes['π']];
const userNo = (votes['π'].size === 0)? '-' : [...votes['π']];
const userUnsure = (votes['π€·'].size === 0)? '-' : [...votes['π€·']];
newEmbed.addFields(
{ name: `User Yes (${votes['π'].size}/${threshold})`, value: userYes, inline: true },
{ name: `User No (${votes['π'].size}/${threshold})`, value: userNo, inline: true },
{ name: 'User Unsure', value: userUnsure, inline: true }
);
await msg.edit(newEmbed);
if (votes['π'].size >= threshold) {
await stop('This answer is good enough to get accepted and an upvote.');
// do something
} else if (votes['π'].size >= threshold) {
await stop('This answer is not good enough to get accepted and an upvote.');
// do something
}
}
const votes = {
'π': new Set(),
'π': new Set(),
'π€·': new Set(),
'ποΈ': new Set()
};
update();
const collector = msg.createReactionCollector((reaction, user) => !user.bot , { dispose: true });
collector.on('collect', async (reaction, user) => {
if (['π', 'π', 'π€·', 'ποΈ'].includes(reaction.emoji.name)) {
const userReactions = msg.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
for (const userReaction of userReactions.values()) {
if (userReaction.emoji.name !== reaction.emoji.name || reaction.emoji.name === 'ποΈ') {
userReaction.users.remove(user.id);
votes[userReaction.emoji.name].delete(user);
}
}
votes[reaction.emoji.name].add(user);
} else {
reaction.remove();
}
update();
});
collector.on('remove', (reaction, user) => {
votes[reaction.emoji.name].delete(user);
update();
});
});
So far I have this code but itβs only for two reactions and I want to know if someone can help me do more than two reactions? A custom emoji most preferably I will really appreciate the help.
const customEmoji = client.emojis.cache.get('707879839943753758');
message.react(customEmoji).then(() => message.react('π'));
const filter = (reaction, user) => {
return user.id === message.author.id;
};
message.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.id === customEmoji.id) {
message.reply('report something');
}
if (reaction.emoji.name === 'π') {
message.reply('did not report anything');
}
})
.catch(collected => {
message.reply('you reacted with neither a thumbs up, nor a thumbs down.');
});
You can use :nameofemoji:idofemoji in message.react()
You can get id emoji type \:youremoji: if custom emoji
const emo0 = (':nameofemoji:idofemoji')
const emo1 = (':nameofemoji:idofemoji')
message.react(`${emo0}`)
.then(() => message.react('π'))
.then(() => message.react(`${emo1}`))
.catch(() => console.error('One of the emojis failed to react.'));