@@ -125,7 +125,7 @@ export class CommandHelper {
125125 }
126126
127127 if ( ! Config . Bot . DiscordApplicationId || ! Config . Bot . DiscordBotToken ) {
128- throw new Error ( "Missing environment variables: APPLICATIONID or TOKEN " ) ;
128+ throw new Error ( "Missing Config variables: DiscordApplicationId or DiscordBotToken " ) ;
129129 }
130130
131131 const restClient = new REST ( { version : "10" } ) . setToken ( Config . Bot . DiscordBotToken ) ;
@@ -135,92 +135,106 @@ export class CommandHelper {
135135 body : [ ] ,
136136 } ) ;
137137
138+
138139 const allGuilds = await client . guilds . fetch ( ) ;
139140 for ( const guild of allGuilds . values ( ) ) {
140141 const buildInCommandOverrides = await database . buildInCommands . findMany ( {
141142 where : {
142143 GuildCommandMangerId : guild . id
143144 }
144145 } )
146+ try {
147+ cmdlist = cmdlist
148+ . filter ( cmd => {
149+ const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
150+ return ! ( override && override . IsEnabled === false ) ;
151+ } )
152+ . map ( cmd => {
153+ const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
154+ if ( override ) {
155+ return {
156+ ...cmd ,
157+ name : override . CustomName ,
158+ description : override . Description ?? client . commands . get ( override . CodeName ) . data . description ,
159+ default_member_permissions : override . Permissions ?? client . commands . get ( override . CodeName ) . data . default_member_permissions
160+ } ;
161+ }
162+ return cmd ;
163+ } )
145164
146- cmdlist = cmdlist
147- . filter ( cmd => {
148- const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
149- return ! ( override && override . IsEnabled === false ) ;
150- } )
151- . map ( cmd => {
152- const override = buildInCommandOverrides . find ( o => o . CodeName === cmd . name ) ;
153- if ( override ) {
154- return {
155- ...cmd ,
156- name : override . CustomName ,
157- description : override . Description ?? client . commands . get ( override . CodeName ) . data . description ,
158- default_member_permissions : override . Permissions ?? client . commands . get ( override . CodeName ) . data . default_member_permissions
159- } ;
160- }
161- return cmd ;
162- } )
165+ await restClient . put ( Routes . applicationGuildCommands ( Config . Bot . DiscordApplicationId , guild . id ) , {
166+ body : cmdlist ,
167+ } ) ;
163168
164- await restClient . put ( Routes . applicationGuildCommands ( Config . Bot . DiscordApplicationId , guild . id ) , {
165- body : cmdlist ,
166- } ) ;
167169
168- const ticketCommands = await database . ticketSetups . findMany ( {
169- where : {
170- GuildId : guild . id
171- }
172- } )
170+ const ticketCommands = await database . ticketSetups . findMany ( {
171+ where : {
172+ GuildId : guild . id
173+ }
174+ } )
173175
174- for ( const ticketCommand of ticketCommands ) {
175- const clientGuild = await client . guilds . fetch ( guild . id ) ;
176+ for ( const ticketCommand of ticketCommands ) {
177+ const clientGuild = await client . guilds . fetch ( guild . id ) ;
176178
177- let guildCommand = null ;
178- try {
179- guildCommand = await clientGuild . commands . fetch ( ticketCommand . SlashCommandId ) ;
180- } catch {
181- }
179+ let guildCommand = null ;
180+ try {
181+ guildCommand = await clientGuild . commands . fetch ( ticketCommand . SlashCommandId ) ;
182+ } catch {
183+ }
182184
183- if ( ! guildCommand ) {
184- guildCommand = await clientGuild . commands . create ( {
185- name : ticketCommand . SlashCommandName ?? `open-${ ticketCommand . CustomId } -ticket` ,
186- description : ticketCommand . SlashCommandDescription ?? ticketCommand . CustomId ,
187- } ) ;
188-
189- await database . ticketSetups . update ( {
190- where : {
191- CustomId : ticketCommand . CustomId ,
192- } ,
193- data : {
194- SlashCommandId : guildCommand . id ,
195- } ,
196- } ) ;
197- } else {
198- if (
199- guildCommand . name !== ticketCommand . SlashCommandName ||
200- guildCommand . description !== ticketCommand . SlashCommandDescription
201- ) {
202- const updated = await guildCommand . edit ( {
203- name : ticketCommand . SlashCommandName ?? guildCommand . name ,
204- description : ticketCommand . SlashCommandDescription ?? guildCommand . description ,
185+ if ( ! guildCommand ) {
186+ guildCommand = await clientGuild . commands . create ( {
187+ name : ticketCommand . SlashCommandName ?? `open-${ ticketCommand . CustomId } -ticket` ,
188+ description : ticketCommand . SlashCommandDescription ?? ticketCommand . CustomId ,
205189 } ) ;
206190
207191 await database . ticketSetups . update ( {
208- where : { CustomId : ticketCommand . CustomId } ,
209- data : { SlashCommandId : updated . id } ,
192+ where : {
193+ CustomId : ticketCommand . CustomId ,
194+ } ,
195+ data : {
196+ SlashCommandId : guildCommand . id ,
197+ } ,
210198 } ) ;
199+ } else {
200+ if (
201+ guildCommand . name !== ticketCommand . SlashCommandName ||
202+ guildCommand . description !== ticketCommand . SlashCommandDescription
203+ ) {
204+ const updated = await guildCommand . edit ( {
205+ name : ticketCommand . SlashCommandName ?? guildCommand . name ,
206+ description : ticketCommand . SlashCommandDescription ?? guildCommand . description ,
207+ } ) ;
208+
209+ await database . ticketSetups . update ( {
210+ where : { CustomId : ticketCommand . CustomId } ,
211+ data : { SlashCommandId : updated . id } ,
212+ } ) ;
213+ }
211214 }
212215 }
216+
217+ } catch ( e ) {
218+ Logger . error ( {
219+ timestamp : new Date ( ) . toISOString ( ) ,
220+ level : "error" ,
221+ label : "CommandHelper" ,
222+ message : `Command loading failed with error: ${ e } - Commands failed at ${ cmdlist . map ( ( c ) => c . name ) } commands` ,
223+ botType : Config . BotType . toString ( ) || "Unknown" ,
224+ action : LoggingAction . Command ,
225+ } ) ;
213226 }
214227
228+ Logger . info ( {
229+ timestamp : new Date ( ) . toISOString ( ) ,
230+ level : "info" ,
231+ label : "CommandHelper" ,
232+ message : `Discord added ${ cmdlist . length } commands (${ stats . subCommands } subCommands, ${ stats . subCommandGroups } subCommandGroups), ${ stats . userInstall } userInstall commands, ${ stats . contextMenus } context menu commands from ${ moduleDirectories . length } module(s) for ${ allGuilds . size } Guilds` ,
233+ botType : Config . BotType . toString ( ) || "Unknown" ,
234+ action : LoggingAction . Command ,
235+ } ) ;
215236 }
216- Logger . info ( {
217- timestamp : new Date ( ) . toISOString ( ) ,
218- level : "info" ,
219- label : "CommandHelper" ,
220- message : `Discord added ${ cmdlist . length } commands (${ stats . subCommands } subCommands, ${ stats . subCommandGroups } subCommandGroups), ${ stats . userInstall } userInstall commands, ${ stats . contextMenus } context menu commands from ${ moduleDirectories . length } module(s) for ${ allGuilds . size } Guilds` ,
221- botType : Config . BotType . toString ( ) || "Unknown" ,
222- action : LoggingAction . Command ,
223- } ) ;
237+
224238 }
225239
226240 public static async guildLoadCommands ( client : ExtendedClient ) {
0 commit comments