Skip to content

Commit 80b4c62

Browse files
authored
Merge pull request #56 from vector-im/tadzik/disablable-commands
Make it possible to disable commands dynamically
2 parents de4d6be + 16ebcb9 commit 80b4c62

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

mautrix/bridge/commands/handler.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def _render_message(message: str, allow_html: bool, render_markdown: bool) -> Op
193193

194194

195195
CommandHandlerFunc = Callable[[CommandEvent], Awaitable[Any]]
196+
IsEnabledForFunc = Callable[[CommandEvent], bool]
196197

197198

198199
class CommandHandler:
@@ -213,14 +214,17 @@ class CommandHandler:
213214
management_only: bool
214215
needs_admin: bool
215216
needs_auth: bool
217+
is_enabled_for: IsEnabledForFunc
216218

217219
_help_text: str
218220
_help_args: str
219221
help_section: HelpSection
220222

221223
def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str,
222224
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:
224228
"""
225229
Args:
226230
handler: The function handling the execution of this command.
@@ -243,6 +247,7 @@ def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str
243247
self._help_text = help_text
244248
self._help_args = help_args
245249
self.help_section = help_section
250+
self.is_enabled_for = is_enabled_for
246251

247252
async def get_permission_error(self, evt: CommandEvent) -> Optional[str]:
248253
"""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,
390395
is_management=is_management, has_bridge_bot=has_bridge_bot)
391396
orig_command = command
392397
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+
405408
try:
406409
await self._run_handler(handler, evt)
407410
except Exception:

mautrix/bridge/commands/meta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def _get_help_text(evt: CommandEvent) -> str:
4545
if cache_key not in help_cache:
4646
help_sections: Dict[HelpSection, List[str]] = {}
4747
for handler in command_handlers.values():
48-
if handler.has_help and handler.has_permission(cache_key):
48+
if handler.has_help and handler.has_permission(cache_key) and handler.is_enabled_for(evt):
4949
help_sections.setdefault(handler.help_section, [])
5050
help_sections[handler.help_section].append(handler.help + " ")
5151
help_sorted = sorted(help_sections.items(), key=lambda item: item[0].order)

0 commit comments

Comments
 (0)