Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to disable commands dynamically #56

Merged
merged 3 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions mautrix/bridge/commands/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def _render_message(message: str, allow_html: bool, render_markdown: bool) -> Op


CommandHandlerFunc = Callable[[CommandEvent], Awaitable[Any]]
IsEnabledForFunc = Callable[[CommandEvent], bool]


class CommandHandler:
Expand All @@ -213,14 +214,17 @@ class CommandHandler:
management_only: bool
needs_admin: bool
needs_auth: bool
is_enabled_for: IsEnabledForFunc

_help_text: str
_help_args: str
help_section: HelpSection

def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str,
help_text: str, help_args: str, help_section: HelpSection,
needs_auth: bool, needs_admin: bool, **kwargs) -> None:
needs_auth: bool, needs_admin: bool,
is_enabled_for: IsEnabledForFunc = lambda _: True,
**kwargs) -> None:
"""
Args:
handler: The function handling the execution of this command.
Expand All @@ -243,6 +247,7 @@ def __init__(self, handler: CommandHandlerFunc, management_only: bool, name: str
self._help_text = help_text
self._help_args = help_args
self.help_section = help_section
self.is_enabled_for = is_enabled_for

async def get_permission_error(self, evt: CommandEvent) -> Optional[str]:
"""Returns the reason why the command could not be issued.
Expand Down Expand Up @@ -390,18 +395,16 @@ async def handle(self, room_id: RoomID, event_id: EventID, sender: BaseUser,
is_management=is_management, has_bridge_bot=has_bridge_bot)
orig_command = command
command = command.lower()
try:
handler = command_handlers[command]
except KeyError:
try:
handler = command_aliases[command]
except KeyError:
if sender.command_status and "next" in sender.command_status:
args.insert(0, orig_command)
evt.command = ""
handler = sender.command_status["next"]
else:
handler = command_handlers["unknown-command"]

handler = command_handlers.get(command, command_aliases.get(command))
if handler is None or not handler.is_enabled_for(evt):
if sender.command_status and "next" in sender.command_status:
args.insert(0, orig_command)
evt.command = ""
handler = sender.command_status["next"]
else:
handler = command_handlers["unknown-command"]

try:
await self._run_handler(handler, evt)
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion mautrix/bridge/commands/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def _get_help_text(evt: CommandEvent) -> str:
if cache_key not in help_cache:
help_sections: Dict[HelpSection, List[str]] = {}
for handler in command_handlers.values():
if handler.has_help and handler.has_permission(cache_key):
if handler.has_help and handler.has_permission(cache_key) and handler.is_enabled_for(evt):
help_sections.setdefault(handler.help_section, [])
help_sections[handler.help_section].append(handler.help + " ")
help_sorted = sorted(help_sections.items(), key=lambda item: item[0].order)
Expand Down