0

So what I'm trying to do is create a "cooldown" like system for my discord bot, I've tried using a database method, here which doesn't work for me.

I'm using the discord.js package.

Here is my code:

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");
}
Community
  • 1
  • 1
swagster
  • 165
  • 2
  • 10

1 Answers1

0

Unless it's very important that the cooldown survives a crash, or the cool down is a very long duration I recomend creating an object with keys that are are either channel or user or server id's depending on what you want the rate limit to apply to. Then every time a command is run set that to the current time. Something like

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

Then whenever someone runs a command just ensure that the current time is greater than the time in the object with that index by more than a certain amount. If you are looking to just simply stop people from spamming the command something similar to what's used in pvpcraft here may be more ideal. https://github.com/macdja38/pvpcraft/blob/master/middleware/rateLimits.js

macdja38
  • 493
  • 3
  • 16