|
| 1 | +import { emojis } from '#main/config/Constants.js'; |
| 2 | +import Logger from '#main/utils/Logger.js'; |
| 3 | +import { isDev } from '#main/utils/Utils.js'; |
1 | 4 | import { Message, PermissionsBitField } from 'discord.js'; |
2 | 5 |
|
3 | 6 | export interface CommandData { |
4 | 7 | name: string; |
5 | 8 | description: string; |
6 | | - category: 'Moderation' | 'Network'; // add more categories as needed |
| 9 | + category: 'Moderation' | 'Network' | 'Utility'; // add more categories as needed |
7 | 10 | usage: string; |
8 | 11 | examples: string[]; |
9 | 12 | aliases: string[]; |
10 | 13 | dbPermission?: boolean; |
| 14 | + totalArgs: number; |
11 | 15 | cooldown?: number; |
12 | 16 | ownerOnly?: boolean; |
13 | | - requiredBotPermissions?: PermissionsBitField[]; |
14 | | - requiredUserPermissions?: PermissionsBitField[]; |
| 17 | + requiredBotPermissions?: PermissionsBitField; |
| 18 | + requiredUserPermissions?: PermissionsBitField; |
15 | 19 | } |
16 | 20 |
|
17 | 21 | export default abstract class BasePrefixCommand { |
18 | 22 | public abstract readonly data: CommandData; |
19 | | - public abstract execute(message: Message, args: string[]): Promise<void>; |
| 23 | + protected abstract run(message: Message, args: string[]): Promise<void>; |
| 24 | + public async execute(message: Message, args: string[]): Promise<void> { |
| 25 | + try { |
| 26 | + // Check if command is owner-only |
| 27 | + if (this.data.ownerOnly && !isDev(message.author.id)) { |
| 28 | + await message.reply(`${emojis.botdev} This command can only be used by the bot owner.`); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Check user permissions |
| 33 | + const { requiredBotPermissions, requiredUserPermissions } = this.data; |
| 34 | + |
| 35 | + const missingPerms = |
| 36 | + requiredUserPermissions && |
| 37 | + message.member?.permissions.missing(requiredUserPermissions, true); |
| 38 | + if (missingPerms?.length) { |
| 39 | + await message.reply(`${emojis.neutral} You're missing the following permissions: ${missingPerms.join(', ')}`); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const botMissingPerms = |
| 44 | + requiredBotPermissions && |
| 45 | + message.guild?.members.me?.permissions.missing(requiredBotPermissions, true); |
| 46 | + if (botMissingPerms?.length) { |
| 47 | + await message.reply(`${emojis.no} I'm missing the following permissions: ${botMissingPerms.join(', ')}`); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (this.data.dbPermission && !message.inGuild()) { |
| 52 | + await message.reply(`${emojis.no} This command can only be used in a server.`); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (this.data.totalArgs > args.length) { |
| 57 | + const examplesStr = |
| 58 | + this.data.examples.length > 0 ? `\n**Examples**: ${this.data.examples.join('\n')}` : ''; |
| 59 | + await message.reply( |
| 60 | + `${emojis.neutral} One or more args missing.\n**Usage**: ${this.data.usage}\n${examplesStr}`, |
| 61 | + ); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // Run command |
| 66 | + await this.run(message, args); |
| 67 | + } |
| 68 | + catch (error) { |
| 69 | + Logger.error(error); |
| 70 | + await message.reply('There was an error executing this command!'); |
| 71 | + } |
| 72 | + } |
20 | 73 | } |
0 commit comments