Discord bot not joining the voice channel i'm in

1.1k Views Asked by At

I'm trying to code my own music bot but it is not joining the voice channel where I'm in. Everything else is working fine. I think that I should install a specific thing using npm but I don't know if that's right or not, i'm not sure so can somebody please tell me what should I do to fix this problem...

Here's My Code :

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

    let args = message.content.substring(prefix.length).split(" ");

    switch (args[0]) {
        case 'play':
            
            function play(connection, message) {
                var server = servers[message.guild.id];

                server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audioonly"}));

                server.queue.shift();

                server.dispatcher.on("end", function() {
                    if(server.queue[0]) {
                        play(connection, message);
                    } else {
                        connection.disconnect();
                    }
                });


            }
            
            
            if(!args[1]) {
                message.channel.send("You need to provide a link!");
                return;
            }

            if(!message.member.voiceChannel) {
                message.channel.send("You must be in a voice channel to use this command!");
                return;
            }

            if(!servers[message.guild.id]) servers[message.guild.id] = {
                queue: []
            }

            var server = servers[message.guild.id];

            server.queue.push(args[1]);

            if(!message.guild.voiceConnection) message.member.voice.channel.join().then(function(connection) {
                play(connection, message);
            })

        break;
    }

});
2

There are 2 best solutions below

5
On

I think you are using the discord.js version 12+, in this version the voiceConnection property is removed from the GuildMember class.

You should try the following -

message.member.voice.channel.join()
0
On

I think that the line code where you define args is typo, You're trying to define 'args' with a 'substring' method but this is a mistake, try change it to 'slice' , that should fix your problem. Unless you tried other commands and they worked.

Try change it to that line of code:

let args = message.content.slice(prefix.length).split(" ");