How can the bot edit its message once the command executor's command has been edited?

Viewed 772
if(message.content === "hello") {
   message.channel.send("World");
}
if(message.content === "hi") {
   message.channel.send("There");
}

I'm trying a new feature for my bot, but I am not entirely sure how to start it. So I have the commands about this message. So what I'm trying to do is when I send Hello to the bot will answer World and if I edited the message to hi the bot will edit its message as well; from World to There.

discord.js version: 12.2.0

2 Answers

This isn't a stable solution because there isn't a proper way to edit the bot's message in this situation, so you'd have to just get the bot's last message in the channel where the message was editted and edit it.

You'd have to use the messageUpdate event like so:

client.on('messageUpdate', async (oldMessage, newMessage) => {
  if (oldMessage.content === 'hello' && newMessage.content === 'hi') {
    const messages = await newMessage.channel.messages.fetch()
    const lastMsg = messages.filter((msg) => msg.author.id === client.user.id).first()
    lastMsg.edit('there')
  }
})

You should do something like this. I tired it and it works.

client.on('messageUpdate', (oldMessage, newMessage) => {
        
        if (newMessage.content === "hello") {
            message.channel.send("World");
        }
        if (newMessage.content === "hi") {
            message.channel.send("There");
        }
   
})

You can combine thie also with deleting the previous message.

Related