how to create /leave command i know how to write /join command

here is the code for join i wan t leave command same like join command
const { SlashCommandBuilder } = require('discord.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('join')
.setDescription('join voice channel in which you are'),
async execute(interaction) {
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channelId,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});
await interaction.reply("joined your voice channel")


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

module.exports = {
data: new SlashCommandBuilder()
.setName('join')
.setDescription('join voice channel in which you are'),
async execute(interaction) {
const connection = joinVoiceChannel({
channelId: interaction.member.voice.channelId,
guildId: interaction.guild.id,
adapterCreator: interaction.guild.voiceAdapterCreator,
});
await interaction.reply("joined your voice channel")


}
}
23 Replies
d.js toolkit
d.js toolkit4mo 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! - Marked as resolved by OP
d.js docs
d.js docs4mo ago
:method: VoiceConnection#disconnect() Disconnects the VoiceConnection, allowing the possibility of rejoining later on. :method: VoiceConnection#destroy() Destroys the VoiceConnection, preventing it from connecting to voice again. This method should be called when you no longer require the VoiceConnection to prevent memory leaks.
Toothless
Toothless4mo ago
i did try but i am noob i like spoon fiding can you write the code thats what you think but the truth is i am reading this god dam voice docummantaton from last 1 month but i got no idea please help me can i ping you @ʎǝɹquoɯ
monbrey
monbrey4mo ago
It would help not to ping while asking if you can, since it's clearly against the rules
mris
mris4mo ago
maybe read up a bit of js that'd really help you and everyone else honestly
Toothless
Toothless4mo ago
sorry i did tried and implement some code so here is the code and error
const { SlashCommandBuilder } = require('discord.js');
const {connection} = require('@discordjs/voice');

module.exports = {
data: new SlashCommandBuilder()
.setName('leave')
.setDescription('leaves voice channel in which you are'),
async execute(interaction) {
const connection = connection.destroy();
await interaction.reply("the bot has leaved the channel")

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

module.exports = {
data: new SlashCommandBuilder()
.setName('leave')
.setDescription('leaves voice channel in which you are'),
async execute(interaction) {
const connection = connection.destroy();
await interaction.reply("the bot has leaved the channel")

}
}
ReferenceError: Cannot access 'connection' before initialization
Heph
Heph4mo ago
Get the voice connection from the server first const { getVoiceConnection } = require('@discordjs/voice') getVoiceConnection(interaction.guildId) https://discord.js.org/docs/packages/voice/0.14.0/getVoiceConnection:Function I'm just a hobbyist so could 1000% be wrong. the voice documentation is quite confusing and very limited compared to everything else, so I don't blame you for not catching onto it.
voice | getVoiceConnection
Finds a voice connection with the given guild id and group. Defaults to the 'default' group.
Toothless
Toothless4mo ago
i tried this wanna see error 😆
Heph
Heph4mo ago
Sure
Heph
Heph4mo ago
I'm actually making the command now too lol though in my basic testing this worked fine
No description
Toothless
Toothless4mo ago
but you are using newstate function thats diff story cna you show full code
Heph
Heph4mo ago
It's the same thing - the guildId The newState is when a user joins / leaves a vc whereas interaction is the... well interaction. But both values being put in is the guildId so it's the same thing
Toothless
Toothless4mo ago
ok let me see wait h a minute
Heph
Heph4mo ago
What error did you get?
Toothless
Toothless4mo ago
wth now i am not getting error bot the command is not working here is the code const { SlashCommandBuilder } = require('discord.js'); const { getVoiceConnection } = require('@discordjs/voice'); module.exports = { data: new SlashCommandBuilder() .setName('leave') .setDescription('leaves voice channel in which you are'), async execute(interaction) { const connection = getVoiceConnection({guildId : interaction.guildId}); if (connection) connection.destroy(); await interaction.reply("the bot has leaved the channel")
} } do you see something wrong
Heph
Heph4mo ago
Yes, why are you doing {guildId : interaction.guildId}
Toothless
Toothless4mo ago
ohh my bad wait wow man you are genius it worked can you explainme how please you are a goat
Heph
Heph4mo ago
To destroy the connection, you need to tell it what the connection is. getVoiceConnection just says what that connection is. getVoiceConnection provided with a guildId checks to see if the bot is connected in a VC in that guild
Toothless
Toothless4mo ago
man i am so pi*sd now because that goad damm voice documantation s**ks i do not get the did so much good job on rest of guid but like on voice the did nothing
Heph
Heph4mo ago
It's a little rough, but it's definitely all there for the most part. Just have to read a little more is all. The documents is not your only guide though. Don't be ashamed to look up on YouTube, GitHub, and simply Google, etc. Just remember to actually learn how it works and not just copy what you see
Toothless
Toothless4mo ago
yes you are right btw i watched every thing what i can lol i am happy now now time to do some work on play command last thing can you be my friend do not worry i will only dm you when its so important
Heph
Heph4mo ago
You don't need to be my friend to dm, just keep in mind I have no obligation to assist or even respond. Probably best to just make a post here But if I'm not busy, happy to help if I have the knowledge
Toothless
Toothless4mo ago
ofcourse okey here is deal for non cooding friend mean i wont dm you for code just be a friend time to mark the post as resolved