在某些部分禁用等待时间:setTimeout
Posted
技术标签:
【中文标题】在某些部分禁用等待时间:setTimeout【英文标题】:Disabling Waiting Time in Some Parts: 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 实际上这工作正常,但两个人可以同时启动此命令。输入命令后,我希望每个人都有一个等待期。这只会给使用它的成员一个等待时间 【参考方案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
);
【讨论】:
实际上这很好用,但是两个人可以同时启动这个命令。输入命令后,我希望每个人都有一个等待期。这只会给使用它的成员一个等待时间以上是关于在某些部分禁用等待时间:setTimeout的主要内容,如果未能解决你的问题,请参考以下文章
为某些路由禁用 vue-router?或者如何运行 SPA 后端和非 SPA 前端?