javascript - setTimeout for client using command -


so i'm trying create "cooldown" system discord bot, i've tried using database method, here doesn't work me.

i'm using discord.js package.

here 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 name.```");     return;   }   if (usr == msg.author.username) {     bot.sendmessage(msg, "```error: 3\n reason: you're unable 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 + " has " + db["users"][usr]);   fs.writefilesync('database.json', json.stringify(db));   bot.sendmessage(msg, usr + " has received 1 point"); } 

unless it's important cooldown survives crash, or cool down long duration recomend creating object keys are either channel or user or server id's depending on want rate limit apply to. every time command run set current time. like

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

then whenever runs command ensure current time greater time in object index more amount. if looking stop people spamming command similar what's used in pvpcraft here may more ideal. https://github.com/macdja38/pvpcraft/blob/master/middleware/ratelimits.js


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -