Striker (Ethan)
Striker (Ethan)
DIAdiscord.js - Imagine an app
Created by Striker (Ethan) on 8/24/2023 in #djs-voice
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)
7 replies