my goal is to use a command that add reactions and you can react to change the text, so when I click and the reaction 1 that's edit the original text to : 1 and same for the others... that's pretty much working, but when i'm trying to remove and re-add the reaction, that's not working I think it's because of : { max: 1 } here's my code :
bot.on('message', msg => {
if(msg.content === PREFIX + "test") {
msg.channel.send(`${msg.author.username}, exemple`).then((sentMessage) => {
setTimeout(function() {
sentMessage.react("⏪");
}, 500);
setTimeout(function() {
sentMessage.react("⬅️");
}, 1000);
setTimeout(function() {
sentMessage.react("➡️");
}, 1500);
setTimeout(function() {
sentMessage.react("⏩");
}, 2000);
let reactionFilter = (reaction, user) => {
reaction.emoji.name === '⏪' && user.id !== "bot id" && user.id == msg.author.id
}
sentMessage.awaitReactions(reactionFilter, { max: 1 }).then(() => {
sentMessage.edit(`1`)
}
)
let reactionFilter2 = (reaction, user) => {
reaction.emoji.name === '⬅️' && user.id !== "bot id" && user.id === msg.author.id
}
sentMessage.awaitReactions(reactionFilter2, { max: 1 }).then(() => {
sentMessage.edit(`2`)
}
)
let reactionFilter3 = (reaction, user) => {
reaction.emoji.name === '➡️' && user.id !== "bot id" && user.id === msg.author.id
}
sentMessage.awaitReactions(reactionFilter3, { max: 1 }).then(() => {
sentMessage.edit(`3`)
}
)
let reactionFilter4 = (reaction, user) => {
reaction.emoji.name === '⏩' && user.id !== "bot id" && user.id === msg.author.id
}
sentMessage.awaitReactions(reactionFilter4, { max: 1 }).then(() => {
sentMessage.edit(`4`)
}
)
})
}
})
Thanks !
Yes it's because of the max limit, you should prob instead be using one reaction collector instead:
const emojis = ["⏪", "⬅️", "➡️", "⏩"];
//no need to check if the user id isnt the bot
const filter = (reaction, user) => emojis.includes(reaction.emoji.name) && user.id === msg.author.id;
// adding a time is probably a better option
const collector = sentMessage.createReactionCollector(filter, { time: 5000 });
collector.on("collect", reaction => {
// array is 0 indexed so just add 1 to the index and you get the number you wanted
// might need to convert the number into a string aswell
sentMessage.edit("" + (emojis.indexOf(reaction.emoji.name) + 1));
});
Should note this doesn't do anything if a reaction is removed, which I think is best because it would be hard to decide what the text should be after it's removed
Related
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();
}
});
i have code that creates a reaction menu then outputs whichever option the user chose
client.on('message', async message => {
if (message.author.bot) return;
if (message.content === 'y.Einek')
{
const sentMessage = await message.channel.send(Einek);
const reactions = ['🇳', '🇭', '🇽'];
for (const reaction of reactions) await sentMessage.react(reaction);
const filter = (reaction, user) => reactions.includes(reaction) && user.id === message.author.id;
const collected = await sentMessage.awaitReactions(filter, { max: 1, timeout: 5000, errors: ['time'] }).catch(() => null);
if (!collected) return message.channel.send('You either took too long or something went wrong selecting your difficulty.');
const { name } = collected.first().emoji;
const response = name === '🇳' ? 'Normal' : (name === '🇭' ? 'Hard' : 'Extreme');
return message.channel.send(response);
}
});
and everything works fine except it doesn't output "response"
This code should work. Using the catch method for any error instead.
client.on('message', async message => {
if (message.author.bot) return;
if (message.content === 'y.Einek') {
const sentMessage = await message.channel.send(Einek);
const reactions = ['🇳', '🇭', '🇽'];
for (const reaction of reactions) await sentMessage.react(reaction);
const filter = (reaction, user) => reactions.includes(reaction) && user.id === message.author.id;
sentMessage.awaitReactions(filter, { max: 1, timeout: 5000, errors: ['time'] }).then(collected => {
const { name } = collected.first().emoji;
const response = name === '🇳' ? 'Normal' : (name === '🇭' ? 'Hard' : 'Extreme');
sentMessage.channel.send(response);
}).catch(() => message.channel.send('You either took too long or something went wrong selecting your difficulty.'));
}
});
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();
});
});
I have a command which allows me to go and query an API to retrieve images and then display them in discord.
Currently I come to search from my API 10 images, I want to set up a navigation system thanks to the discord reactions. Everything works but the problem is that once the user has clicked the reaction remains active. I need to delete the reaction to improve the user experience. Currently it is therefore necessary to double click which is not very practical. Here's the method that doesn't work for deletion :
const removeReaction = (m, msg, emoji) => {
try {
m.reactions.find(r => r.emoji.name == emoji).users.remove(msg.author.id);
} catch(err) {
console.log('err: ', err)
}
}
here is all my code
bot.on('message', async msg => {
if (msg.content.startsWith('!test')) {
const args = msg.content.slice('!test').split(' ')
console.log('args :', args[1])
axios(`https://json-api.example.com/posts?tags=${args[1]}&limit=10`, {
method: 'GET',
})
.then((response) => {
if (response.status === 500) {
msg.channel.send('error')
} else {
if (response.data.posts.length === 0) {
msg.channel.send(`No result for ${args[1]}`)
}
if (response.data.posts.length > 0) {
const resultsLength = response.data.posts.length
const options = {
limit: 60000,
min: 1,
max: resultsLength - 1,
page: 1
}
const pages = []
response.data.posts.map((i, index) => {
pages.push({
"title": `Result page number ${index} for ${args[1]}`,
"url": `${response.data.posts[index].sample_url}`,
"color": 43333,
"footer": {
"icon_url": "https://cdn.discordapp.com/app-icons/708760465999790244/228b2993e942a361518b557ee4511b26.png?size=32",
"text": "Cool footer"
},
"image": {
"url": `${response.data.posts[index].url}`
},
"fields": [
{
"name": "tags",
"value": `${response.data.posts[index].tags[0] || '/'}, ${response.data.posts[index].tags[1] || '/'}, ${response.data.posts[index].tags[2] || '/'}, ${response.data.posts[index].tags[3] || '/'}`
}
]
})
})
const m = msg.channel.send({ embed: pages[options.page] }).then((el) => {
el.react('⬅️')
el.react('➡️')
el.react('🗑️')
})
const filter = (reaction, user) => {
return ['⬅️', '➡️', '🗑️'].includes(reaction.emoji.name) && user.id == msg.author.id
}
const awaitReactions = (msg, m, options, filter) => {
const { min, max, page, limit } = options
m.awaitReactions(filter, { max: 1, time: limit, errors: ['time'] })
.then((collected) => {
const reaction = collected.first()
const removeReaction = (m, msg, emoji) => {
try { m.reactions.find(r => r.emoji.name == emoji).users.remove(msg.author.id); } catch(err) { console.log('err: ', err) }
}
if (reaction.emoji.name === '⬅️') {
removeReaction(m, msg, '⬅️')
if (page != min) {
page = page - 1
m.edit({ embed: pages[page] })
}
awaitReactions(msg, m, options, filter)
}
else if (reaction.emoji.name === '➡️') {
removeReaction(m, msg, '➡️');
if (page != max) {
page = page + 1
m.edit({ embed: pages[page] })
}
awaitReactions(msg, m, options, filter);
}
else if (reaction.emoji.name === '➡️') {
removeReaction(m, msg, '➡️');
if (page != max) {
page = page + 1
m.edit({ embed: pages[page] })
}
awaitReactions(msg, m, options, filter);
}
else if (reaction.emoji.name === '🗑️') {
return m.delete()
}
else {
awaitReactions(msg, m, options, filter)
}
}).catch((err) => { console.log('err: ', err) })
}
awaitReactions(msg, m, options, filter)
Thanks in advance for your help
so, after much research I finally found. I share the answer here.
With the 12+ version of discord.js package a lot has evolved including my problem:
m.reactions.find(r => r.emoji.name == emoji).users.remove(msg.author.id);
must become :
m.reactions.cache.find(r => r.emoji.name == emoji).users.remove(msg.author);
I have made this code a while ago.
It is for an older version of Discord.js but according to docs it should still work.
Basically what you want to do is to use Discord.js createReactionCollector function after you send the picture, then inside this.reactionCollector.on('collect', (reaction, reactionCollector) => {//do your work here}) you can change the picture then call reaction.remove(user); which immediately removes the reaction for that user.
Here's my code:
Note: I have made a class for this command so it might look a bit different for you.
Also in my version, only the user who initially called the command can change the picture. So you will need to modify that part if you want to let anyone change the picture. (It might be a pretty bad idea if your bot is in a large server.)
this.channel.send({embed}).then(msg => {
this.message = msg;
this.message.react('⬅️').then(x => {
this.message.react('➡️');
})
const filter = (reaction, user) => {
return (reaction.emoji.name === '➡️' || reaction.emoji.name === '⬅️') && user.id === this.user.id;
};
this.reactionCollector = this.message.createReactionCollector(filter, { time: 600000 });
this.reactionCollector.on('collect', (reaction, reactionCollector) => {
if(reaction.emoji.name === '➡️') {
if(this.index + 1 < result.length) {
this.index++;
var embed = this.makeEmbed(result[this.index].img);
this.message.edit({embed});
}
}
else if(reaction.emoji.name === '⬅️') {
if(this.index - 1 > -1) {
this.index--;
var embed = this.makeEmbed(result[this.index].img);
this.message.edit({embed});
}
}
reaction.remove(this.user);
});
});