diff --git a/src/commands/voice/_category.ts b/src/commands/voice/_category.ts new file mode 100644 index 0000000..b2742d7 --- /dev/null +++ b/src/commands/voice/_category.ts @@ -0,0 +1,8 @@ +import { CommandCategoryInfo } from '../../commands'; + +const info: CommandCategoryInfo = { + name: 'Voice', + description: 'Voice chat related commands' +}; + +export default info; diff --git a/src/commands/voice/join.ts b/src/commands/voice/join.ts new file mode 100644 index 0000000..afd9679 --- /dev/null +++ b/src/commands/voice/join.ts @@ -0,0 +1,47 @@ +import { ChatInputCommandInteraction, GuildMember, 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.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 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; \ No newline at end of file diff --git a/src/commands/voice/leave.ts b/src/commands/voice/leave.ts new file mode 100644 index 0000000..511a9e0 --- /dev/null +++ b/src/commands/voice/leave.ts @@ -0,0 +1,32 @@ +import { ChatInputCommandInteraction, GuildMember, SlashCommandBuilder } from "discord.js"; +import { Command } from "../../commands"; +import { getVoiceConnection } from "@discordjs/voice"; + +const builder = new SlashCommandBuilder() + .setName("leave") + .setDescription("Makes the bot leave its 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 === null) { + interaction.reply("This command only works on guilds"); + return; + } + + const connection = getVoiceConnection(interaction.guildId as string); + if (!connection) { + interaction.reply('currently not connected to a voice channel') + return; + } + + connection.disconnect(); + connection.destroy(); + + interaction.reply("Disconnected"); + } +} + +export default cmd; \ No newline at end of file