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:
26 Replies
• 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.discord.js: v14.11.0
node: v18.16.0
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:
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.
interaction.guild.members.me.voice.disconnect()
it works, though in the debug console it says channel is not defined
unless
IIRC this leaves the VoiceConnection open but disconnected, can start eating ram.
the code works now when I changed it to this
however, it says "successfully left null" whenever it leaves
is there any way to change that so it displays the channel it left?
Oh yeah at the end there, channel needs changed
what do I change it to?
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 saferwhat should I change 'channel' to in order to grab the channel it's disconnecting from?
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}>
ended up getting an error saying cannot read properies of null (reading 'id')
and the application did not respond
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
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
Send the message before disconnecting
tried it, and it still gave me the same error
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
Cannot read properties of null (reading 'id')
Are you using this code?
I changed it a bit so it's this as recommended
If this doesn't work sadly you would have to fetch 😔
what does changing it to content do?
Nothing, it will appear as raw text
Same as what's written earlier, just ordered
A response body may have
it worked! thank you
I appreciate all of the help
Ty