Reputation: 23
I've made a discord bot, here the code
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("ready", () => {
console.log("I am ready!");
});
client.on("message", (message) => {
if (message.content.startsWith("!kevin")) {
message.channel.send("i'm kevin");
}
if (message.content.startsWith("!thomas")) {
message.channel.send("random text blabla");
}
Basically, when I type !something, my bot answer in the chat by the proper line, my current issue is my discord is kinda big nowadays, and I would like to restrict the bot to only X messages per minutes, but I can't find an easy function to do that
my question :
Is it possible to get a timer between 2 messages to send by the bot, because without that my bot is just spamming answer to each users typing !somethingsomething
, I would like my bot to as soon as someone type !something
, the bot lock itself of replying to any other !something
for X amount of time
Example,
User 1 : !thomas
User 2 : !thomas
User 3 : !thomas
But the bot only reply to one of them and put a timer to himself before being able to send a new !message
reply
So basically, is it any way to make the bot reply to X amounts of !cmd each minutes, or to limits the cooldown between 2 messages the bot send
here my script : https://i.sstatic.net/6b8W5.jpg ( i know its terrible especially since it have over 9000 differents !cmd , but i converted quickly a old MIRC script and im terrible at javascript)
Upvotes: 2
Views: 2754
Reputation: 711
So I found out from your comments that you want the bot to only run a command only every "X" amount of time. I don't recommend this, I recommend just preventing USERS from registering a cmd every "X" amount of time. I've included both in this example.
let lastCmdSentTime = {};
let waitTimeForUser = 60000 * 5; //Users can only run a command once every 5 minutes
let botLastSent = false;
let timeBetweenEachCmd = 60000; //Bot will only respond once a minute.
client.on("message", (message) => {
if(botLastSent !== false ? message.createdTimestamp - botLastSent < timeBetweenEachCmd : false) return; //don't let the bot run a cmd every [timeBetweenEachCmd]
let userLastSent = lastCmdSentTime[message.author.id] || false;
if(userLastSent !== false ? message.createdTimestamp - userLastSent < waitTimeForUser : false) return; //don't let the user run a cmd every [waitTimeForUser]
lastCmdSentTime[message.author.id] = message.createdTimestamp;
botLastSent = message.createdTimestamp;
//RUN COMMANDS
});
Upvotes: 1
Reputation: 711
Simply store the date the command was ran, then check if a certain amount has passed since the date was set.
Example:
Edited to support multiple commands
let date = false;
let commandsTimers = {
"!kevin":{
waitTime: 5 * 60000, // 5 minutes wait for this particular command.
lastUsed: false,
}
}
let defaultWaitTime = 60000 * 2; //User needs to wait 2 minutes for each command unless specified
client.on("message", (message) => {
let msgSentDate = Date.now();
let commandWaitTimer = commandsTimers[message.content.split(" ")[0]] || {waitTime:defaultWaitTime, lastUsed:false};
if((commandWaitTimer.lastUsed !== false ? msgSentDate - commandWaitTimer.lastUsed < commandWaitTimer.waitTime : false)){
console.log('User needs to wait: ' + (commandWaitTimer.waitTime - (msgSentDate - commandWaitTimer .lastUsed)) / 1000 + ' seconds');
return
}
commandsTimers[message.content.split(" ")[0]].lastUsed = msgSentDate;
if (message.content.startsWith("!thomas")) {
message.channel.send("random text blabla");
}
if (message.content.startsWith("!kevin")) {
message.channel.send("Kevin!");
}
});
Upvotes: 0