How can I get a voice connection object from an interaction object?

Pretty straight forward question in the title,

I set up my Bot as per the structure in the docs with the separate 'event', 'command' folders, I have created a new command called join which makes the bot join the channel, and also houses the "joinVoiceChannel" object. But in a separate command of 'play' I'm not sure how I can get the 'voice connection' object in it so that I can create an audio stream?

const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');
const { guildId } = require('../config.json');

module.exports = {
    data: new SlashCommandBuilder()
        .setName("join")
        .setDescription("Joins an audio channel"),
    async execute(interaction) {
        joinVoiceChannel({
            channelId: "",
            guildId,
            adapterCreator: interaction.channel.guild.voiceAdapterCreator,
        });

        await interaction.reply("Playing!");
    },
}


const { SlashCommandBuilder } = require('discord.js');
const { createAudioPlayer } = require('@discordjs/voice');

module.exports = {
    data: new SlashCommandBuilder()
        .setName("play")
        .setDescription(""),
    async execute(interaction) {
        const player = createAudioPlayer();
        
        // >>>> How to get the channel voice connection?


        await interaction.reply("Playing!");
    },
}
Was this page helpful?