所以我做了一个机器人,其中一个命令要求它等待。当其他人正在使用该命令时,我该如何让另一个用户使用该命令,而不要等到另一个人完成之后?
if(command ==='rm'){
var hithere2 = message.content
var ret = hithere2.replace('-rm ','');
var result = ret.slice('&').split(',');
let remindercontent = result[0]
let remindertime = result[1]
if(!isNaN(remindertime)){
if(remindertime % 1 == 0){
message.channel.send({embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: "Reminder set for " + remindercontent,
description: 'It will go off in ' + remindertime + ' seconds',
}})
let sleeptime = remindertime * 1000
sleep(sleeptime);
message.channel.send('<@' + message.author.id + '>')
message.channel.send({embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: `BEEEP`,
description: `${remindertime} seconds ago you wanted to remind yourself to **${remindercontent}**`,
}})
}
}else {
message.channel.send('Please enter a valid number after the comma!');
}
上面是我需要同时运行的东西的命令。我已经安装了系统睡眠模块。
答案 0 :(得分:0)
您是否尝试过setTimeout函数?通常我会考虑使用这种不好的做法来使用sleep()函数,因为这会使线程停止工作。
类似的方法可能对您有用(没有尝试过,您可能需要将参数传递给该函数):
setTimeout( function() {
message.channel.send('<@' + message.author.id + '>')
message.channel.send({embed: {
color: Math.floor(Math.random() * 16777214) + 1,
title: `BEEEP`,
description: `${remindertime} seconds ago you wanted to remind yourself to
**${remindercontent}**`,
}})
}, sleeptime);
要显示平均值jj,下面是一个代码段示例:
var commentInXMilliseconds = (delay) => {
const text = "New time";
setTimeout( function() {
console.log(`${text} ${new Date()}`);
}, delay);
}
console.log(`Start time: ${new Date()}`);
commentInXMilliseconds(2000);