From f522276a3a9ebd21d4289b6a67fbc9e23c02eab1 Mon Sep 17 00:00:00 2001 From: "H. Shay" Date: Tue, 29 Oct 2024 14:54:59 -0700 Subject: [PATCH] prettify help menu and stop printing help menu on unknown command --- src/commands/CommandHandler.ts | 79 +++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/commands/CommandHandler.ts b/src/commands/CommandHandler.ts index 4c3ae17b..92b0fbf5 100644 --- a/src/commands/CommandHandler.ts +++ b/src/commands/CommandHandler.ts @@ -146,35 +146,49 @@ export async function handleCommand(roomId: string, event: { content: { body: st return await execIgnoreCommand(roomId, event, mjolnir, parts); } else if (parts[1] === "ignored") { return await execListIgnoredCommand(roomId, event, mjolnir, parts); - } else { + } else if (parts[1] === "help") { // Help menu - const menu = + const protectionMenu = "" + - "!mjolnir - Print status information\n" + - "!mjolnir status - Print status information\n" + "!mjolnir status protection [subcommand] - Print status information for a protection\n" + - "!mjolnir ban [reason] - Adds an entity to the ban list\n" + - "!mjolnir unban [apply] - Removes an entity from the ban list. If apply is 'true', the users matching the glob will actually be unbanned\n" + - "!mjolnir redact [room alias/ID] [limit] - Redacts messages by the sender in the target room (or all rooms), up to a maximum number of events in the backlog (default 1000)\n" + - "!mjolnir redact - Redacts a message by permalink\n" + - "!mjolnir kick [room alias/ID] [reason] - Kicks a user or all of those matching a glob in a particular room or all protected rooms\n" + - "!mjolnir rules - Lists the rules currently in use by Mjolnir\n" + - "!mjolnir rules matching - Lists the rules in use that will match this entity e.g. `!rules matching @foo:example.com` will show all the user and server rules, including globs, that match this user\n" + - "!mjolnir sync - Force updates of all lists and re-apply rules\n" + - "!mjolnir verify - Ensures Mjolnir can moderate all your rooms\n" + - "!mjolnir list create - Creates a new ban list with the given shortcode and alias\n" + - "!mjolnir watch - Watches a ban list\n" + - "!mjolnir unwatch - Unwatches a ban list\n" + - "!mjolnir import - Imports bans and ACLs into the given list\n" + - "!mjolnir default - Sets the default list for commands\n" + - "!mjolnir deactivate - Deactivates a user ID\n" + "!mjolnir protections - List all available protections\n" + "!mjolnir enable - Enables a particular protection\n" + "!mjolnir disable - Disables a particular protection\n" + "!mjolnir config set . [value] - Change a protection setting\n" + "!mjolnir config add . [value] - Add a value to a list protection setting\n" + "!mjolnir config remove . [value] - Remove a value from a list protection setting\n" + - "!mjolnir config get [protection] - List protection settings\n" + + "!mjolnir config get [protection] - List protection settings\n"; + + const actionMenu = + "" + + "!mjolnir ban [reason] - Adds an entity to the ban list\n" + + "!mjolnir unban [apply] - Removes an entity from the ban list. If apply is 'true', the users matching the glob will be manually unbanned in each protected room.\n" + + "!mjolnir redact [room alias/ID] [limit] - Redacts messages by the sender in the target room (or all rooms), up to a maximum number of events in the backlog (default 1000)\n" + + "!mjolnir redact - Redacts a message by permalink\n" + + "!mjolnir kick [room alias/ID] [reason] - Kicks a user or all of those matching a glob in a particular room or all protected rooms\n" + + "!mjolnir deactivate - Deactivates a user ID\n" + + "!mjolnir since / [rooms...] [reason] - Apply an action ('kick', 'ban', 'mute', 'unmute' or 'show') to all users who joined a room since / (up to users)\n" + + "!mjolnir powerlevel [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" + + "!mjolnir make admin [user alias/ID] - Make the specified user or the bot itself admin of the room\n" + + "!mjolnir suspend - Suspend the specified user\n" + + "!mjolnir unsuspend - Unsuspend the specified user\n" + + "!mjolnir ignore - Add user to list of users/servers that cannot be banned/ACL'd. Note that this does not survive restart.\n" + + "!mjolnir ignored - List currently ignored entities.\n" + + "!mjolnir shutdown room [message] - Uses the bot's account to shut down a room, preventing access to the room on this server\n"; + + const policyListMenu = + "" + + "!mjolnir list create - Creates a new ban list with the given shortcode and alias\n" + + "!mjolnir watch - Watches a ban list\n" + + "!mjolnir unwatch - Unwatches a ban list\n" + + "!mjolnir import - Imports bans and ACLs into the given list\n" + + "!mjolnir default - Sets the default list for commands\n" + + "!mjolnir rules - Lists the rules currently in use by Mjolnir\n" + + "!mjolnir rules matching - Lists the rules in use that will match this entity e.g. `!rules matching @foo:example.com` will show all the user and server rules, including globs, that match this user\n" + + "!mjolnir sync - Force updates of all lists and re-apply rules\n"; + + const roomsMenu = + "" + "!mjolnir rooms - Lists all the protected rooms\n" + "!mjolnir rooms add - Adds a protected room (may cause high server load)\n" + "!mjolnir rooms remove - Removes a protected room\n" + @@ -185,20 +199,25 @@ export async function handleCommand(roomId: string, event: { content: { body: st "!mjolnir alias add - Adds to \n" + "!mjolnir alias remove - Deletes the room alias from whatever room it is attached to\n" + "!mjolnir resolve - Resolves a room alias to a room ID\n" + - "!mjolnir since / [rooms...] [reason] - Apply an action ('kick', 'ban', 'mute', 'unmute' or 'show') to all users who joined a room since / (up to users)\n" + - "!mjolnir shutdown room [message] - Uses the bot's account to shut down a room, preventing access to the room on this server\n" + - "!mjolnir powerlevel [room alias/ID] - Sets the power level of the user in the specified room (or all protected rooms)\n" + - "!mjolnir make admin [user alias/ID] - Make the specified user or the bot itself admin of the room\n" + - "!mjolnir suspend - Suspend the specified user\n" + - "!mjolnir unsuspend - Unsuspend the specified user\n" + - "!mjolnir ignore - Add user to list of users/servers that cannot be banned/ACL'd. Note that this does not survive restart.\n" + - "!mjolnir ignored - List currently ignored entities.\n" + + "!mjolnir shutdown room [message] - Uses the bot's account to shut down a room, preventing access to the room on this server\n"; + + const botMenu = + "" + + "!mjolnir - Print status information\n" + + "!mjolnir status - Print status information\n" + + "!mjolnir verify - Ensures Mjolnir can moderate all your rooms\n" + "!mjolnir help - This menu\n"; - const html = `Mjolnir help:
${htmlEscape(menu)}
`; - const text = `Mjolnir help:\n${menu}`; + + const html = `

Mjolnir help menu:


Protection Actions/Options:
${htmlEscape(protectionMenu)}

Moderation Actions:
${htmlEscape(actionMenu)}

Policy List Options/Actions:
${htmlEscape(policyListMenu)}

Room Managment:
${htmlEscape(roomsMenu)}

Bot Status and Management:
${htmlEscape(botMenu)}
`; + const text = `Mjolnir help menu:\n Protection Actions/Options:\n ${protectionMenu} \n Moderation Actions: ${actionMenu}\n Policy List Options/Actions: \n ${policyListMenu} \n Room Management: ${roomsMenu} \n Bot Status and Management: \n ${botMenu} `; const reply = RichReply.createFor(roomId, event, text, html); reply["msgtype"] = "m.notice"; return await mjolnir.client.sendMessage(roomId, reply); + } else { + return await mjolnir.client.sendMessage(roomId, { + msgtype: "m.text", + body: "Unknown command - use `!mjolnir help` to display the help menu.", + }); } } catch (e) { LogService.error("CommandHandler", extractRequestError(e));