/[sudobot]/branches/2.x/src/utils/structures/BaseCommand.ts
ViewVC logotype

Contents of /branches/2.x/src/utils/structures/BaseCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4191 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
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
17 constructor(private name: string, private category: string, private aliases: Array<string>) {
18
19 }
20
21 getName(): string {
22 return this.name;
23 }
24
25 getCategory(): string {
26 return this.category;
27 }
28
29 getAliases(): Array<string> {
30 return this.aliases;
31 }
32
33 async permissionValidation(client: DiscordClient, member: GuildMember): Promise <boolean> {
34 return true;
35 }
36
37 async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions): Promise <void> {
38
39 }
40
41 async default(client: DiscordClient, interaction: Interaction): Promise <void> {
42
43 }
44
45 async modalSubmit(client: DiscordClient, interaction: ModalSubmitInteraction): Promise <void> {
46
47 }
48
49 async deferReply(msg: Message | CommandInteraction | ContextMenuInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {
50 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 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 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 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 abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;
119 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26