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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (show annotations)
Mon Jul 29 17:29:01 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3569 byte(s)
refactor(commands): require permissions
1 import { ModalSubmitInteraction } from 'discord-modals';
2 import { PermissionResolvable, AutocompleteInteraction, CommandInteraction, CommandInteractionOption, ContextMenuInteraction, Interaction, Message, MessageEditOptions, MessageOptions, MessagePayload, WebhookEditMessageOptions, SelectMenuInteraction, ButtonInteraction, GuildMember } from 'discord.js';
3 import DiscordClient from '../../client/Client';
4 import AutoCompleteOptions from '../../types/AutoCompleteOptions';
5 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 supportsContextMenu: boolean = false;
12 coolDown?: number;
13 ownerOnly: boolean = false;
14 permissions: PermissionResolvable[] = [];
15
16 constructor(private name: string, private category: string, private aliases: Array<string>) {
17
18 }
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 async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions): Promise <void> {
33
34 }
35
36 async default(client: DiscordClient, interaction: Interaction): Promise <void> {
37
38 }
39
40 async modalSubmit(client: DiscordClient, interaction: ModalSubmitInteraction): Promise <void> {
41
42 }
43
44 async deferReply(msg: Message | CommandInteraction | ContextMenuInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {
45 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 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 abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;
98 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26