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");
}
2
  • you didnt mention what is your problem? Commented Jul 4, 2016 at 19:26
  • My problem is is that I have no idea what to do.. Commented Jul 5, 2016 at 7:14

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.