【问题标题】:How can I get the number of users in a voice channel?如何获取语音通道中的用户数?
【发布时间】:2020-09-16 04:18:55
【问题描述】:

我正在制作一个音乐机器人,当成员写“~play”时,机器人会在文件夹中搜索一个随机文件 (.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))
}

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    嗯我不知道它是否已经是一个非数组,如果是那么

    channel.members.length == 1
    

    【讨论】:

    • 当我尝试它时,我离开了语音频道,只有机器人在里面,它没有离开,所以它不适用于.members.length,我也没有收到任何错误,所以我无法判断问题所在。
    • 你不是在检查某人何时离开频道,要么为此添加一个听众,要么检查歌曲何时结束并且频道内只有一个人离开频道
    【解决方案2】:

    我找到了答案。这是我的代码:

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

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 2019-04-24
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2022-01-16
      • 2019-09-23
      相关资源
      最近更新 更多