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 { 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!");
},
}
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!");
},
}
4 Replies
Unknown User
Unknown User15mo ago
Message Not Public
Sign In & Join Server To View
ShompiFlen
ShompiFlen15mo ago
discordjs/voice has a getVoiceConnection method that you can use
d.js docs
d.js docs15mo ago
method getVoiceConnection Finds a voice connection with the given guild id and group. Defaults to the 'default' group.
(DMG) Cantcrit
(DMG) Cantcrit15mo ago
Thanks I’m dumb and didn’t realize I could change the docs at the top to voice, I was searching all through the default discordjs docs