Command Timeout | Discord.js

13,341

You'll need to store the last date that the command was used and then fork the flow accordingly. To also show who last used the command 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 command ignore message

        var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array

        switch (args[0].toLowerCase()) { //not case-sensitive anymore

            case "hello":
                hello(message);
                break;

             //rest of the commands
  }}})
})

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 commands 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 command ignore message

        var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array
        const command = args[0].toLowerCase();

        if (!commands[command]) {
          throw new Error(`Unknown command supplied: ${command}`);
        }
        commands[command](message);
  }}})
})

const commands = {
  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}`);
    }
  }
};
Share:
13,341
qtt qtt
Author by

qtt qtt

Updated on June 04, 2022

Comments

  • qtt qtt
    qtt qtt about 2 years

    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 command ignore message
    
            var args = message.content.substring(PREFIX.length).split(" "); //splits commands so each word = pos in array
    
            switch (args[0].toLowerCase()) { //not case-sensitive anymore
    
                case "hello":
                    message.channel.send("hello");
                    break;
    
                 //rest of the commands
    

    I would like to limit the usage of the command ",hello". I want there to be a 10 second timeout for every-time a user enters ",hello". And if a user enters the command before this cooldown it will send a message saying who used the command last and how long remaining on the cooldown.

    This is what I want the outcome to look like:

    User1:          ,hello
    Bot:             hello
    
    (After 1 second)
    
    User2:          ,hello
    Bot:            User1 has already used this command, please wait another 9 seconds to use it again
    
    (After 9 seconds)
    
    User 2:         ,hello
    Bot:            hello
    

    All help is appreciated. Thanks,

  • Chance Hudson
    Chance Hudson over 6 years
    Yes, lastHelloCommandDate should be initialized to 0. If it's undefined (as written above) the result is new Date() - undefined which results in NaN (which is not greater than any number).