Search code examples
javascriptdiscorddiscord.jsmessageedit

How to make a bot edit its own message on Discord


My friend wrote this amazing code for me but it doesn't seem to work. It's meant to send a message on a command then edit the message over and over again. But when I run the code my terminal says

DiscordAPIError: Cannot edit a message authored by another user method: 'patch', path: '/channels/808300406073065483/messages/811398346853318668', code: 50005, httpStatus: 403

Is there a way to fix this problem?

client.on('message', userMessage => 
{
    if (userMessage.content === 'hi') 
    {
        botMessage = userMessage.channel.send('hi there')
        botMessage.edit("hello");
        botMessage.edit("what up");
        botMessage.edit("sup");
        botMessage.react(":clap:")
    }
});

Solution

  • The Channel#send() method returns a promise, meaning you must wait for the action to finish before being able to define it. This can be done using either .then() or async and await. From personal preference, I regularly use the second option, although I have laid both options out for you.

    Final Code

    client.on('message', async userMessage => {
      if (userMessage.content === 'hi') 
        {
            /*
              botMessage = await userMessage.channel.send('hi there')
            */
            userMessage.channel.send('hi there').then(botMessage => {
              await botMessage.edit("hello");
              await botMessage.edit("what up");
              botMessage.edit("sup");
              botMessage.react(":clap:")
            })
        }
    });