【问题标题】:Delete invocator message after bot's answer机器人回答后删除调用者消息
【发布时间】:2019-11-19 09:27:35
【问题描述】:

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

我搜索“提示”并找到了这个主题 (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 => {

谢谢

【问题讨论】:

  • .delete() 返回一个承诺,因此很可能是异步的。但我不确定它是否与您看到的错误直接相关。
  • 您确定您的机器人有权删除该消息。另外,'它运行良好,直到出现控制台错误'是什么意思?它是否有效然后停止删除消息,或者代码是否有效直到这一行?
  • @JackRed 是的,我的机器人获得了所有权限,并且通过“'它运行良好,直到出现控制台错误”我想说,消息被正确删除,机器人仍然有效除了控制台,一切都很好。
  • 您确定问题与这条线有关吗?因为从我的角度来看,承诺似乎得到了正确的处理
  • 只有在邮件被删除时才会发生,我会尝试不删除。编辑:我试过没有删除“命令”,it just works

标签: javascript node.js discord discord.js


【解决方案1】:

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

所以代码应该是这样的:

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

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2017-07-10
    • 2020-08-24
    • 2021-10-14
    • 2022-01-06
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2021-08-06
    • 2021-11-13
    相关资源
    最近更新 更多