Demon
Demon

Reputation: 13

Bot doesnt connect to voice channel

My bot doesn't connect to voice channel, before I was using old function, but I changed it for new and it still doesn't work. I tried to place it in If(video), but it didn't work.

Code:

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

module.exports = {
    name: 'play',
    description: '',
    async execute(message, args){
        const voiceChannel = message.member.voice.channel;
        const { joinVoiceChannel } = require('@discordjs/voice');

        if (!voiceChannel) return message.channel.send('You need to be in a voice channel');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have a permission to do that');
        if (!permissions.has('SPEAK')) return message.channel.send('You dont have a permission to do that');
        if (!args.length) return message.channel.send('You have to add a link');

        const connection = joinVoiceChannel({
            channelId: message.member.voice.channel.id,
            guildId: message.guild.id,
            adapterCreator: message.guild.voiceAdapterCreator
        });
            
        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 message.reply(`Now playing: ***${video.title}***`)
        } else {
            message.channel.send('No video redults found');
        }
    }
}

Upvotes: 0

Views: 117

Answers (1)

Maciej Januś
Maciej Januś

Reputation: 33

If you aren't getting any errors, I'd recommend checking if you have valid intents provided. I believe you need the 'GUILD_VOICE_STATES' intent.

Cheers!

Upvotes: 1

Related Questions