簡體   English   中英

如何獲取語音通道中的用戶數?

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

我正在制作一個音樂機器人,當成員寫“〜播放”時,機器人會在文件夾中搜索一個隨機文件(.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))
}

嗯我不知道它是否已經是一個不是的數組,如果是那么

channel.members.length == 1

我找到了答案。 這是我的代碼:

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