Javascript 发送消息并很快将其删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46907207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:39:41  来源:igfitidea点击:

Send message and shortly delete it

javascriptdiscorddiscord.js

提问by haley

I'm trying to make a Discord bot delete its "system messages" after, say, 10 seconds, because I've been seeing a lot of "Invalid command" errors and "Done!" notifications, and I'd like to clear them out for actual messages. This is different from deleting messages where the user has a command; I already have that capability.

我试图让 Discord 机器人在 10 秒后删除它的“系统消息”,因为我看到了很多“无效命令”错误和“完成!” 通知,我想清除它们以获取实际消息。这与删除用户有命令的消息不同;我已经有那个能力了。

回答by LW001

I recommend you send the message, wait for the response and delete the returned message after that time. Here's how it'd work:

我建议您发送消息,等待响应并在此时间之后删除返回的消息。这是它的工作原理:

message.reply('Invalid command')
  .then(msg => {
    msg.delete(10000)
  })
  .catch(/*Your Error handling if the Message isn't returned, sent, etc.*/);

As you can read from the Docs, you can just pass a value in Milliseconds to Message.delete()as a timeout.

正如您可以从Docs 中读取的那样,您可以将一个以毫秒为单位的值Message.delete()作为超时传递给。

回答by Tajni

Current API differs from older version. Now proper way to pass timeout looks like this.

当前 API 与旧版本不同。现在传递超时的正确方法如下所示。

message.reply('Invalid command!')
  .then(msg => {
    msg.delete({ timeout: 10000 })
  })
  .catch(console.error);

Discord.js v12

Discord.js v12