【问题标题】:Making the bot stop sending messages if a user hasn't sent a message within x minutes如果用户在 x 分钟内未发送消息,则使机器人停止发送消息
【发布时间】:2018-11-12 16:05:42
【问题描述】:

我有一个 !trivia 命令,它有 4 个函数,在函数内部我有一个 .then() 函数和 .setTimeout()。问题是一旦有人触发命令,它只会在有人说“!停止”时停止。如果在 5 分钟内没有人输入该频道,我该如何让它停止?

【问题讨论】:

标签: discord.js


【解决方案1】:

使用.awaitMessages()

let userUsingTheCommand = "<USER_ID>";
let filter = msg => msg.author.id = userUsingTheCommand;
// Errors: ['time'] treats ending because of the time limit as an error
function wait() {
  channel.awaitMessages(filter, {
      max: 1,
      time: 60000 * 5,
      errors: ['time']
    })
    .then(wait)
    .catch(() => {
      console.log("Time to stop!");
    });
}

【讨论】:

  • 如果我不想让其他人在琐事进行时使用该命令,我可以这样做吗?
  • 这就是filter 所做的。它检查msg.author.id 是否等于userUsingTheCommand。当用户开始琐事时,将他们的 ID 保存到userUsingTheCommand
  • 我将您的代码添加到一个全新的文件中只是为了检查它。它并没有真正在控制台中打印某人。我是不是做错了什么?
猜你喜欢
  • 2021-02-22
  • 2019-06-27
  • 1970-01-01
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 2017-03-19
  • 2021-08-19
  • 1970-01-01
相关资源
最近更新 更多