Skip to content
Advertisement

How do I check if a discord.js bot is inside the same voice channel as the member? [closed]

The problem:
I can’t check if the dscord.js bot that I programmed is in the same voice channel as a member that is trying to command it. I need a function like: if (client.member.voice.channel==true). And then if the “IF” statement is true the bot will do the command, and if it is not the bot will apologize and say that you need to be in the same channel as the bot.

The code I am using:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => res.send('Music Bot'));



app.listen(port, () => console.log(`Web is ready!`));



const Discord = require('discord.js');


const ytdl = require('ytdl-core');

const ytSearch = require('yt-search');

const ffmpeg = require('ffmpeg-static');

const client = new Discord.Client()
//=========================================


client.once( 'ready', () => {


console.log('Bot is ready!')

client.user.setActivity(`Use +play or +leave!`, { type: "LISTENING" }).catch(console.error)

});



client.on('message', async msg => {


const args = msg.content.trim().split(/ +/g);



if(msg.content.startsWith('+play'))
{
const voiceChannel = msg.member.voice.channel;

let videoName = msg.content.slice(5).trim()
 
        if (!voiceChannel) return msg.channel.send('You need to be in a channel to play music!');
        const permissions = voiceChannel.permissionsFor(msg.client.user);
        
        if (!videoName) return msg.channel.send('You need to type the song name!');
 
        const validURL = (str) =>{
            var regex = /(http|https)://(w+:{0,1}w*)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%!-/]))?/;
            if(!regex.test(str)){
                return false;
            } else {
                return true;
            }
        }
 
        if(validURL(args[0])){
 
            const  connection = await voiceChannel.join();
            const stream  = ytdl(args[0], {filter: 'audioonly'});
 
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
                voiceChannel.leave();
                msg.channel.send('Leaving channe!');
            });
 
            await msg.reply(`Now Playing ***Your Link!***`)
 
            return
        }
 
        
        const  connection = await voiceChannel.join();
 
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);
 
            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
 
        }
 
        const video = await videoFinder(args.join(' '));
 
        if(video){
            const stream  = ytdl(video.url, {filter: 'audioonly'});
            connection.play(stream, {seek: 0, volume: 1})
            .on('finish', () =>{
                voiceChannel.leave();
            });
 
            await msg.reply(`Now Playing ***${video.title}***`)
        } else {
            msg.channel.send('No results found');
        }

}

if(msg.content.startsWith('+leave'))
{

 const voiceChannel = msg.member.voice.channel;
 
        if(!voiceChannel) return msg.channel.send("You need to be in a voice channel to stop the music!");
        await voiceChannel.leave();
        await msg.channel.send(`Leaving **${voiceChannel.name}**!`)




}









});


client.login(process.env.Token)

What I need:

A function that checks if the bot is already in another voice channel, works similar to the code above, in “The problem”.

THANKS

Advertisement

Answer

The client has a voice property that returns a ClientVoiceManager. voice.connections returns a collection of VoiceConnections, so it has a size property.

It means, you can check if client.voice.connections.size is greater than 0. If it is, the bot is in a voice channel.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement