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


    },
};
Was this page helpful?