按 ID 删除消息
Delete message by ID
我创建了一个机器人,它在我的服务器上显示投票面板,理想情况下我希望它删除旧消息并发送新消息以在人们投票时更新投票。
我已经设法使用 message.channel.fetchMessage 获取旧消息 ID,'LastMessageID' 是正确的 ID,但我似乎无法找到 select 和删除的方法该消息不会在我的控制台中产生大量错误。
例如我试过:
message.channel.fetchMessage(LastMessageID)
.then(message.delete)
它只是给出了以下错误:
(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.
我真的很傻,我不知道如何做像按 ID 删除消息这样简单的事情。任何帮助将不胜感激。
这就是您要找的东西:找到邮件然后将其删除。
// 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());
这很好,但让我建议您一个更好的方法:
// 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.");
});
我创建了一个机器人,它在我的服务器上显示投票面板,理想情况下我希望它删除旧消息并发送新消息以在人们投票时更新投票。
我已经设法使用 message.channel.fetchMessage 获取旧消息 ID,'LastMessageID' 是正确的 ID,但我似乎无法找到 select 和删除的方法该消息不会在我的控制台中产生大量错误。 例如我试过:
message.channel.fetchMessage(LastMessageID)
.then(message.delete)
它只是给出了以下错误:
(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.
我真的很傻,我不知道如何做像按 ID 删除消息这样简单的事情。任何帮助将不胜感激。
这就是您要找的东西:找到邮件然后将其删除。
// 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());
这很好,但让我建议您一个更好的方法:
// 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.");
});