Search code examples
node.jsdiscord.jsembedupdatesmessage

Discord.js - Updating a MessageEmbed in the following code


So, I have (this is only part of it) the following code, but I can't figure out this whole updating a sent embed thing...

The coding is all working if I send a new embed every time, but I don't want to clutter the channel; thus attempting to update the first embed.

Code:

const filter = m => m.author.id === message.author.id;
let hangembedStart = new Discord.MessageEmbed()
    .setDescription("Let's get started!")
    .setColor('#0099ff')
    .setThumbnail(sicon)
    .addField('Word:', asterisc)
message.channel.send(hangembedStart);
const collector = message.channel.createMessageCollector(filter, {
    maxMatches: 9,
    time: 30000
});
collector.on('collect', m => {
    if (m.content === 'cancel') {
        inProgress = false;
        delete guessed;
        collector.stop();
        return;
    }
    if (lowChar === text) {
        message.channel.send(`Congratulations, you guessed the word!`);
        inProgress = false;
        delete guessed;
        collector.stop();
        return;
    }
    let hits = checkChar(lowChar, text);
    if (hits === 0) {
        let hangembedGuess = new Discord.MessageEmbed()
            .setDescription("Hangman - The Game - In progress")
            .setColor('#0099ff')
            .setThumbnail(sicon)
            .addField('Word:', reveal)
            .addField('Guessed:', guessed.join(" "))
        message.channel.send(hangembedGuess);
    } else if (hits > 0) {
        let hangembedGuess = new Discord.MessageEmbed()
            .setDescription("Hangman - The Game - In progress")
            .setColor('#0099ff')
            .setThumbnail(sicon)
            .addField('Word:', reveal)
            .addField('Guessed:', guessed.join(" "))
        message.channel.send(hangembedGuess);
    }
});
collector.on('end', collected => {
    message.channel.send(`Game ended, word was: ${text}!`);
    inProgress = false;
    delete guessed;
    //collector.stop();
});

How the ... can I update the first embed in this code, instead of sending a new one each time? I tried using message.edit() but that triggers: UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot edit a message authored by another user

I've googled, read, searched, tried, tested just about everything I've come across, but can't wrap my head around this one...


Solution

  • Got it sorted!!

    Added the following line:

    const hangmanMessage = await message.channel.send(hangembedStart);
    //above
    const filter = m => m.author.id === message.author.id;
    

    Then changed the following line(s):

    message.channel.send(hangembedGuess);
    //to this
    hangmanMessage.edit(hangembedGuess);
    

    Now it updates the first embed, instead of sending a new one each time :D