admin管理员组文章数量:1415484
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 yourmessage.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
1 Answer
Reset to default 1This 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.");
});
本文标签: javascriptDelete message by IDStack Overflow
版权声明:本文标题:javascript - Delete message by ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745150451a2644897.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论