javascript - 机器人回答后删除调用者消息
问题描述
我最近开始创建一个不和谐的机器人来玩,我试图删除调用者消息,但徒劳无功。
我搜索“提示”并找到了这个主题(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 => {
:)
谢谢
解决方案
好的,所以我找到了解决问题的方法。我只需要在 message.delete() 中添加超时
所以代码应该是这样的:
if (message.content.startsWith(prefix)) {
message.delete(1000)
.then(msg => console.log(`Deleted message from ${msg.author.username}`))
.catch(console.error);
}
谢谢您的帮助。
推荐阅读
- inno-setup - 在 Inno 下载插件中自定义“正在下载”消息
- python - 基于特定子字符串的python正则表达式提取
- python - 这个代码块在这个程序中做了什么?
- node.js - 创建 nodejs webapp - azure 和 Visual Studio 代码
- c# - 性能差异 WndProc WM_PAINT 与 OnPaint
- python - 拦截来自本地 Web 应用程序的所有 REST 和 SOAP 请求和响应
- azure - 使用逻辑应用提取电子邮件正文
- javascript - 如何将一个属性映射到 litElement 中的两个属性?
- c++ - 如何遍历 C++ 中的未知对象方法?
- qt - 在再次使用之前清除 Qt 中的 GridLayout