interaction.guild.members.me.voice.channel gives null response?

Tried grabbing the voice channel ID from where the bot was at, and I keep getting null or it won't change when the bot joins a new vc.
const { SlashCommandBuilder } = require('discord.js');
const { getVoiceConnection } = require('@discordjs/voice')

module.exports = {
data: new SlashCommandBuilder()
.setName('leave')
.setDescription('Get the discord bot to leave the vc it is currently in <>_<>'),

async execute(interaction) {

let connection = getVoiceConnection(interaction.guild.id);
if (connection === undefined) {
await interaction.reply("I am not in a vc!");
return;
};

const channel = interaction.guild.members.me.voice.channel

await interaction.reply({content: `Successfully left ${channel}`});
return connection.destroy()
},
};
const { SlashCommandBuilder } = require('discord.js');
const { getVoiceConnection } = require('@discordjs/voice')

module.exports = {
data: new SlashCommandBuilder()
.setName('leave')
.setDescription('Get the discord bot to leave the vc it is currently in <>_<>'),

async execute(interaction) {

let connection = getVoiceConnection(interaction.guild.id);
if (connection === undefined) {
await interaction.reply("I am not in a vc!");
return;
};

const channel = interaction.guild.members.me.voice.channel

await interaction.reply({content: `Successfully left ${channel}`});
return connection.destroy()
},
};
12 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Box Bunny with Fan Noises
node version: v18.16.0 discordjs version: [email protected]
Danial
Danial2y ago
The code you put in the title does that
Box Bunny with Fan Noises
oh? I thought it grabbed the one I was in- sorry are you sure it doesn't grab the one I'm in? when I was testing it, it only grabs the one I'm in
Danial
Danial2y ago
It returns the channel the bot is in
d.js docs
d.js docs2y ago
property GuildMemberManager#me The client user as a GuildMember of this guild
Box Bunny with Fan Noises
Updated it to match the problem I was having with it
Danial
Danial2y ago
The bot is not in a voice channel then
Box Bunny with Fan Noises
it was when the leave command was run, and it should return the disconnect after the message is sent also when removing connection.destroy() it still has the same issue
Aman
Aman2y ago
Do you have the GuildVoiceStates intent?
Box Bunny with Fan Noises
no, should I? and would I add it as const client = new Client({ intents: [GatewayIntentBits.GuildVoiceStates]}); ?
Aman
Aman2y ago
yes
Want results from more Discord servers?
Add your server