Search code examples
javascriptaudio-player

Connection.play is not a function


I just try to create discord bot in javascript, and I want, when someone join the voice chat, bot will join. But when I try it, bot will join, but I will get an error. I started this day, and it's just for fun so. If anybody has something, please tell me, I'm a beginner.

const Discord = require('discord.js');
const client = new Discord.Client();

const prefix = 'more '

client.once('ready', () => {
    console.log('Hello ')
});



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

    //if(!message.content.startsWith(prefix) || message.author.bot) return;

    if (message.content === '%play') {
        // Join the same voice channel of the author of the message
        if (message.member.voice.channel) {
            const connection = await message.member.voice.channel.join();
            // Play audio, see below
            const dispatcher = connection.play('sound.mp3');
            dispatcher.on('finish', () => message.member.voice.channel.leave());
        }};
    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'play'){
        message.channel.send('%play');
        return;
    }



});


client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => { // Listeing to the voiceStateUpdate event
    if (newVoiceState.channel) { // The member connected to a channel.
        const connection = newVoiceState.channel.join();
            // Play audio, see below
            const dispatcher = connection.play('sound.mp3');
            dispatcher.on('finish', () => newVoiceState.channel.leave());
    } else if (oldVoiceState.channel) { // The member disconnected from a channel.
        console.log(`${oldVoiceState.member.user.tag} disconnected from ${oldVoiceState.channel.name}.`)
    };
});

client.login('NjI2ODQ0NDIzOTY3MzQyNjAz.XY0AXA.WEghPj9EZkLFPblsORUhVgvNkYI');

And here is error:

TypeError: newVoiceState.channel.play is not a function
    at Client.client.on (C:\Users\Admin\Desktop\DiscordBot\main.js:41:54)
    at Client.emit (events.js:198:13)
    at VoiceStateUpdate.handle (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\VoiceStateUpdate.js:40:14)
    at Object.module.exports [as VOICE_STATE_UPDATE] (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\VOICE_STATE_UPDATE.js:4:35)
    at WebSocketManager.handlePacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (C:\Users\Admin\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:825:20)

Thank you for some help.


Solution

  • You need to add the await keyword in this code part: const connection = newVoiceState.channel.join();, it will turn into const connection = await newVoiceState.channel.join();. This should work! Another way you can do this is use .then on the join function since it returns a promise. Read more about it here: https://discord.js.org/#/docs/main/stable/class/VoiceChannel?scrollTo=join