最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete message by ID - Stack Overflow

programmeradmin0浏览0评论

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 message.channel.fetchMessage 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:

message.channel.fetchMessage(LastMessageID)
 .then(message.delete)

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.

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 message.channel.fetchMessage 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:

message.channel.fetchMessage(LastMessageID)
 .then(message.delete)

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.

Share Improve this question edited Oct 15, 2018 at 16:40 Federico Grandi 6,7865 gold badges33 silver badges51 bronze badges asked Oct 14, 2018 at 23:34 Sony_Sony_ 11 gold badge1 silver badge2 bronze badges 5
  • the error it self is about rejection. If you do x.then().catch(er => console.error(er)) then the current error would disappear. However this wouldn't fix the deletion. Can we see your message.delete function? – Eduard Jacko Commented Oct 15, 2018 at 0:24
  • That pretty much is the entire function. @EduardJacko // Delete the old message. if (LastMessageID != "") { message.channel.fetchMessage(LastMessageID) .then(message.delete()) .catch(er => console.error(er)) } I'm just trying to fetch the message with that ID and delete it. I know the LastMessageID is correct because I can manually copy the ID from Discord to check. (LastMessageID is just bot.user.lastMessage.id) Sorry if this isn't what you meant, I'm very new to JS. – Sony_ Commented Oct 15, 2018 at 3:22
  • @EduardJacko Alternatively, here's the code on hastebin seeing as I apparently can't figure out how ment formatting works. link – Sony_ Commented Oct 15, 2018 at 3:32
  • Well, you said you are new in JS, then maybe try .then( message => message.delete()) very mon mistake by novice. – Eduard Jacko Commented Oct 15, 2018 at 5:40
  • also I know your ID is not the issue because the error plain about unhandled promise rejection which es from github./discordjs/discord.js/blob/…. Basically this is undefined which mean you scope is bind somewhere else. Above suggestion should help. – Eduard Jacko Commented Oct 15, 2018 at 5:55
Add a ment  | 

1 Answer 1

Reset to default 1

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

// ASSUMPTIONS:
// 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.");
});
发布评论

评论列表(0)

  1. 暂无评论