如果频道中没有人,如何让我的机器人离开语音频道



我最近创建了一个播放广播的discord机器人,我想知道如何确保当没有人引导它时,机器人会引导它。


if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel)
return;
// otherwise, check how many people are in the channel now
if (!oldState.channel.members.size === 1) 
setTimeout(() => { // if 1 (you), wait five minutes
if (!oldState.channel.members.size === 1) // if there's still 1 member, 
oldState.channel.leave(); // leave
}, 10);
});

我测试了这个,但它不起作用,有人能帮我吗?thx!

试试这个:

client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel){
return(0); //If it's not the channel the bot's playing in
}
if((oldState.channel.members.size -1) <= 1){
//The bot's still in channel so it's never be zero
//So we'll use <=1
oldState.channel.leave();  //Leave the channel
}
});

如果您想在一段时间后触发setTimeout
可选:

client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelID !==  oldState.guild.me.voice.channelID || newState.channel){
return(0); //If it's not the channel the bot's playing in
}
if(!oldState.channel.members.size -2){
//The bot's still in channel so it's never be zero
//We'll use -2 this time
oldState.channel.leave();  //Leave the channel
}
});

评论应该是不言自明的

相关内容

最新更新