установить время ожидания команды для всех пользователей: setTimeout - PullRequest
0 голосов
/ 30 мая 2020

Я установил время ожидания для команды! Corona, она работает нормально, но я хочу удалить время ожидания, когда пользователь неправильно вводит аббревиатуру страны. Все мои коды указаны ниже.

Короче говоря, когда введена неправильная команда страны, например "! Corona US" должна запрашивать новую без ожидания времени.

на самом деле это отлично работает но два человека могут запустить эту команду одновременно. как только команда введена, я хочу период ожидания для всех. это дает только время ожидания для участника, который его использует

const Discord = require("discord.js");
const fetch = require("node-fetch");
const hereTimeOut = new Set();

exports.run = async (bot, message, args) => {
    if (hereTimeOut.has(message.author.id)) {

        const waitsetTimeOut = new Discord.RichEmbed()
        waitsetTimeOut.setColor(0x00AE86)
        waitsetTimeOut.setTimestamp()
        waitsetTimeOut.setAuthor(message.author.username, message.author.avatarURL)
        waitsetTimeOut.setTitle("[wait a while]")
        waitsetTimeOut.setDescription('please wait 1 minute')
        return message.channel.sendEmbed(waitsetTimeOut);
    }else {


    let country = args.slice(0).join(' ');

    if(!country){

        fetch("https://covid19.mathdro.id/api/").then(res => res.json()).then(json => {

            const embed = new Discord.RichEmbed();
                embed.addField("**= Total Number of Cases =**",`**`+ json.confirmed['value'] +` person**`)
                embed.addField("**= Number of Healing Cases =**",`**`+ json.recovered['value'] +` person**`)
                embed.addField("**= Number of Cases Losing Life =**",`**`+ json.deaths['value'] +` person**`)
                embed.setColor(15962634)
                embed.setTitle('Worldwide COVID-19 Statistics')
            message.channel.send({embed: embed});

        });

    }else{

        fetch(`https://covid19.mathdro.id/api/countries/${country}`).then(res => res.json()).then(json => {

                const embed = new Discord.RichEmbed();
                    embed.addField("**= Total Number of Cases =**",`**`+ json.confirmed['value'] +` person**`)
                    embed.addField("**= Number of Healing Cases =**",`**`+ json.recovered['value'] +` person**`)
                    embed.addField("**= Number of Cases Losing Life =**",`**`+ json.deaths['value'] +` person**`)
                    embed.setColor(15962634)
                    embed.setTitle(`COVID-19 Statistics (${country})`)
                message.channel.send({embed: embed});

        }).catch(() => {

            message.reply("I couldn't find the country you are looking for, be careful not to use Turkish letters when writing the country. You can also write country abbreviations (ex: TR, USA, AZ)");

        });

    }

    hereTimeOut.add(message.author.id);
        setTimeout(() => {
          // Removes the user from the set after a minute
          hereTimeOut.delete(message.author.id);
        }, 60000);
    }

};

exports.conf = {
  enabled: true,
  guildOnly: true,
  aliases: ['corona'],
  permLevel: 0
};

exports.help = {
  name: "corona",
  description: "covid19",
  usage: "coronavirus"
};

часть ошибки, полученной при использовании неправильной команды страны;

.catch(() => {
    message.reply("I couldn't find the country you are looking for, be careful not to use Turkish letters when writing the country. You can also write country abbreviations (ex: TR, USA, AZ)");
});

1 Ответ

1 голос
/ 30 мая 2020

Вы можете использовать переменную, чтобы сохранить, доступна ли команда для использования или нет, затем вы можете отредактировать ее следующим образом:

// Setup
let isAvailable = true,
  cooldownMS = 5000; // in ms, this is 5 seconds

// When you're running the command:
if (isAvailable) {
  isAvailable = false
  setTimeout(() => {
    isAvailable = true
  }, cooldownMS)
  // Run your command ...
} else {
  // The command is still cooling down, send a message to the user to let them know
}
...