Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Discord.JS倒计时/计时器命令问题_Javascript_Discord.js - Fatal编程技术网

Javascript Discord.JS倒计时/计时器命令问题

Javascript Discord.JS倒计时/计时器命令问题,javascript,discord.js,Javascript,Discord.js,我现在正在处理一个命令,在使用一个命令(例如,?hello)后,它会将您放入一个集合中。如果您再次使用该命令,您将收到一条消息(例如,“请等待10秒,然后再使用该命令”)。但是,如果您等待10秒,您可以再次使用该命令 问题是,在10秒结束之前,每次我在10秒结束之前执行?hello,bot都会说“请等待10秒,然后再使用此命令”。这意味着即使我等待了,比方说8秒,并且使用了该命令,bot仍会以“请等待10秒后再使用此命令。”而不是“请等待2秒后再使用此命令。” 我目前正在为此使用setTimeo

我现在正在处理一个命令,在使用一个命令(例如,
?hello
)后,它会将您放入一个集合中。如果您再次使用该命令,您将收到一条消息(例如,“请等待10秒,然后再使用该命令”)。但是,如果您等待10秒,您可以再次使用该命令

问题是,在10秒结束之前,每次我在10秒结束之前执行
?hello
,bot都会说“请等待10秒,然后再使用此命令”。这意味着即使我等待了,比方说8秒,并且使用了该命令,bot仍会以“请等待10秒后再使用此命令。”而不是“请等待2秒后再使用此命令。”

我目前正在为此使用
setTimeout()
,但是bot没有倒计时。我已经尝试使用
setInterval()
for()
循环,但我不知道如何使用这两个循环来实现这一点

我的基本代码正确吗?hello命令

const helloSet = new Set();

bot.on('message', msg => {
  let args = msg.content.substring(prefix.length).split(" ");
  if(args[0] === 'hello'){
    if(helloSet.has(msg.author.id)){
      msg.channel.send(`Please wait 10 seconds before using this command again.`)
      setTimeout(() => {
        helloSet.delete(msg.author.id)
      }, 10000)
    } else {
      msg.channel.send(`Hello!`)
      helloSet.add(msg.author.id)
    }
  }
})
这里没有计时器/倒计时功能,所以当我使用
?hello
命令时会发生这种情况

User: ?hello
Bot: Hello!

User right after using ?hello: ?hello
Bot: Please wait 10 seconds before using this command again

User 8 seconds later: ?hello
Bot: Please wait 10 seconds before using this command again
*Even after I wait for 8 seconds, the bot still says wait 10 more seconds although I really only need to wait 2 more seconds.*

User after a total 10 seconds later: ?hello
Bot: Hello!
同样,我也不太确定如何使用类似于
setInterval()
的方法来实现
for()
循环,因为我已经尝试过了,但没有成功


如果您能为我提供帮助,我将不胜感激。非常感谢!

这是因为您确实告诉机器人说10秒,您需要设置一个变量:

if(helloSet.has(message.author.id)){
const expirationTime=helloSet.get(message.author.id)+10000;//10000是冷却时间
如果(现在<到期时间){
const timeLeft=(expirationTime-now)/1000;
return message.reply(`please wait${timeLeft.toFixed(1)}秒,然后再次使用此命令`);
}
}
但是,无论哪种方式,计时器都不会过期,因为您需要在将用户添加到
集合()后立即运行
setTimeout()
函数:

}其他{
msg.channel.send(`Hello!`)
helloSet.add(msg.author.id)
设置超时(()=>{
helloSet.delete(msg.author.id)
}, 10000)
}

这是因为您确实告诉机器人说10秒,您需要设置一个变量:

if(helloSet.has(message.author.id)){
const expirationTime=helloSet.get(message.author.id)+10000;//10000是冷却时间
如果(现在<到期时间){
const timeLeft=(expirationTime-now)/1000;
return message.reply(`please wait${timeLeft.toFixed(1)}秒,然后再次使用此命令`);
}
}
但是,无论哪种方式,计时器都不会过期,因为您需要在将用户添加到
集合()后立即运行
setTimeout()
函数:

}其他{
msg.channel.send(`Hello!`)
helloSet.add(msg.author.id)
设置超时(()=>{
helloSet.delete(msg.author.id)
}, 10000)
}

根据您的描述,机器人似乎正在正确地倒计时,但它只是没有正确地输出剩余时间。 这是因为当有人调用帮助功能时,您打印的消息是静态的(
请等待10秒,然后再使用此命令。

setTimeout没有检索执行前剩余时间的方法,因此您必须手动记录第一次调用命令的时间,并在以后的调用中计算剩余时间,将其作为超时和已过部分之间的差值

声明用于跟踪时间的变量:

var callTime=0;

调用setTimeout时,请记下当前日期/时间:

callTime=(新日期()).getTime();

最后,使用以下命令更改冷却时打印的消息:


请等待${10-((new Date()).getTime()-callTime)}秒后再使用此命令。

根据您的描述,机器人似乎正在正确倒计时,但它只是没有正确输出剩余时间。 这是因为当有人调用帮助功能时,您打印的消息是静态的(
请等待10秒,然后再使用此命令。

setTimeout没有检索执行前剩余时间的方法,因此您必须手动记录第一次调用命令的时间,并在以后的调用中计算剩余时间,将其作为超时和已过部分之间的差值

声明用于跟踪时间的变量:

var callTime=0;

调用setTimeout时,请记下当前日期/时间:

callTime=(新日期()).getTime();

最后,使用以下命令更改冷却时打印的消息:

请等待${10-((new Date()).getTime()-callTime)}秒后再使用此命令。