Creating a leave voice channel command for a bot

I have a working join command for discordjs v14, so now I'm trying to make a working disconnect command. code is here:
const { SlashCommandBuilder, ChannelType} = require('discord.js');
const { joinVoiceChannel, VoiceConnectionStatus, AudioPlayerStatus, VoiceConnection, getVoiceConnection, createAudioResource, createAudioPlayer} = 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) {

const channel = interaction.options.getChannel('channel');

const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});

connection.destroy();

await interaction.reply(`successfully left ${channel}!`);


},
};
const { SlashCommandBuilder, ChannelType} = require('discord.js');
const { joinVoiceChannel, VoiceConnectionStatus, AudioPlayerStatus, VoiceConnection, getVoiceConnection, createAudioResource, createAudioPlayer} = 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) {

const channel = interaction.options.getChannel('channel');

const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});

connection.destroy();

await interaction.reply(`successfully left ${channel}!`);


},
};
26 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
discord.js: v14.11.0 node: v18.16.0
Nixtl
Nixtl2y ago
So, you don't need most of those in the require() and you don't need to join the channel again. Best way to do it is to just use: https://old.discordjs.dev/#/docs/voice/main/function/getVoiceConnection Then destroy it, it can return undefined so you should check for that. https://old.discordjs.dev/#/docs/voice/main/class/VoiceConnection?scrollTo=destroy Here is what you could use as an example based off your code:
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;
};
connection.destroy();
await interaction.reply(`Successfully left ${channel}!`);
},
};
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;
};
connection.destroy();
await interaction.reply(`Successfully left ${channel}!`);
},
};
Discord.js
Discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
Aman
Aman2y ago
interaction.guild.members.me.voice.disconnect()
Box Bunny with Fan Noises
it works, though in the debug console it says channel is not defined unless
Nixtl
Nixtl2y ago
IIRC this leaves the VoiceConnection open but disconnected, can start eating ram.
Box Bunny with Fan Noises
the code works now when I changed it to this
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) {

const channel = interaction.options.getChannel('channel');

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

connection.destroy();

await interaction.reply(`Successfully left ${channel}!`);
},
};
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) {

const channel = interaction.options.getChannel('channel');

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

connection.destroy();

await interaction.reply(`Successfully left ${channel}!`);
},
};
however, it says "successfully left null" whenever it leaves is there any way to change that so it displays the channel it left?
Nixtl
Nixtl2y ago
Oh yeah at the end there, channel needs changed
Box Bunny with Fan Noises
what do I change it to?
Aman
Aman2y ago
That depends, interaction.guild.members.me.voice represents the current voice connection, if you choose to destroy it, any ongoing stream will be destroyed too (doesn't sound good to me, cuz it's like a sudden jerk) disconnecting would be safer
Box Bunny with Fan Noises
what should I change 'channel' to in order to grab the channel it's disconnecting from?
Nixtl
Nixtl2y ago
If you want to just say the name, change client to: interaction.guild.members.me.voice.channel.name If you want to ping it change ${client} to <#${interaction.guild.members.me.voice.channel.id}>
Box Bunny with Fan Noises
ended up getting an error saying cannot read properies of null (reading 'id') and the application did not respond
Aman
Aman2y ago
Djs works with cache so if you leave your bot connected and restart your bot to apply changes it won't work, just like a phone that closes all apps before shutdown/restart
Box Bunny with Fan Noises
oh, so if I reformat it so the message appears before the disconnect it'll work? tried it, and it still gave me the same error so I'm wondering if it's an issue with the statement in general
Aman
Aman2y ago
Send the message before disconnecting
Box Bunny with Fan Noises
tried it, and it still gave me the same error
Aman
Aman2y ago
Disconnect the bot from any voice channel, run a command to make it join again, and try disconnecting with the new command If the error persists show me what it is
Box Bunny with Fan Noises
Cannot read properties of null (reading 'id')
Aman
Aman2y ago
Are you using this code?
Box Bunny with Fan Noises
I changed it a bit so it's this as recommended
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) {

const channel = interaction.options.getChannel('channel');

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

await interaction.reply(`Successfully left ${interaction.guild.members.me.voice.channel.id}!`);

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) {

const channel = interaction.options.getChannel('channel');

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

await interaction.reply(`Successfully left ${interaction.guild.members.me.voice.channel.id}!`);

connection.destroy();
},
};
Aman
Aman2y ago
await interaction.reply({
content: `Successfully left ${interaction.guild.members.me.voice.channel}`
});
await interaction.reply({
content: `Successfully left ${interaction.guild.members.me.voice.channel}`
});
If this doesn't work sadly you would have to fetch 😔
Box Bunny with Fan Noises
what does changing it to content do?
Aman
Aman2y ago
Nothing, it will appear as raw text Same as what's written earlier, just ordered A response body may have
{
content: "",
embeds: [],
components: [],
files: []
}
{
content: "",
embeds: [],
components: [],
files: []
}
Box Bunny with Fan Noises
it worked! thank you I appreciate all of the help
Aman
Aman2y ago
Ty
Want results from more Discord servers?
Add your server