import { ChatInputCommandInteraction, GuildMember, PermissionsBitField, SlashCommandBuilder } from 'discord.js'; import { Command } from '../../commands'; import { CreateVoiceConnectionOptions, getVoiceConnection, joinVoiceChannel, JoinVoiceChannelOptions } from '@discordjs/voice'; const builder = new SlashCommandBuilder() .setName('join') .setDescription('Makes the bot join your current voice channel'); const cmd: Command = { name: builder.name, builder: builder, execute: async (interaction: ChatInputCommandInteraction): Promise => { const member = interaction.member as GuildMember; if (!member || !interaction.guild) { interaction.reply('This command only works on guilds'); return; } if (!member.voice.channel || !member.voice.channelId) { interaction.reply('You are not currently on a voice channel'); return; } const me = interaction.guild.members.me as GuildMember; if ( getVoiceConnection(interaction.guild.id) && me.voice.channelId === member.voice.channelId ) { interaction.reply('Already connected'); return; } const voiceChannel = member.voice.channel; if ( voiceChannel.userLimit != 0 && voiceChannel.members.size >= voiceChannel.userLimit ) { interaction.reply('Channel is full'); return; } const perms = voiceChannel.permissionsFor(me); if (!perms.has(PermissionsBitField.Flags.ViewChannel)) { interaction.reply("I don't have permissions to see that channel"); return; } if (!perms.has(PermissionsBitField.Flags.Connect)) { interaction.reply("I don't have the permissions to join that channel"); return; } if (!perms.has(PermissionsBitField.Flags.Speak)) { interaction.reply("I don't have permissions to speak on that channel"); return; } const voiceOptions: JoinVoiceChannelOptions & CreateVoiceConnectionOptions = { channelId: member.voice.channelId, guildId: interaction.guild.id, adapterCreator: interaction.guild.voiceAdapterCreator }; const connection = await joinVoiceChannel(voiceOptions); if (!connection) { interaction.reply('Unable to join'); return; } interaction.reply('Joined'); } }; export default cmd;