首页 > 解决方案 > 机器人回答后删除调用者消息

问题描述

我最近开始创建一个不和谐的机器人来玩,我试图删除调用者消息,但徒劳无功。

我搜索“提示”并找到了这个主题(Discord.js Delete Single Message)。所以我使用了他的代码,它运行良好,直到出现控制台错误:

(node:8012) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message
    at item.request.gen.end (D:\Windows\Documents\discord\node_modules\discord.js\src\client\rest\RequestHandlers\Sequential.js:85:15)
    at then (D:\Windows\Documents\discord\node_modules\snekfetch\src\index.js:215:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:8012) 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: 3)
(node:8012) [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.

这是我的删除命令:

if (message.content.startsWith(prefix)) {
        message.delete()
            .then(msg => console.log(`Deleted message from ${msg.author.username}`))
            .catch(console.error);
    }

我不知道是什么原因造成的,因为我没有使用任何async

(我的消息事件client.on('message', message => {:)

谢谢

标签: javascriptnode.jsdiscorddiscord.js

解决方案


好的,所以我找到了解决问题的方法。我只需要在 message.delete() 中添加超时

所以代码应该是这样的:

if (message.content.startsWith(prefix)) {
        message.delete(1000)
            .then(msg => console.log(`Deleted message from ${msg.author.username}`))
            .catch(console.error);
    }

谢谢您的帮助。


推荐阅读