izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Update existing message #302

Closed bomb-on closed 5 years ago

bomb-on commented 5 years ago

Is it possible to edit/update the message sent with bot.sendMessage() from the README example?

bot.on('message', function(user, userID, channelID, message, event) {
    if (message === "ping") {
        bot.sendMessage({
            to: channelID,
            message: "pong"  // THIS IS THE MESSAGE I'D LIKE TO UPDATE SOMETIMES IN THE FUTURE
        });
    }
});
Peacerekam commented 5 years ago

use callback function to get message's ID

var m_id = '';

// ...

bot.sendMessage({
     to: channelID,
    message: "pong"  // THIS IS THE MESSAGE I'D LIKE TO UPDATE SOMETIMES IN THE FUTURE
}, function (err, res){
      // this is inside of callback function

      // err     =>  contains potential error
      // res     =>  contains message object (response)
      // res.id  =>  contains "pong" 's messageID

      m_id = res.id
});

now you can use bot.editMessage sometime in the future (you might want to save channelID too tbh)

bot.editMessage({
   channelID: channelID,
   messageID: m_id,
   message: "pongy pong"
})
bomb-on commented 5 years ago

Thanks a lot!!