【问题标题】:set the waiting time for a command to all users: setTimeout为所有用户设置命令的等待时间:setTimeout
【发布时间】:2020-05-30 14:38:50
【问题描述】:

我为 !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)");
});

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您可以使用变量来存储命令是否可用,然后您可以像这样编辑它:

    // 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2021-08-26
      • 2016-01-29
      • 1970-01-01
      • 2016-07-07
      相关资源
      最近更新 更多