【问题标题】:setTimeout for client using command使用命令为客户端设置超时
【发布时间】:2016-07-04 13:10:04
【问题描述】:

所以我想做的是为我的不和谐机器人创建一个类似“冷却”的系统,我尝试使用数据库方法 here 这对我不起作用。

我正在使用 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");
}

【问题讨论】:

  • 你没有提到你的问题是什么?
  • 我的问题是我不知道该怎么做..

标签: javascript node.js


【解决方案1】:

除非冷却在崩溃中幸存下来非常重要,或者冷却时间很长,否则我建议创建一个对象,其键可以是通道或用户或服务器 ID,具体取决于您希望应用速率限制的内容到。然后每次运行命令时将其设置为当前时间。类似的东西

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

然后,每当有人运行命令时,只需确保当前时间比具有该索引的对象中的时间大一定数量以上。 如果您只想简单地阻止人们向命令发送垃圾邮件,则类似于此处 pvpcraft 中使用的命令可能更理想。 https://github.com/macdja38/pvpcraft/blob/master/middleware/rateLimits.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 2012-01-01
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多