Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.js how to edit/update embed?

I'm working on a "small" bot for fun and currently trying to create a blackjack command. The first half works fine, but the problem appears when I want to update the embed that was already posted by the bot. I keep getting an error: UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot edit a message authored by another user Here is part of the code:

        const embd = new Discord.MessageEmbed()
            .addFields(
                { name: 'Dealer cards: ' + botCards + ' + ?'},
                { name: 'Your cards: ' + userCards},
            )

            message.channel.send(embd).then(embdReact => {
                embdReact.react('🟩');
                embdReact.react('🟥');

                const filter = (reaction, user) => {
                    return ['🟩','🟥'].includes(reaction.emoji.name) && user.id === message.author.id;
                };
            
                embdReact.awaitReactions(filter, { max: 1, time: 60000})
                    .then(collected => {
                        const reaction = collected.first();
            
                        if (reaction.emoji.name === '🟩'){
                            const newEmbd = new Discord.MessageEmbed()
                                .setTitle("Wow");
                            message.edit(newEmbd);
                        }
                        else {
                            message.reply('boo');
                        }
                        })
            }) 

For testing I tried to change just the title, but in the perfect world a corresponding field would be updated. Ex: "Your cards:" field.

like image 781
Wowy Avatar asked Oct 16 '22 02:10

Wowy


1 Answers

You're editing the wrong message :

The line :

message.edit(newEmbd);

should be :

embdReact.edit(newEmbd);

Hope this will help you to fix your issue !

like image 119
Tenclea Avatar answered Oct 20 '22 16:10

Tenclea