diff --git a/src/command.ts b/src/command.ts index 424b5814..b68327c0 100644 --- a/src/command.ts +++ b/src/command.ts @@ -35,6 +35,11 @@ abstract class SlashCommand { * @default false */ public readonly needsVoiceChannel: boolean; + /** + * If the command needs the user to be in the same voice channel as the bot + * @default false + */ + public readonly needsSameVoiceChannel: boolean; /** * If the command is a global command * @@ -54,13 +59,15 @@ abstract class SlashCommand { defer = true, ephemeral = true, guildOnly = false, - needsVoiceChannel = false + needsVoiceChannel = false, + needsSameVoiceChannel = false } = {}){ this.data = data; this.defer = defer; this.ephemeral = ephemeral; this.guildOnly = guildOnly; this.needsVoiceChannel = needsVoiceChannel; + this.needsSameVoiceChannel = needsSameVoiceChannel; } /** diff --git a/src/commands/disconnect.ts b/src/commands/disconnect.ts index 13864eb1..4496456b 100644 --- a/src/commands/disconnect.ts +++ b/src/commands/disconnect.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from "@discordjs/builders"; -import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js"; +import { CommandInteraction, MessageEmbed } from "discord.js"; import { CustomClient } from "../client"; import { SlashCommand } from "../command"; @@ -11,7 +11,7 @@ module.exports = class extends SlashCommand { { ephemeral: false, guildOnly: true, - needsVoiceChannel: true + needsSameVoiceChannel: true }); } @@ -20,16 +20,6 @@ module.exports = class extends SlashCommand { {guild} = interaction, {channel} = guild.me.voice; - if(!(interaction.member as GuildMember).voice.channel.equals(channel)) return interaction.editReply({ - embeds: [ - new MessageEmbed({ - color: "RED", - title: "Can't disconnect", - description: "You need to be in the same voice channel to disconnect" - }) - ] - }); - client.player.deleteQueue(guild); interaction.editReply({ diff --git a/src/commands/nowPlaying.ts b/src/commands/nowPlaying.ts index 93652b0e..f8f3f97b 100644 --- a/src/commands/nowPlaying.ts +++ b/src/commands/nowPlaying.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder } from "@discordjs/builders"; import { stripIndents } from "common-tags"; -import { CommandInteraction, GuildMember, MessageEmbed } from "discord.js"; +import { CommandInteraction, MessageEmbed } from "discord.js"; import { CustomClient } from "../client"; import { SlashCommand } from "../command"; @@ -11,7 +11,7 @@ module.exports = class extends SlashCommand { .setDescription("show the current playing song"), { guildOnly: true, - needsVoiceChannel: true + needsSameVoiceChannel: true }); } @@ -30,17 +30,6 @@ module.exports = class extends SlashCommand { ] }); - if(!(interaction.member as GuildMember).voice.channel.equals(interaction.guild.me.voice.channel)) - return interaction.editReply({ - embeds: [ - new MessageEmbed({ - color: "RED", - title: "Not in voice channel", - description: "You need to be in the same voice channel to show the current song" - }) - ] - }); - interaction.editReply({ embeds: [ new MessageEmbed({ diff --git a/src/events/client/interactionCreate.ts b/src/events/client/interactionCreate.ts index 1b7cc313..f4a0a403 100644 --- a/src/events/client/interactionCreate.ts +++ b/src/events/client/interactionCreate.ts @@ -1,5 +1,6 @@ -import { CommandInteraction, GuildMember, Interaction, MessageEmbed } from "discord.js"; +import { CommandInteraction, GuildMember, Interaction, MessageEmbed, MessageEmbedOptions } from "discord.js"; import { CustomClient } from "../../client"; +import { SlashCommand } from "../../command"; import { CustomConsole } from "../../console"; import { ClientEvent } from "../../event"; @@ -8,6 +9,21 @@ module.exports = class extends ClientEvent { super("interactionCreate"); } + checks: { + fn: (interaction: CommandInteraction, command: SlashCommand) => boolean; + reply: Omit; + }[] = [ + { + fn(interaction, command){ + return command.guildOnly && !interaction.inGuild(); + }, + reply: { + title: "Guild Only", + description: "This command can only be used in a guild" + } + } + ]; + async run(interaction: Interaction){ if(!interaction.isCommand()) return; @@ -16,7 +32,7 @@ module.exports = class extends ClientEvent { if(!command) return; - //#region checks + //#region misc checks if(command.guildOnly && !interaction.inGuild()) return (interaction as CommandInteraction).reply({ embeds: [ @@ -28,8 +44,12 @@ module.exports = class extends ClientEvent { ], ephemeral: true }); + //#endregion - if(command.needsVoiceChannel && !(interaction.member as GuildMember).voice.channel) + //#region voice channel checks + const voiceChannel = (interaction.member as GuildMember).voice.channel; + + if(command.needsVoiceChannel && !voiceChannel) return interaction.reply({ embeds: [ new MessageEmbed({ @@ -40,6 +60,18 @@ module.exports = class extends ClientEvent { ], ephemeral: true }); + + if(command.needsSameVoiceChannel && !interaction.guild.me.voice.channel?.equals(voiceChannel)) + return interaction.reply({ + embeds: [ + new MessageEmbed({ + color: "RED", + title: "Not in voice channel", + description: "You need to be in the same voice channel as me to use this command" + }) + ], + ephemeral: true + }); //#endregion if(command.defer) await interaction.deferReply({ephemeral: command.ephemeral});