我又回来了。我有一个关于Discord.js问题的问题。因此,我想检查一个用户在使用同一命令时是否加入了VoiceChannel。然后,当他确实加入语音频道时,我想阻止该听众收听该事件。
好的,我有这个小命令集,可以检查用户是否在语音通道中。
if (!message.member.voiceChannel) {
// Request to join voiceChannel.
}
此后,我收到一条消息,提示“请加入语音通道”或其他无关紧要的消息。但是,重要的是我想听的事件,这与voiceStateUpdate
相似。我想等到事件触发(因此,当用户加入语音通道时),然后再执行其余代码。
await Client.on("voiceStateUpdate", function (Old, member) {
// Do stuff, event got fired.
});
最后,我想知道这是否正确。等待某人加入频道,然后继续进行下去。
谢谢。 〜Q
答案 0 :(得分:0)
正如bambam在他的评论中提到的那样,您不能等待回调...实际上,这就是回调的全部要点-在完成大型操作时保持主线程自由/运行。
我不知道Discord库的复杂性,因此Client.on(voiceStatusUpdate)
函数可能会有一个promise版本...但是仅从原始JS角度来看,您可以将现有代码包装在一个承诺,并await
完成承诺:
await new Promise( (resolve, reject) => {
Client.on("voiceStateUpdate", function (Old, member) {
// Do stuff, event got fired.
// After you've finished doing your stuff here, tell the promise you're finished.
// Then, since the promise has "resolved", the code will continue past the "await"d line
resolve()
});
})
**请注意,您必须使用async
关键字标记父函数,才能在其中使用await
……但是,如果您搞砸了异步/等等,我想您可能已经熟悉该要求。