使用命令设置客户端超时时间

setTimeout for client using command

本文关键字:超时 时间 客户端 设置 命令      更新时间:2023-09-26

所以我要做的是为我的不和谐机器人创建一个"冷却"系统,我尝试过使用数据库方法,这里不适合我。

我正在使用discord.js包。

下面是我的代码:
if (msg.content.startsWith("!point")) {
  var usr = msg.content.split(" ").slice(1).join(" ");
  if (!usr) {
    bot.sendMessage(msg, "```Error: 1'n Reason: Please state a name.```");
    return;
  }
  if (usr == msg.author.username) {
    bot.sendMessage(msg, "```Error: 3'n Reason: You're unable to point yourself.```");
    return;
  }
  if (!db["users"][usr]) {
    db["users"][usr] = 0;
  }
  console.log(usr + " has " + db["users"][usr]);
  db["users"][usr] += 1;
  console.log(usr + " now has " + db["users"][usr]);
  fs.writeFileSync('database.json', JSON.stringify(db));
  bot.sendMessage(msg, usr + " has received 1 point");
}

除非它是非常重要的冷却存活在崩溃,或者冷却是一个很长的持续时间,我建议创建一个对象的键是通道或用户或服务器的id,这取决于你想要的速率限制应用。然后,每次执行命令时,将其设置为当前时间。就像

rateLimitObject[msg.author.id] = "current time";

那么无论何时有人运行命令,只要确保当前时间比具有该索引的对象中的时间大一定数量。如果你只是想简单地阻止人们滥发命令,类似于pvpcraft中使用的命令在这里可能更理想。https://github.com/macdja38/pvpcraft/blob/master/middleware/rateLimits.js