#Creating a leave voice channel command for a bot

38 messages · Page 1 of 1 (latest)

deft mason
#

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}!`);


    },
};```
elder prismBOT
#

• 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.

deft mason
#

discord.js: v14.11.0
node: v18.16.0

sturdy cloak
# deft mason I have a working join command for discordjs v14, so now I'm trying to make a wor...

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}!`);
    },
};
dusky hill
#

interaction.guild.members.me.voice.disconnect()

deft mason
#

it works, though in the debug console it says channel is not defined

#

unless

sturdy cloak
deft mason
#

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}!`);
    },
};```
#

however, it says "successfully left null" whenever it leaves

#

is there any way to change that so it displays the channel it left?

sturdy cloak
#

Oh yeah at the end there, channel needs changed

deft mason
#

what do I change it to?

dusky hill
deft mason
#

what should I change 'channel' to in order to grab the channel it's disconnecting from?

sturdy cloak
#

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}>

deft mason
#

ended up getting an error saying cannot read properies of null (reading 'id')

#

and the application did not respond

dusky hill
deft mason
#

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

dusky hill
deft mason
#

tried it, and it still gave me the same error

dusky hill
#

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

deft mason
#

Cannot read properties of null (reading 'id')

deft mason
#

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();
    },
};```
dusky hill
#
await interaction.reply({
  content: `Successfully left ${interaction.guild.members.me.voice.channel}`
});
#

If this doesn't work sadly you would have to fetch 😔

deft mason
#

what does changing it to content do?

dusky hill
#

Same as what's written earlier, just ordered

#

A response body may have

{
  content: "",
  embeds: [],
  components: [],
  files: []
}
deft mason
#

it worked! thank you

#

I appreciate all of the help