Search code examples
javascriptnode.jsdiscorddiscord.js

Join an specific voice channel when the bot is ready in discord.js v13


I found some articles about this question. like this.

But it was in discord v12. I want discord.js v13

My code:

client.on("ready", async() => {
const { joinVoiceChannel } = require('@discordjs/voice');
joinVoiceChannel({
            channelId: "863783336860975114",
            guildId: "847071265100791849",
            adapterCreator: channelId.guild.voiceAdapterCreator
        })

// It does not work
}

How can I make my bot that it joins a specific channel when it's ready?

I am using discord.js v13 and node.js v16.15


Solution

  • In the documentation for @discordjs/voice an in the discord.js guide we see:

    const { joinVoiceChannel } = require('@discordjs/voice');
    
    const connection = joinVoiceChannel({
        channelId: channel.id,
        guildId: channel.guild.id,
        adapterCreator: channel.guild.voiceAdapterCreator,
    });
    

    You need to fetch the guild or channel first since you need to use a voiceAdapterCreator.

    You can use something like client.channels.fetch to get the channel object.

    Example code to put inside ready event:

    client.channels.fetch(id) // voice channel's id
        .then((channel) => { // channel object
    
            const VoiceConnection = joinVoiceChannel({
                channelId: channel.id, // the voice channel's id
                guildId: channel.guild.id, // the guild that the channel is in
                adapterCreator: channel.guild.voiceAdapterCreator // and setting the voice adapter creator
            });
        });