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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 56 by rakin, Mon Jul 29 17:28:24 2024 UTC revision 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1  import { CommandInteraction, CommandInteractionOption, Interaction, Message, MessageEditOptions, MessageOptions, MessagePayload, WebhookEditMessageOptions } from 'discord.js';  /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20    import { ModalSubmitInteraction } from 'discord-modals';
21    import { PermissionResolvable, AutocompleteInteraction, CommandInteraction, ContextMenuInteraction, Interaction, Message, MessageEditOptions, MessageOptions, MessagePayload, WebhookEditMessageOptions, GuildMember } from 'discord.js';
22  import DiscordClient from '../../client/Client';  import DiscordClient from '../../client/Client';
23    import AutoCompleteOptions from '../../types/AutoCompleteOptions';
24  import CommandOptions from '../../types/CommandOptions';  import CommandOptions from '../../types/CommandOptions';
25  import InteractionOptions from '../../types/InteractionOptions';  import InteractionOptions from '../../types/InteractionOptions';
26    
27  export default abstract class BaseCommand {  export default abstract class BaseCommand {
28      supportsInteractions: boolean = false;      supportsInteractions: boolean = false;
29      supportsLegacy: boolean = true;      supportsLegacy: boolean = true;
30        supportsContextMenu: boolean = false;
31      coolDown?: number;      coolDown?: number;
32        ownerOnly: boolean = false;
33        permissions: PermissionResolvable[] = [];
34    
35        
36      constructor(private name: string, private category: string, private aliases: Array<string>) {      constructor(private name: string, private category: string, private aliases: Array<string>) {
37            
38      }      }
39        
40      getName(): string {      getName(): string {
41          return this.name;          return this.name;
42      }      }
43        
44      getCategory(): string {      getCategory(): string {
45          return this.category;          return this.category;
46      }      }
47        
48      getAliases(): Array<string> {      getAliases(): Array<string> {
49          return this.aliases;          return this.aliases;
50      }      }
51        
52        async permissionValidation(client: DiscordClient, member: GuildMember): Promise <boolean> {
53            return true;
54        }
55    
56        async autoComplete(client: DiscordClient, interaction: AutocompleteInteraction, options: AutoCompleteOptions): Promise <void> {
57    
58        }
59    
60        async default(client: DiscordClient, interaction: Interaction): Promise <void> {
61            
62        }
63    
64        async modalSubmit(client: DiscordClient, interaction: ModalSubmitInteraction): Promise <void> {
65            
66        }
67    
68      async deferReply(msg: Message | CommandInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {      async deferReply(msg: Message | CommandInteraction | ContextMenuInteraction, options: MessageOptions | string | MessagePayload | WebhookEditMessageOptions, edit: boolean = false): Promise<Message | CommandInteraction> {
69          if (msg instanceof Message) {          if (msg instanceof Message) {
70              return await msg[edit ? 'edit' : 'reply'](options as (MessageOptions & MessageEditOptions));              return await msg[edit ? 'edit' : 'reply'](options as (MessageOptions & MessageEditOptions));
71          }          }
# Line 32  export default abstract class BaseComman Line 73  export default abstract class BaseComman
73          return (await msg.editReply(options as string | MessagePayload | WebhookEditMessageOptions)) as Message;          return (await msg.editReply(options as string | MessagePayload | WebhookEditMessageOptions)) as Message;
74      }      }
75    
76        async perms(client: DiscordClient, message: Message | Interaction) {
77            let member: GuildMember | null = null;
78    
79            if (message.member && !(message.member instanceof GuildMember)) {
80                try {
81                    member = (await message.guild?.members.fetch(message.member!.user.id)) ?? null;
82                }
83                catch (e) {
84                    console.log(e);
85                    return false;
86                }
87            }
88            else {
89                member = message.member;
90            }
91    
92            for await (let permission of this.permissions) {
93                if (!member?.permissions.has(permission, true)) {                
94                    if (message instanceof Interaction && !message.isRepliable())
95                        return;
96        
97                    await message.reply({
98                        embeds: [
99                            {
100                                description: ":x: You don't have enough permissions to run this command.",
101                                color: 0xf14a60
102                            }
103                        ]
104                    });
105        
106                    return false;
107                }
108            }
109    
110            if (!(await this.permissionValidation(client, member!))) {
111                if (message instanceof Interaction && !message.isRepliable())
112                    return;
113                
114                await message.reply({
115                    embeds: [
116                        {
117                            description: ":x: You don't have enough permissions to run this command.",
118                            color: 0xf14a60
119                        }
120                    ]
121                });
122    
123                return false;
124            }
125    
126            return true;
127        }
128    
129        async execute(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions) {
130            if (!(await this.perms(client, message))) {
131                return;
132            }
133    
134            await this.run(client, message, options);
135        }
136    
137      abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;      abstract run(client: DiscordClient, message: Message | Interaction, options: CommandOptions | InteractionOptions): Promise<void>;
 }  
138    }

Legend:
Removed from v.56  
changed lines
  Added in v.393

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26