【问题标题】:Disabling Waiting Time in Some Parts: setTimeout在某些部分禁用等待时间:setTimeout
【发布时间】:2020-09-14 11:02:27
【问题描述】:

我为 !corona 命令设置了一个等待时间,它可以正常工作,但我想在用户错误地输入国家/地区缩写时删除等待时间。我所有的代码如下。

简而言之,当输入了错误的国家/地区命令时,例如“!corona US”应该无需等待时间即可查询新的国家/地区命令。

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

exports.run = async (bot, message, args) => {
    if (waitsetTimeOut.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)");
});

【问题讨论】:

  • 您能否提供更具体的信息来说明您希望应用程序做什么?
  • @Puk 实际上这工作正常,但两个人可以同时启动此命令。输入命令后,我希望每个人都有一个等待期。这只会给使用它的成员一个等待时间

标签: javascript node.js discord discord.js node-modules


【解决方案1】:

你可以把你的超时代码变成一个可重用的函数:

function startTimeout(authorId) {
    hereTimeOut.add(authorId);
    setTimeout(() => {
        hereTimeOut.delete(authorId);
    }, 60000);
}

然后在您希望用户在使用命令之前必须等待时调用它:

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});
            startTimeout(message.author.id) // Add this after every successfull run
        });

【讨论】:

  • 实际上这很好用,但是两个人可以同时启动这个命令。输入命令后,我希望每个人都有一个等待期。这只会给使用它的成员一个等待时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多