I'm making a music bot, when the member writes "~play" the bot search a random file (.mp3) in a folder, and joines the voice channel where the user currently in. I want my bot to leave the voice channel when all the users leave the voicechannel.
const fs = require('fs');
module.exports.run = async (bot, message, args) => {
let channel = message.member.voice.channel;
if(!channel) return message.reply("You're not connected to a voice channel!");
if (channel) {
channel.join()
.then(connection => {
const commandFiles = fs.readdirSync('./commands/Workout/').filter(file => file.endsWith('.mp3'));
let randomfile = commandFiles[Math.floor(Math.random() * commandFiles.length)]
const dispatcher = connection.play(`./commands/Workout/${randomfile}`);
dispatcher.on('start', () => {
dispatcher.setVolume(0.70);
message.reply(" started this song: " + ` ${randomfile}`)
}).catch(err => console.error(err))
//console.log("Playing music");
})
dispatcher.on('error', (err) => console.log(err));
if(channel.members == 1){ //this is the problem
channel.leave()
}
dispatcher.on('finish', finish => {
message.reply(" Song ended! - " + ` ${randomfile}`)
//console.log("Playing ended");
channel.leave()
})
}).catch(err => console.error(err))
}