#discord #discord.js
#Discord #discord.js
Вопрос:
Итак, недавно я делал музыкального бота для discord, но я хотел бы, чтобы мой бот покидал канал, если никто не находится в голосовом канале дольше 5 минут. На данный момент мой бот отключается только по команде stop
if (!queue) return message.reply("There is nothing playing.").catch(console.error);
if (!canModifyQueue(message.member)) return;
queue.songs = [];
queue.connection.dispatcher.end();
queue.textChannel.send(`${message.author} ⏹ stopped the music!`).catch(console.error);
}
};
Кто-нибудь может мне помочь с этим? Я был бы очень признателен.
Ответ №1:
Вы можете использовать voiceStateUpdate
событие, которое выдается, когда кто-то присоединяется к голосовому каналу или покидает его (среди прочего).
client.on('voiceStateUpdate', (oldState, newState) => {
// if nobody left the channel in question, return.
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
}, 300000); // (5 min in ms)
});