1- import {
2- Guild ,
3- SlashCommandBuilder ,
4- ContextMenuCommandBuilder ,
5- GuildApplicationCommandManager ,
6- } from 'discord.js' ;
1+ import { Guild , GuildApplicationCommandManager } from 'discord.js' ;
72import { getFilePaths } from '../../utils/get-paths' ;
83import { CommandHandlerData , CommandHandlerOptions } from './typings' ;
94import { ContextCommandObject , SlashCommandObject } from '../../../typings' ;
@@ -80,28 +75,17 @@ export class CommandHandler {
8075 }
8176
8277 for ( const command of commands ) {
83- const commandData = command . data ;
84-
85- if (
86- commandData instanceof SlashCommandBuilder ||
87- commandData instanceof ContextMenuCommandBuilder
88- ) {
89- try {
90- commandData . toJSON ( ) ;
91- } catch ( error ) { }
92- }
93-
94- // <!-- TODO: Edit command if there's any changes -->
95-
9678 // <!-- Delete command if options.deleted -->
9779 if ( command . options ?. deleted ) {
9880 const targetCommand = appCommands ?. cache . find ( ( cmd ) => cmd . name === command . data . name ) ;
9981
10082 if ( ! targetCommand ) {
101- console . log ( `⏩ Ignoring: Command "${ command . data . name } " is set to be deleted.` ) ;
83+ console . log (
84+ `⏩ Ignoring: Command "${ command . data . name } " is globally marked as deleted.`
85+ ) ;
10286 } else {
10387 targetCommand . delete ( ) . then ( ( ) => {
104- console . log ( `🗑️ Deleted command "${ command . data . name } " globally.` ) ;
88+ console . log ( `🚮 Deleted command "${ command . data . name } " globally.` ) ;
10589 } ) ;
10690 }
10791
@@ -110,12 +94,12 @@ export class CommandHandler {
11094
11195 if ( ! targetCommand ) {
11296 console . log (
113- `⏩ Ignoring: Command "${ command . data . name } " is set to be deleted in ${ guildCommands . guild . name } .`
97+ `⏩ Ignoring: Command "${ command . data . name } " is marked as deleted for ${ guildCommands . guild . name } .`
11498 ) ;
11599 } else {
116100 targetCommand . delete ( ) . then ( ( ) => {
117101 console . log (
118- `🗑️ Deleted command "${ command . data . name } " in ${ guildCommands . guild . name } .`
102+ `🚮 Deleted command "${ command . data . name } " in ${ guildCommands . guild . name } .`
119103 ) ;
120104 } ) ;
121105 }
@@ -124,6 +108,70 @@ export class CommandHandler {
124108 continue ;
125109 }
126110
111+ // <!-- Edit command if there's any changes -->
112+ let commandData = command . data ;
113+ let editedCommand = false ;
114+
115+ ( ( ) => {
116+ // global
117+ const appGlobalCommand = appCommands ?. cache . find ( ( cmd ) => cmd . name === command . data . name ) ;
118+
119+ if ( appGlobalCommand ) {
120+ const commandsAreDifferent = this . _areSlashCommandsDifferent (
121+ appGlobalCommand ,
122+ commandData
123+ ) ;
124+
125+ if ( commandsAreDifferent ) {
126+ appGlobalCommand
127+ . edit ( commandData )
128+ . then ( ( ) => {
129+ console . log ( `✅ Edited command "${ commandData . name } " globally.` ) ;
130+ } )
131+ . catch ( ( error ) => {
132+ console . log ( `❌ Failed to edit command "${ commandData . name } " globally.` ) ;
133+ console . error ( error ) ;
134+ } ) ;
135+
136+ editedCommand = true ;
137+ }
138+ }
139+
140+ // guilds
141+ for ( const guildCommands of devGuildCommands ) {
142+ const appGuildCommand = guildCommands . cache . find (
143+ ( cmd ) => cmd . name === commandData . name
144+ ) ;
145+
146+ if ( appGuildCommand ) {
147+ const commandsAreDifferent = this . _areSlashCommandsDifferent (
148+ appGuildCommand ,
149+ commandData
150+ ) ;
151+
152+ if ( commandsAreDifferent ) {
153+ appGuildCommand
154+ . edit ( commandData )
155+ . then ( ( ) => {
156+ console . log (
157+ `✅ Edited command "${ commandData . name } " in ${ guildCommands . guild . name } .`
158+ ) ;
159+ } )
160+ . catch ( ( error ) => {
161+ console . log (
162+ `❌ Failed to edit command "${ commandData . name } " in ${ guildCommands . guild . name } .`
163+ ) ;
164+ console . error ( error ) ;
165+ } ) ;
166+
167+ editedCommand = true ;
168+ }
169+ }
170+ }
171+ } ) ( ) ;
172+
173+ if ( editedCommand ) continue ;
174+
127175 // <!-- Registration -->
128176 // guild-based command registration
129177 if ( command . options ?. devOnly ) {
@@ -263,6 +311,21 @@ export class CommandHandler {
263311 } ) ;
264312 }
265313
314+ _areSlashCommandsDifferent ( appCommand : any , localCommand : any ) {
315+ if ( ! appCommand . options ) appCommand . options = [ ] ;
316+ if ( ! localCommand . options ) localCommand . options = [ ] ;
317+
318+ if ( ! appCommand . description ) appCommand . description = '' ;
319+ if ( ! localCommand . description ) localCommand . description = '' ;
320+
321+ if (
322+ localCommand . description !== appCommand . description ||
323+ localCommand . options . length !== appCommand . options . length
324+ ) {
325+ return true ;
326+ }
327+ }
328+
266329 getCommands ( ) {
267330 return this . _data . commands ;
268331 }
0 commit comments