/[sudobot]/trunk/src/utils/structures/BaseCommand.ts
ViewVC logotype

Annotation of /trunk/src/utils/structures/BaseCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (hide annotations)
Mon Jul 29 17:29:01 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 3569 byte(s)
refactor(commands): require permissions
1 rakin 81 import { ModalSubmitInteraction } from 'discord-modals';
2 rakin 207 import { PermissionResolvable, AutocompleteInteraction, CommandInteraction, CommandInteractionOption, ContextMenuInteraction, Interaction, Message, MessageEditOptions, MessageOptions, MessagePayload, WebhookEditMessageOptions, SelectMenuInteraction, ButtonInteraction, GuildMember } from 'discord.js';
3 rakin 51 import DiscordClient from '../../client/Client';
4 rakin 64 import AutoCompleteOptions from '../../types/AutoCompleteOptions';
5 rakin 51 import CommandOptions from '../../types/CommandOptions';
6     import InteractionOptions from '../../types/InteractionOptions';
7    
8     export default abstract class BaseCommand {
9     supportsInteractions: boolean = false;
10     supportsLegacy: boolean = true;
11 rakin 125 supportsContextMenu: boolean = false;
12 rakin 56 coolDown?: number;
13 rakin 58 ownerOnly: boolean = false;
14 rakin 197 permissions: PermissionResolvable[] = [];
15    
16 rakin 51 constructor(private name: string, private category: string, private aliases: Array<string>) {
17 rakin 197
18 rakin 51 }
19    
20     getName(): string {
21     return this.name;
22     }
23    
24     getCategory(): string {
25     return this.category;
26     }
27    
28     getAliases(): Array<string> {
29     return this.aliases;
30     }
31    
32 rakin 64 async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions): Promise <void> {
33    
34     }
35    
36 rakin 81 async default(client: DiscordClient, interaction: Interaction): Promise <void> {
37    
38     }
39    
40     async modalSubmit(client: DiscordClient, interaction: ModalSubmitInteraction): Promise <void> {
41    
42     }
43    
44 rakin 125 async deferReply(msg: Message | CommandInteraction | ContextMenuInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {
45 rakin 51 if (msg instanceof Message) {
46     return await msg[edit ? 'edit' : 'reply'](options as (MessageOptions & MessageEditOptions));
47     }
48    
49     return (await msg.editReply(options as string | MessagePayload | WebhookEditMessageOptions)) as Message;
50     }
51    
52 rakin 207 async perms(client: DiscordClient, message: Message | Interaction) {
53     let member: GuildMember | null = null;
54    
55     if (message.member && !(message.member instanceof GuildMember)) {
56     try {
57     member = (await message.guild?.members.fetch(message.member!.user.id)) ?? null;
58     }
59     catch (e) {
60     console.log(e);
61     return false;
62     }
63     }
64     else {
65     member = message.member;
66     }
67    
68     for await (let permission of this.permissions) {
69     if (!member?.permissions.has(permission, true)) {
70     if (message instanceof Interaction && !message.isRepliable())
71     return;
72    
73     await message.reply({
74     embeds: [
75     {
76     description: ":x: You don't have enough permissions to run this command.",
77     color: 0xf14a60
78     }
79     ]
80     });
81    
82     return false;
83     }
84     }
85    
86     return true;
87     }
88    
89     async execute(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions) {
90     if (!(await this.perms(client, message))) {
91     return;
92     }
93    
94     await this.run(client, message, options);
95     }
96    
97 rakin 51 abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;
98 rakin 197 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26