@@ -193,6 +193,7 @@ def _render_message(message: str, allow_html: bool, render_markdown: bool) -> Op
193
193
194
194
195
195
CommandHandlerFunc = Callable [[CommandEvent ], Awaitable [Any ]]
196
+ IsEnabledForFunc = Callable [[CommandEvent ], bool ]
196
197
197
198
198
199
class CommandHandler :
@@ -213,14 +214,17 @@ class CommandHandler:
213
214
management_only : bool
214
215
needs_admin : bool
215
216
needs_auth : bool
217
+ is_enabled_for : IsEnabledForFunc
216
218
217
219
_help_text : str
218
220
_help_args : str
219
221
help_section : HelpSection
220
222
221
223
def __init__ (self , handler : CommandHandlerFunc , management_only : bool , name : str ,
222
224
help_text : str , help_args : str , help_section : HelpSection ,
223
- needs_auth : bool , needs_admin : bool , ** kwargs ) -> None :
225
+ needs_auth : bool , needs_admin : bool ,
226
+ is_enabled_for : IsEnabledForFunc = lambda _ : True ,
227
+ ** kwargs ) -> None :
224
228
"""
225
229
Args:
226
230
handler: The function handling the execution of this command.
@@ -243,6 +247,7 @@ def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str
243
247
self ._help_text = help_text
244
248
self ._help_args = help_args
245
249
self .help_section = help_section
250
+ self .is_enabled_for = is_enabled_for
246
251
247
252
async def get_permission_error (self , evt : CommandEvent ) -> Optional [str ]:
248
253
"""Returns the reason why the command could not be issued.
@@ -390,18 +395,16 @@ async def handle(self, room_id: RoomID, event_id: EventID, sender: BaseUser,
390
395
is_management = is_management , has_bridge_bot = has_bridge_bot )
391
396
orig_command = command
392
397
command = command .lower ()
393
- try :
394
- handler = command_handlers [command ]
395
- except KeyError :
396
- try :
397
- handler = command_aliases [command ]
398
- except KeyError :
399
- if sender .command_status and "next" in sender .command_status :
400
- args .insert (0 , orig_command )
401
- evt .command = ""
402
- handler = sender .command_status ["next" ]
403
- else :
404
- handler = command_handlers ["unknown-command" ]
398
+
399
+ handler = command_handlers .get (command , command_aliases .get (command ))
400
+ if handler is None or not handler .is_enabled_for (evt ):
401
+ if sender .command_status and "next" in sender .command_status :
402
+ args .insert (0 , orig_command )
403
+ evt .command = ""
404
+ handler = sender .command_status ["next" ]
405
+ else :
406
+ handler = command_handlers ["unknown-command" ]
407
+
405
408
try :
406
409
await self ._run_handler (handler , evt )
407
410
except Exception :
0 commit comments