How to play specific audio file in voice channel?

Viewed 94

I'm pretty new to javascript and discord.js, does anyone know how to make a bot join a channel, play a file and leave?

Here's what I've tried:

    if(msg.content.startsWith(prefix + "vc")){
        var voiceChannel = msg.member.voiceChannel; 
        isReady = false;
        if (!voiceChannel)
            return msg.channel.send("Join a VC.")
        else voiceChannel.join().then(connection => {
            const dispatcher = connection.playFile('./audio/audio.mp3');
            dispatcher.on("end", end => voiceChannel.leave());
        }).catch(err => console.log(err));
    }
})

Right now, it will send the message to join a voice channel regardless of whether I am in one or not, and if I am it doesn't join the voice channel and play the file. Any help is appreciated.

1 Answers

Since discord.js v12, GuildMember.voiceChannel was changed to GuildMember.voice.channel

var voiceChannel = msg.member.voice.channel
Related