/[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 245 - (hide annotations)
Mon Jul 29 17:29:11 2024 UTC (8 months, 2 weeks ago) by rakin
File MIME type: application/typescript
File size: 4191 byte(s)
feat: add support of custom permission checker method
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 rakin 245
16 rakin 197
17 rakin 51 constructor(private name: string, private category: string, private aliases: Array<string>) {
18 rakin 245
19 rakin 51 }
20 rakin 245
21 rakin 51 getName(): string {
22     return this.name;
23     }
24 rakin 245
25 rakin 51 getCategory(): string {
26     return this.category;
27     }
28 rakin 245
29 rakin 51 getAliases(): Array<string> {
30     return this.aliases;
31     }
32 rakin 245
33     async permissionValidation(client: DiscordClient, member: GuildMember): Promise <boolean> {
34     return true;
35     }
36 rakin 51
37 rakin 64 async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions): Promise <void> {
38    
39     }
40    
41 rakin 81 async default(client: DiscordClient, interaction: Interaction): Promise <void> {
42    
43     }
44    
45     async modalSubmit(client: DiscordClient, interaction: ModalSubmitInteraction): Promise <void> {
46    
47     }
48    
49 rakin 125 async deferReply(msg: Message | CommandInteraction | ContextMenuInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {
50 rakin 51 if (msg instanceof Message) {
51     return await msg[edit ? 'edit' : 'reply'](options as (MessageOptions & MessageEditOptions));
52     }
53    
54     return (await msg.editReply(options as string | MessagePayload | WebhookEditMessageOptions)) as Message;
55     }
56    
57 rakin 207 async perms(client: DiscordClient, message: Message | Interaction) {
58     let member: GuildMember | null = null;
59    
60     if (message.member && !(message.member instanceof GuildMember)) {
61     try {
62     member = (await message.guild?.members.fetch(message.member!.user.id)) ?? null;
63     }
64     catch (e) {
65     console.log(e);
66     return false;
67     }
68     }
69     else {
70     member = message.member;
71     }
72    
73     for await (let permission of this.permissions) {
74     if (!member?.permissions.has(permission, true)) {
75     if (message instanceof Interaction && !message.isRepliable())
76     return;
77    
78     await message.reply({
79     embeds: [
80     {
81     description: ":x: You don't have enough permissions to run this command.",
82     color: 0xf14a60
83     }
84     ]
85     });
86    
87     return false;
88     }
89     }
90    
91 rakin 245 if (message instanceof Interaction && !message.isRepliable())
92     return;
93    
94     if (!(await this.permissionValidation(client, member!))) {
95     await message.reply({
96     embeds: [
97     {
98     description: ":x: You don't have enough permissions to run this command.",
99     color: 0xf14a60
100     }
101     ]
102     });
103    
104     return false;
105     }
106    
107 rakin 207 return true;
108     }
109    
110     async execute(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions) {
111     if (!(await this.perms(client, message))) {
112     return;
113     }
114    
115     await this.run(client, message, options);
116     }
117    
118 rakin 51 abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;
119 rakin 197 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26