0
votes

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))
}
2

2 Answers

0
votes

Um I don't know if it's already an array of not, if it is then

channel.members.length == 1
0
votes

I found the answer. Here's my code:

const fs = require('fs');

module.exports.run = async (bot, message, args) => {
let found = 0;
let channel = message.member.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(" has started this song: " + ` ${randomfile}`).then(sent => {
            sentmessage = sent;
            //console.log(id);
          }).catch(err => console.error(err))
            //console.log("Playing music");
        })
        dispatcher.on('error', (err) => console.log(err));

        const voicechannel = message.member.voice.channel; //saving the voicechannel to an array

        for (const [memberID, member] of voicechannel.members) { //look for member in the voicechannel array
          if(member.user) found++; //member gives giuld.user, when found, found++

          if(found < 1) return channel.leave();

          //console.log("when bot joins:" + found);
        }

        bot.on("voiceStateUpdate", (oldState, newState) => {
            let oldVoice = oldState.channelID; 
            let newVoice = newState.channelID;
            if (oldVoice != newVoice) {
                if (oldVoice == null) {
                    //console.log("before join:" + found);
                    found++;
                    //console.log("after join:" + found);
                    //console.log("User joined!");
                }
                if (newVoice == null) {
                    //console.log("before leaving:" + found); 
                    found--;
                    //console.log("after leaving:" + found);
                    if(found == 1) return channel.leave();
                  }
             }
        })

        dispatcher.on('finish', finish => {
            message.reply(" song ended! - " + ` ${randomfile}`)
            //console.log("Playing end");
            channel.leave()
        })
    }).catch(err => console.error(err))
  }
};

module.exports.help = {
    name: "play"
  };