Discord JS在特定时间加入语音频道



我知道我的代码看起来像sh*t,但我是新的im nodejs,我不知道为什么我的代码不能正常工作(机器人加入语音频道,理论上正在播放音乐,但实际上没有)代码:

while (true)
{
d = new Date();
currtime = d.getHours() * 100 + d.getMinutes();
console.log(currtime);
if (currtime == 1912 && loop == true)
{
loop = false;
console.log("21:37");
const connection =   await client.channels.cache.get(VOICECHANNEL).join(); 
connection.play(LINK, { volume: VOLUME }); // link = url (mp3)
setTimeout(() => {  client.channels.cache.get(VOICECHANNEL).leave();},204000);  // 204000 = length of the song
}
else 
{
sleep(204000);
loop = true;
}
}

我不完全确定您想要实现什么,但我可以看到一些问题。

首先,您没有在等待sleep()函数,因此您的循环将以尽可能快的速度一次又一次地执行。

其次,在您等待sleep()之后,它当前设置为睡眠204秒,但您正在检查curtime是否正好是目标时间,而不是大于。您需要减少睡眠时间,或更改if语句(或两者都更改)。

我不能调试你的discord代码,因为我不知道语法,但这至少应该让你开始。

const targetTime = 1326;
while (true) {
d = new Date();
currtime = d.getHours() * 100 + d.getMinutes();
console.log(currtime);
if (currtime >= targetTime && currtime < targetTime + 1) {
//const connection = await client.channels.cache.get(VOICECHANNEL).join();
console.log('connected');
//connection.play(LINK, { volume: VOLUME }); // link = url (mp3)
//setTimeout(() => { client.channels.cache.get(VOICECHANNEL).leave(); }, 204000);  // 204000 = length of the song
break;
}
else {
console.log('sleep')
await sleep(60000);
loop = true;
}
}

同样值得研究的是:https://www.npmjs.com/package/node-schedule

这是为安排任务而优化的。

最新更新