Joining the user's current voice channel

On [email protected] & node v18.12 I've been trying to create a command that joins whichever channel the user is in. I keep running into an issue where it doesn't read the user's current voice, instead it thinks "currVoiceChannel" is false.
// Todo: ejoin makes bot join user when called
// Todo: if already in call, reply to user-only, already in call

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

module.exports = {
data: new SlashCommandBuilder()
.setName('ejoin')
.setDescription('Bot will join your current voice.'),
async execute(interaction) {
// figure out current voice channel
const currVoiceChannel = interaction.member.voice.channel;

// if voicechannel is empty, reply join voice channel, end command
if (!currVoiceChannel) {
return interaction.reply('Please join a voice channel.');
} else {
console.log('Current VC: ', currVoiceChannel.id);
}

// try joining voice channel, or catch the error, end command
try {
const connection = joinVoiceChannel({
channelId: currVoiceChannel.id,
guildId: currVoiceChannel.guild.id,
adapterCreator: currVoiceChannel.guild.voiceAdapterCreator,
});

console.log(getVoiceConnections());
await interaction.reply(`Joined: ${currVoiceChannel.name}.`);

}
catch (error) {
console.log(error);
await interaction.reply('Could not join voice channel.');
}

// if called when already in a voice channel, reply already in call, end command

},
};
// Todo: ejoin makes bot join user when called
// Todo: if already in call, reply to user-only, already in call

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

module.exports = {
data: new SlashCommandBuilder()
.setName('ejoin')
.setDescription('Bot will join your current voice.'),
async execute(interaction) {
// figure out current voice channel
const currVoiceChannel = interaction.member.voice.channel;

// if voicechannel is empty, reply join voice channel, end command
if (!currVoiceChannel) {
return interaction.reply('Please join a voice channel.');
} else {
console.log('Current VC: ', currVoiceChannel.id);
}

// try joining voice channel, or catch the error, end command
try {
const connection = joinVoiceChannel({
channelId: currVoiceChannel.id,
guildId: currVoiceChannel.guild.id,
adapterCreator: currVoiceChannel.guild.voiceAdapterCreator,
});

console.log(getVoiceConnections());
await interaction.reply(`Joined: ${currVoiceChannel.name}.`);

}
catch (error) {
console.log(error);
await interaction.reply('Could not join voice channel.');
}

// if called when already in a voice channel, reply already in call, end command

},
};
Whenever I join a VC and call the bot it returns "Please join a voice channel". I've looked at multiple examples and they all use the If (!currVoiceChannel)
5 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
chewie
chewie16mo ago
Do you have the GuildVoiceStates intent?
Striker (Ethan)
Striker (Ethan)OP16mo ago
As in the permissions for the bot?
Magikaas
Magikaas16mo ago
The intent Discord.GatewayIntentBits.GuildVoiceStates
Striker (Ethan)
Striker (Ethan)OP16mo ago
I didn't have the intents updated, I only had .Guilds It works, you guys are geniuses
Want results from more Discord servers?
Add your server