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:
Whenever someone clicks on like, all my code lines work and I finally change the Field value linked to like just like that :
messageReaction.message.embeds[0].fields[0] = "Some much like";
But the embed message doesn't update.
I've tried to update the message with this:
function doAfakeEdit(message){
message.edit(message.content);
}
It still keeps the old value of the field.
What should I do?
Editing the embedded message content To edit the content of an embed you need to pass a new EmbedBuilder structure or embed object to the messages . edit() method. If you want to build the new embed data on a previously sent embed template, make sure to read the caveats in the previous section.
An Embed object is another component of Discord messages that can be used to present data with special formatting and structure. An embed can contain the following components: Author, including link and avatar. Title.
Definition: Embedding refers to the integration of links, images, videos, gifs and other content into social media posts or other web media. Embedded content appears as part of a post and supplies a visual element that encourages increased click through and engagement.
Pro Tip: If you want to cancel an embed for one specific link, you can wrap the link in '< >' tags.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With