'How to make my bot leave a voice channel if there is no one in the channel for longer than 5 minutes
So recently I have been making a Music bot for discord, but I would like to have my bot leave a channel if no one is in the voice channel for longer than 5 minutes. For now my bot disconnects by the stop command only
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);
}
};
Can someone help me with this? I would greatly appreciate it.
Solution 1:[1]
You can use the voiceStateUpdate event, which emits when somebody joins or leaves a voice channel (among other things).
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)
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lioness100 |
