I want to make a vote with an embed message.
When someone adds a reaction, I want to add a like and to show the number of likes in the embed. Here an example:
Very late answer. But just in case someone finds this. Theres a much shorter way.
And more useful if you have large embeds and don't want to rebuild your whole embed:
message.embeds[0].fields[0] = "Some much like";
message.edit(new Discord.RichEmbed(message.embeds[0]));
I wonder if your problem is that you're either re-using variable names, putting the old data back into the edited message, or something else. Anyway, here's something that worked for me:
1) Create an Embed
to send to the user (I assume you already did this, creating the Embed
you showed on imgr):
const embed = new Discord.RichEmbed({
title: 'Suggestion by someone',
description: 'This is a test suggestion. Can you please like it or dislike it :)',
fields: [{
name: 'Like:',
value: '<3'
}]
});
2) Send Embed
to your channel (I added some Reaction
s to it - possibly the same way as you):
// add reaction emojis to message
message.channel.send(embed)
.then(msg => msg.react('✅'))
.then(mReaction => mReaction.message.react('❎'))
.then(mReaction => {
// fun stuff here
})
.catch(console.log);
3) Create a ReactionCollector
inside where I put // fun stuff here
(you can use a different reactionFilter
and time limit):
const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
// createReactionCollector - responds on each react, AND again at the end.
const collector = mReaction.message
.createReactionCollector(reactionFilter, {
time: 15000
});
// set collector events
collector.on('collect', r => {
// see step 4
});
// you can put anything you want here
collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
4) In the 'collect'
event (where I put // see step 4
), create a new Embed
with mostly similar values (or not - you change whatever you want), then put that new Embed
back into the original message via .edit(...)
:
// immutably copy embed's 'Like:' field to new obj
let embedLikeField = Object.assign({}, embed.fields[0]);
// update 'field' with new value - you probably want emojis here
embedLikeField.value = '<3 <3 <3';
// create new embed with old title & description, new field
const newEmbed = new Discord.RichEmbed({
title: embed.title,
description: embed.description,
fields: [embedLikeField]
});
// edit message with new embed
// NOTE: can only edit messages you author
r.message.edit(newEmbed)
.then(newMsg => console.log(`new embed added`)) // this is not necessary
.catch(console.log); // useful for catching errors
So the whole thing ends up looking something like this:
const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
const embed = new Discord.RichEmbed({
title: 'Suggestion by someone',
description: 'This is a test suggestion. Can you please like it or dislike it :)',
fields: [{
name: 'Like:',
value: '<3'
}]
});
// add reaction emoji to message
message.channel.send(embed)
.then(msg => msg.react('✅'))
.then(mReaction => mReaction.message.react('❎'))
.then(mReaction => {
// createReactionCollector - responds on each react, AND again at the end.
const collector = mReaction.message
.createReactionCollector(reactionFilter, {
time: 15000
});
// set collector events
collector.on('collect', r => {
// immutably copy embed's Like field to new obj
let embedLikeField = Object.assign({}, embed.fields[0]);
// update 'field' with new value
embedLikeField.value = '<3 <3 <3';
// create new embed with old title & description, new field
const newEmbed = new Discord.RichEmbed({
title: embed.title,
description: embed.description,
fields: [embedLikeField]
});
// edit message with new embed
// NOTE: can only edit messages you author
r.message.edit(newEmbed)
.then(newMsg => console.log(`new embed added`))
.catch(console.log);
});
collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
})
.catch(console.log);
For my code, edits are only made when the ✅ emoji is pressed, just for fun. Please let me know if you need help editing the code above. Hope it helps.