@@ -21,6 +21,8 @@ export class CommandHelper {
2121
2222 Logger . info ( `Starting Command loading for ${ guildId } ....` . gray . italic )
2323
24+ await this . addDefaultCommandsToGuild ( client , guildId )
25+
2426 let cmdlist : any [ ] = [ ] ;
2527 const stats = {
2628 commands : 0 ,
@@ -112,50 +114,60 @@ export class CommandHelper {
112114 GuildCommandMangerId : guildId
113115 }
114116 } )
117+ const commands = cmdlist
118+ . filter ( cmd => {
119+ const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
120+ return ! ( override && override . IsEnabled === false ) ;
121+ } )
122+ . map ( cmd => {
123+ const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
124+ if ( override ) {
125+ return {
126+ ...cmd ,
127+ name : override . CustomName . slice ( 0 , 31 ) ,
128+ description : override . Description . slice ( 0 , 99 ) ?? client . commands . get ( override . CodeName ) . command . description . slice ( 0 , 99 ) ,
129+ default_member_permissions : override . Permissions ?? client . commands . get ( override . CodeName ) . command . default_member_permissions
130+ } ;
131+ }
132+ return cmd ;
133+ } )
134+
135+ Logger . info ( `Sending commands to guild ${ guildId } for client ${ client . user . username } ` ) ;
136+
137+ const restClient = new REST ( ) . setToken ( Config . Bot . DiscordBotToken )
138+ const currentCommands = await ( await client . guilds . fetch ( guildId ) ) . commands . fetch ( ) ;
139+
140+ for ( const [ commandId , command ] of currentCommands ) {
141+ try {
142+ const cmd = commands . find ( ( c ) => c . name === command . name ) ;
115143
116- if ( buildInCommandOverrides . length > 0 ) {
117- cmdlist = cmdlist
118- . filter ( cmd => {
119- const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
120- return ! ( override && override . IsEnabled === false ) ;
121- } )
122- . map ( cmd => {
123- const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
124- if ( override ) {
125- return {
126- ...cmd ,
127- name : override . CustomName ,
128- description : override . Description ?? client . commands . get ( override . CodeName ) . command . description ,
129- default_member_permissions : override . Permissions ?? client . commands . get ( override . CodeName ) . command . default_member_permissions
130- } ;
144+ if ( ! cmd ) {
145+ await restClient . delete (
146+ Routes . applicationGuildCommand ( client . user . id , guildId , commandId )
147+ ) ;
148+ Logger . info ( `[CMD DELETE] Deleted command: ${ command . name } ` ) ;
149+ } else {
150+ if ( Config . Commands . CommandsToUpdate . includes ( command . name ) ) {
151+ await restClient . patch (
152+ Routes . applicationGuildCommand ( client . user . id , guildId , commandId ) ,
153+ { body : cmd }
154+ ) ;
155+ Logger . info ( `[CMD UPDATE] Updated command: ${ command . name } ` ) ;
131156 }
132- return cmd ;
133- } )
157+ }
158+ } catch ( e ) {
159+ Logger . error ( `[CMD] Failed to process command ${ command . name } : ${ e } ` ) ;
160+ }
134161 }
135162
136- Logger . info ( `Sending commands to guild ${ guildId } for client ${ client . user . username } ` ) ;
137-
138- try {
139- await fetch ( `https://discord.com/api/v10/applications/${ client . user . id } /guilds/${ guildId } /commands` , {
140- method : "PUT" ,
141- headers : {
142- "Content-Type" : "application/json" ,
143- Authorization : `Bot ${ Config . Bot . DiscordBotToken } `
144- } ,
145- body : JSON . stringify ( [ ] )
146- } )
147- await fetch ( `https://discord.com/api/v10/applications/${ client . user . id } /guilds/${ guildId } /commands` , {
148- method : "PUT" ,
149- headers : {
150- "Content-Type" : "application/json" ,
151- Authorization : `Bot ${ Config . Bot . DiscordBotToken } `
152- } ,
153- body : JSON . stringify ( cmdlist )
154- } )
155- } catch ( e ) {
156- Logger . error ( `Failed to load commands: ${ e } ` )
157- } finally {
158- await this . addDefaultCommandsToGuild ( client , guildId )
163+ for ( const cmd of commands ) {
164+ if ( ! currentCommands . some ( ( c ) => c . name === cmd . name ) ) {
165+ await restClient . post (
166+ Routes . applicationGuildCommands ( client . user . id , guildId ) ,
167+ { body : cmd }
168+ ) ;
169+ Logger . info ( `[CMD ADD] Added new command: ${ cmd . name } ` ) ;
170+ }
159171 }
160172
161173 const ticketCommands = await database . ticketSetups . findMany ( {
@@ -166,52 +178,79 @@ export class CommandHelper {
166178
167179 if ( ticketCommands . length > 0 ) {
168180 for ( const ticketCommand of ticketCommands ) {
169- const clientGuild = await client . guilds . fetch ( guildId ) ;
170-
171- let guildCommand = null ;
172181 try {
173- guildCommand = await clientGuild . commands . fetch ( ticketCommand . SlashCommandId ) ;
174- } catch ( e ) {
175- Logger . error ( `Failed to load commands: ${ e } ` )
176- }
182+ const clientGuild = await client . guilds . fetch ( guildId ) ;
177183
178- if ( ! guildCommand ) {
184+ if ( ! clientGuild || ! ticketCommand || ! ticketCommand . SlashCommandId ) return
185+
186+ let guildCommand = null ;
179187 try {
180- guildCommand = await clientGuild . commands . create ( {
181- name : ticketCommand . SlashCommandName ?? `open-${ ticketCommand . CustomId } -ticket` ,
182- description : ticketCommand . SlashCommandDescription ?? ticketCommand . CustomId ,
183- } ) ;
184-
185- await database . ticketSetups . update ( {
186- where : {
187- CustomId : ticketCommand . CustomId ,
188- } ,
189- data : {
190- SlashCommandId : guildCommand . id ,
191- } ,
192- } ) ;
188+ guildCommand = await clientGuild . commands . fetch ( ticketCommand . SlashCommandId ) ;
193189 } catch ( e ) {
194- Logger . error ( `Failed to load commands: ${ e } ` )
190+ Logger . error ( `[TICKET] Failed to load commands: ${ e } ` )
191+ return
195192 }
196- } else {
197- if (
198- guildCommand . name !== ticketCommand . SlashCommandName ||
199- guildCommand . description !== ticketCommand . SlashCommandDescription
200- ) {
193+
194+ let name = `open-${ ticketCommand . CustomId . split ( "-" ) [ 0 ] } -ticket`
195+ if ( name . length >= 32 || ( ticketCommand ?. SlashCommandName && ticketCommand . SlashCommandName ?. length >= 32 ) ) {
196+ return
197+ } else if ( ticketCommand . SlashCommandName . length <= 30 ) {
198+ name = ticketCommand . SlashCommandName
199+ }
200+
201+ let description = "Open Ticket with command."
202+ if ( ticketCommand && ticketCommand . SlashCommandDescription . length < 31 ) {
203+ description = "Open Ticket with command."
204+ } else if ( guildCommand && guildCommand ?. description < 99 ) {
205+ description = guildCommand . description
206+ }
207+
208+ if ( ! guildCommand ) {
201209 try {
202- const updated = await guildCommand . edit ( {
203- name : ticketCommand . SlashCommandName ?? guildCommand . name ,
204- description : ticketCommand . SlashCommandDescription ?? guildCommand . description ,
210+
211+
212+ guildCommand = await clientGuild . commands . create ( {
213+ name : name ,
214+ description : description ,
205215 } ) ;
206216
207217 await database . ticketSetups . update ( {
208- where : { CustomId : ticketCommand . CustomId } ,
209- data : { SlashCommandId : updated . id } ,
218+ where : {
219+ CustomId : ticketCommand . CustomId ,
220+ } ,
221+ data : {
222+ SlashCommandId : guildCommand . id ,
223+ } ,
210224 } ) ;
211225 } catch ( e ) {
212- Logger . error ( `Failed to load commands: ${ e } ` )
226+ Logger . error ( `[TICKET] Failed to load commands: ${ e } ` )
227+ return
228+ }
229+ } else {
230+ if (
231+ guildCommand . name !== ticketCommand . SlashCommandName ||
232+ guildCommand . description !== ticketCommand . SlashCommandDescription
233+ ) {
234+
235+
236+ try {
237+ const updated = await guildCommand . edit ( {
238+ name : name ,
239+ description : description ,
240+ } ) ;
241+
242+ await database . ticketSetups . update ( {
243+ where : { CustomId : ticketCommand . CustomId } ,
244+ data : { SlashCommandId : updated . id } ,
245+ } ) ;
246+ } catch ( e ) {
247+ Logger . error ( `[TICKET] Failed to load commands: ${ e } ` )
248+ return
249+ }
213250 }
214251 }
252+ } catch ( e ) {
253+ return
215254 }
216255 }
217256 }
0 commit comments