@@ -4,51 +4,51 @@ import type { CommandInteraction, Interaction } from "discord.js";
44import { Permissions , Formatters } from "discord.js" ;
55
66export async function commandHandler (
7- context : MyContext ,
8- interaction : Interaction
7+ context : MyContext ,
8+ interaction : Interaction
99) {
10- if ( interaction . isCommand ( ) ) {
11- await interaction . deferReply ( ) ;
12- const command = context . commands . get ( interaction . commandName ) ;
13- if ( ! command ) return interaction . editReply ( ` Command not found` ) ;
10+ if ( interaction . isCommand ( ) ) {
11+ await interaction . deferReply ( ) ;
12+ const command = context . commands . get ( interaction . commandName ) ;
13+ if ( ! command ) return interaction . editReply ( " Command not found" ) ;
1414
15- if ( commandPermissionCheck ( interaction , command ) ) return ;
16- if ( commandCooldownCheck ( interaction , command , context ) ) return ;
17- try {
18- await command . execute ( interaction , context ) ;
19- } catch ( e ) {
20- console . error ( e ) ;
21- const errorMessage = "An error has occurred" ;
22- interaction . editReply ( errorMessage ) . catch ( console . error ) ;
15+ if ( commandPermissionCheck ( interaction , command ) ) return ;
16+ if ( commandCooldownCheck ( interaction , command , context ) ) return ;
17+ try {
18+ await command . execute ( interaction , context ) ;
19+ } catch ( e ) {
20+ console . error ( e ) ;
21+ const errorMessage = "An error has occurred" ;
22+ interaction . editReply ( errorMessage ) . catch ( console . error ) ;
23+ }
2324 }
24- }
2525}
2626/**
2727 * Locally loads the commands to the context for further use
2828 * @param context
2929 * @returns
3030 */
3131export function loadCommands ( context : MyContext ) {
32- // Promisifies the process of glob
33- return new Promise ( ( resolve ) => {
32+ // Promisifies the process of glob
33+ return new Promise ( ( resolve ) => {
3434 // Find all js files
35- glob ( `${ __dirname } /../commands/**/*.js` , async ( err , files ) => {
36- await Promise . all (
37- files . map ( async ( file ) => {
38- const { default : myCommandFile } : { default : Command } = await import (
39- file
40- ) . catch ( ( err ) => {
41- console . error ( err ) ;
42- // Since the return value gets destructured, an empty object is returned
43- return { } ;
44- } ) ;
45- if ( ! myCommandFile ) return ;
46- context . commands . set ( myCommandFile . data . name , myCommandFile ) ;
47- } )
48- ) ;
49- resolve ( undefined ) ;
35+ glob ( `${ __dirname } /../commands/**/*.js` , async ( err , files ) => {
36+ await Promise . all (
37+ files . map ( async ( file ) => {
38+ const { default : myCommandFile } : { default : Command } = await import (
39+ file
40+ ) . catch ( ( err ) => {
41+ console . error ( err ) ;
42+ // Since the return value gets destructured, an empty object is returned
43+ return { } ;
44+ } ) ;
45+ if ( ! myCommandFile ) return ;
46+ context . commands . set ( myCommandFile . data . name , myCommandFile ) ;
47+ } )
48+ ) ;
49+ resolve ( undefined ) ;
50+ } ) ;
5051 } ) ;
51- } ) ;
5252}
5353/**
5454 * Checks if the bot or a user has the needed permissions to run a command
@@ -57,88 +57,89 @@ export function loadCommands(context: MyContext) {
5757 * @returns Whether to cancel the command
5858 */
5959function commandPermissionCheck (
60- interaction : CommandInteraction ,
61- command : Command
60+ interaction : CommandInteraction ,
61+ command : Command
6262) : boolean {
63- const { client, user, channel } = interaction ;
64- if ( channel . type === "DM" || ! channel ) {
65- if ( command . guildOnly ) {
66- interaction
67- . editReply (
68- `This is a guild exclusive command, not to be executed in a dm`
69- )
70- . catch ( console . error ) ;
71- // For guild only commands that were executed in a dm, cancel the command
72- return true ;
63+ const { client, user, channel } = interaction ;
64+ // If the channel is a dm, if it's a partial, channel.type wouldn't exist
65+ if ( channel . type === "DM" || ! channel ) {
66+ if ( command . guildOnly ) {
67+ interaction
68+ . editReply (
69+ "This is a guild exclusive command, not to be executed in a dm"
70+ )
71+ . catch ( console . error ) ;
72+ // For guild only commands that were executed in a dm, cancel the command
73+ return true ;
74+ }
75+ // If it's not a guild only command, since permissions aren't a thing on dms, allow execution
76+ return false ;
7377 }
74- // If it's not a guild only command, since permissions aren't a thing on dms, allow execution
75- return false ;
76- }
77- if ( command . botPermissions ) {
78- const botPermissions = new Permissions ( command . botPermissions ) ;
79- // The required permissions for the bot to run the command, missing in the channel.
80- const missingPermissions = channel
81- . permissionsFor ( client . user )
82- . missing ( botPermissions ) ;
83- if ( missingPermissions . length > 0 ) {
84- interaction
85- . editReply (
86- `In order to run this command, I need the following permissions: ${ missingPermissions
87- . map ( ( perm ) => `\`${ perm } \`` )
88- . join ( ", " ) } `
89- )
90- . catch ( console . error ) ;
91- return true ;
78+ if ( command . botPermissions ) {
79+ const botPermissions = new Permissions ( command . botPermissions ) ;
80+ // The required permissions for the bot to run the command, missing in the channel.
81+ const missingPermissions = channel
82+ . permissionsFor ( client . user )
83+ . missing ( botPermissions ) ;
84+ if ( missingPermissions . length > 0 ) {
85+ interaction
86+ . editReply (
87+ `In order to run this command, I need the following permissions: ${ missingPermissions
88+ . map ( ( perm ) => `\`${ perm } \`` )
89+ . join ( ", " ) } `
90+ )
91+ . catch ( console . error ) ;
92+ return true ;
93+ }
9294 }
93- }
94- if ( command . authorPermissions ) {
95- const authorPermissions = new Permissions ( command . authorPermissions ) ;
96- // The required permissions for the user to run the command, missing in the channel.
97- const missingPermissions = channel
98- . permissionsFor ( user . id )
99- . missing ( authorPermissions ) ;
100- if ( missingPermissions . length > 0 ) {
101- interaction
102- . editReply (
103- `In order to run this command, you need: ${ missingPermissions
104- . map ( ( perm ) => `\` ${ perm } \`` )
105- . join ( ", " ) } `
106- )
107- . catch ( console . error ) ;
108- return true ;
95+ if ( command . authorPermissions ) {
96+ const authorPermissions = new Permissions ( command . authorPermissions ) ;
97+ // The required permissions for the user to run the command, missing in the channel.
98+ const missingPermissions = channel
99+ . permissionsFor ( user . id )
100+ . missing ( authorPermissions ) ;
101+ if ( missingPermissions . length > 0 ) {
102+ interaction
103+ . editReply (
104+ `In order to run this command, you need: ${ missingPermissions
105+ . map ( ( perm ) => `\` ${ perm } \`` )
106+ . join ( ", " ) } `
107+ )
108+ . catch ( console . error ) ;
109+ return true ;
110+ }
109111 }
110- }
111- // By default, allow execution;
112- return false ;
112+ // By default, allow execution;
113+ return false ;
113114}
114115function commandCooldownCheck (
115- interaction : CommandInteraction ,
116- command : Command ,
117- context : MyContext
116+ interaction : CommandInteraction ,
117+ command : Command ,
118+ context : MyContext
118119) : boolean {
119- const { user } = interaction ;
120- if ( command . cooldown ) {
121- const id = user . id + "/" + interaction . commandName ;
122- const existingCooldown = context . cooldownCounter . get ( id ) ;
123- if ( existingCooldown ) {
124- if ( Date . now ( ) >= existingCooldown ) {
125- context . cooldownCounter . delete ( id ) ;
126- return false ;
127- }
128- interaction
129- . editReply (
130- `Please wait ${ Formatters . time (
131- Date . now ( ) + existingCooldown ,
132- "R"
133- ) } before using the command again`
134- )
135- . catch ( console . error ) ;
136- return true ;
120+ const { user } = interaction ;
121+ if ( command . cooldown ) {
122+ const id = user . id + "/" + interaction . commandName ;
123+ const existingCooldown = context . cooldownCounter . get ( id ) ;
124+ if ( existingCooldown ) {
125+ if ( Date . now ( ) >= existingCooldown ) {
126+ context . cooldownCounter . delete ( id ) ;
127+ return false ;
128+ }
129+ interaction
130+ . editReply (
131+ `Please wait ${ Formatters . time (
132+ Date . now ( ) + existingCooldown ,
133+ "R"
134+ ) } before using the command again`
135+ )
136+ . catch ( console . error ) ;
137+ return true ;
138+ }
139+ context . cooldownCounter . set (
140+ user . id + "/" + interaction . commandName ,
141+ Date . now ( ) + command . cooldown
142+ ) ;
137143 }
138- context . cooldownCounter . set (
139- user . id + "/" + interaction . commandName ,
140- Date . now ( ) + command . cooldown
141- ) ;
142- }
143- return false ;
144+ return false ;
144145}
0 commit comments