Currently I have this:
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
var bot = new Discord.Client();
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not mand ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits mands so each word = pos in array
switch (args[0].toLowerCase()) { //not case-sensitive anymore
case "hello":
message.channel.send("hello");
break;
//rest of the mands
I would like to limit the usage of the mand ",hello". I want there to be a 10 second timeout for every-time a user enters ",hello". And if a user enters the mand before this cooldown it will send a message saying who used the mand last and how long remaining on the cooldown.
This is what I want the oute to look like:
User1: ,hello
Bot: hello
(After 1 second)
User2: ,hello
Bot: User1 has already used this mand, please wait another 9 seconds to use it again
(After 9 seconds)
User 2: ,hello
Bot: hello
All help is appreciated. Thanks,
Currently I have this:
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
var bot = new Discord.Client();
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not mand ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits mands so each word = pos in array
switch (args[0].toLowerCase()) { //not case-sensitive anymore
case "hello":
message.channel.send("hello");
break;
//rest of the mands
I would like to limit the usage of the mand ",hello". I want there to be a 10 second timeout for every-time a user enters ",hello". And if a user enters the mand before this cooldown it will send a message saying who used the mand last and how long remaining on the cooldown.
This is what I want the oute to look like:
User1: ,hello
Bot: hello
(After 1 second)
User2: ,hello
Bot: User1 has already used this mand, please wait another 9 seconds to use it again
(After 9 seconds)
User 2: ,hello
Bot: hello
All help is appreciated. Thanks,
Share Improve this question asked Dec 27, 2017 at 18:51 qtt qttqtt qtt 1873 gold badges6 silver badges19 bronze badges1 Answer
Reset to default 2You'll need to store the last date that the mand was used and then fork the flow accordingly. To also show who last used the mand you'll need to store that information with the timestamp.
Here's an example based on yours:
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
const bot = new Discord.Client();
let lastHelloCommandDate, lastHelloCommandUser;
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not mand ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits mands so each word = pos in array
switch (args[0].toLowerCase()) { //not case-sensitive anymore
case "hello":
hello(message);
break;
//rest of the mands
}}})
})
function hello(message) {
const now = new Date();
if (now - lastHelloCommandDate > 10 * 60 * 1000) {
// It's been more than 10 mins
message.channel.send("hello");
lastHelloCommandDate = now;
lastHelloCommandUser = message.sender;
} else {
// It's been less than 10 mins
// send a direct message to the user
// i don't know if message.sender exists, check the api
message.sender.send(`Command last used by ${lastHelloCommandUser}`);
}
}
This sample is reworked such that mands are stored in a single object and dynamically checked for. This removes the need for a switch statement.
const Discord = require("discord.js");
const PREFIX = ",";
const token = "my token";
const bot = new Discord.Client();
let lastHelloCommandDate, lastHelloCommandUser;
bot.on('ready', () => {
bot.on('message', message => {
if (!message.content.startsWith(PREFIX)) return; //if not mand ignore message
var args = message.content.substring(PREFIX.length).split(" "); //splits mands so each word = pos in array
const mand = args[0].toLowerCase();
if (!mands[mand]) {
throw new Error(`Unknown mand supplied: ${mand}`);
}
mands[mand](message);
}}})
})
const mands = {
hello: message => {
const now = new Date();
if (now - lastHelloCommandDate > 10 * 60 * 1000) {
// It's been more than 10 mins
message.channel.send("hello");
lastHelloCommandDate = now;
lastHelloCommandUser = message.sender;
} else {
// It's been less than 10 mins
// send a direct message to the user
// i don't know if message.sender exists, check the api
message.sender.send(`Command last used by ${lastHelloCommandUser}`);
}
}
};