From 69aed2815b84e10207ce5e500669c56052e3979c Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 8 Jun 2019 13:51:36 -0700 Subject: [PATCH] Dynamically list the possible commands --- bot.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 6969515..2d9d382 100644 --- a/bot.py +++ b/bot.py @@ -126,7 +126,11 @@ def cmd_ohai(match: Match[str]) -> Response: @handle_message('!discord') def cmd_discord(match: Match[str]) -> Response: - return MessageResponse(match, 'We do have Discord, you are welcome to join: https://discord.gg/HxpQ3px') + return MessageResponse( + match, + 'We do have Discord, you are welcome to join: ' + 'https://discord.gg/HxpQ3px', + ) class UptimeResponse(Response): @@ -164,9 +168,14 @@ def cmd_uptime(match: Match[str]) -> Response: return UptimeResponse() +COMMAND_RE = re.compile(r'!\w+') + + @handle_message(r'!\w') def cmd_help(match: Match[str]) -> Response: - msg = 'possible commands: !help, !ohai, !uptime' + possible = [COMMAND_RE.search(reg.pattern) for reg, _ in HANDLERS] + commands = ['!help'] + sorted(match.group() for match in possible if match) + msg = f'possible commands: {", ".join(commands)}' if not match['msg'].startswith('!help'): msg = f'unknown command ({esc(match["msg"].split()[0])}), {msg}' return MessageResponse(match, msg)