简体   繁体   English

如何获取语音通道中的用户数?

[英]How can I get the number of users in a voice channel?

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.我正在制作一个音乐机器人,当成员写“〜播放”时,机器人会在文件夹中搜索一个随机文件(.mp3),并加入用户当前所在的语音频道。我希望我的机器人离开语音频道当所有用户离开语音通道时。

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

Um I don't know if it's already an array of not, if it is then嗯我不知道它是否已经是一个不是的数组,如果是那么

channel.members.length == 1

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"
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM