Delete Message By ID

I've created a bot that shows a vote panel for polls on my server, ideally I would like it to delete the old message and send a new one to update the poll when people vote on things.

I've managed to get the old message ID using and 'LastMessageID' is the right ID, but I can't seem to find a way to select and delete the message without making a load of errors in my console. For example I've tried:

And it just gives the following errors:

(node:82184) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'client' of undefined
    at resolve (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:480:14)
    at new Promise (<anonymous>)
    at delete (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:479:14)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7) (node:82184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:82184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I feel really silly that I can't figure out how to do something so simple as delete a message by its ID. Any help would be much appreciated.



This is the thing you're looking for: finds the message and then deletes it.

// channel: the channel you want the message to be sent in
// lastmsg: the id of the last poll message

channel.fetchMessage(lastmsg).then(msg => msg.delete());

This is fine, but let me suggest you a better way to do it:

// Option A: delete the old message and send the new one in the same function
channel.fetchMessage(lastmsg).then(async msg => {
  await channel.send("Your new message.");
  if (msg) msg.delete();

// Option B: if you have a poll dedicated channel that is kept cleaned and organized, 
// you can edit the old message (you avoid notifications for every update)
channel.fetchMessage(lastmsg).then(msg => {
  if (msg) msg.edit("Your new message.");