@@ -42,10 +42,7 @@ const allCommands: Command[] = [];
4242const getApplicationCommands = ( ) => allCommands . filter ( c => "handleInteraction" in c ) ;
4343const getAutocompleteCommands = ( ) => allCommands . filter ( c => "autocomplete" in c ) ;
4444const getSpecialCommands = ( ) => allCommands . filter ( c => "handleSpecialMessage" in c ) ;
45- const getMessageCommands = ( modCommand : boolean ) =>
46- allCommands
47- . filter ( c => modCommand === ( c . modCommand ?? false ) )
48- . filter ( c => "handleMessage" in c ) ;
45+ const getMessageCommands = ( ) => allCommands . filter ( c => "handleMessage" in c ) ;
4946
5047const latestSpecialCommandInvocations : Record < string , number > = { } ;
5148
@@ -190,18 +187,17 @@ const hasPermissions = (
190187 */
191188
192189async function commandMessageHandler (
193- isModCommand : boolean ,
194190 commandString : string ,
195191 message : ProcessableMessage ,
196192 context : BotContext ,
197193) : Promise < unknown > {
198194 const lowerCommand = commandString . toLowerCase ( ) ;
199- const matchingCommand = getMessageCommands ( isModCommand ) . find (
195+ const command = getMessageCommands ( ) . find (
200196 cmd => cmd . name . toLowerCase ( ) === lowerCommand || cmd . aliases ?. includes ( lowerCommand ) ,
201197 ) ;
202198
203- if ( ( matchingCommand ?. modCommand ?? false ) !== isModCommand ) {
204- throw new Error ( "Somehow we got `command.modCommand !== isModCommand`" ) ;
199+ if ( ! command ) {
200+ return ;
205201 }
206202
207203 if (
@@ -214,21 +210,14 @@ async function commandMessageHandler(
214210 return ;
215211 }
216212
217- if ( ! matchingCommand ) {
218- return ;
219- }
220-
221213 const invoker = message . member ;
222214
223- const isModCommandAndAllowed = ! isModCommand || context . roleGuard . isMod ( invoker ) ;
215+ const isNormalCommandOrUserIsMod = ! command . modCommand || context . roleGuard . isMod ( invoker ) ;
224216
225- if (
226- isModCommandAndAllowed &&
227- hasPermissions ( invoker , matchingCommand . requiredPermissions ?? [ ] )
228- ) {
217+ if ( isNormalCommandOrUserIsMod && hasPermissions ( invoker , command . requiredPermissions ?? [ ] ) ) {
229218 return await sentry . startSpan (
230- { name : matchingCommand . name , op : "command.message" } ,
231- async ( ) => await matchingCommand . handleMessage ( message , context ) ,
219+ { name : command . name , op : "command.message" } ,
220+ async ( ) => await command . handleMessage ( message , context ) ,
232221 ) ;
233222 }
234223
@@ -308,15 +297,13 @@ export const messageCommandHandler = async (
308297 if ( content . startsWith ( plebPrefix ) || content . startsWith ( modPrefix ) ) {
309298 const splitContent = content . split ( / \s + / ) ;
310299
311- const isModCommand = content . startsWith ( modPrefix ) ;
312-
313- const cmdString = isModCommand
300+ const cmdString = content . startsWith ( modPrefix )
314301 ? splitContent [ 0 ] . slice ( modPrefix . length )
315302 : splitContent [ 0 ] . slice ( plebPrefix . length ) ;
316303
317304 if ( cmdString ) {
318305 try {
319- await commandMessageHandler ( isModCommand , cmdString , message , context ) ;
306+ await commandMessageHandler ( cmdString , message , context ) ;
320307 } catch ( err ) {
321308 log . error ( err , "Error while handling message command" ) ;
322309 sentry . captureException ( err ) ;
0 commit comments