From 9e5c87ac569e5c8a0fabb54325545e56442b32e9 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 12 Feb 2020 15:27:27 -0700 Subject: [PATCH] Add options to specify custom command prefixes Fixes https://github.com/matrix-org/mjolnir/issues/29 --- config/default.yaml | 17 +++++++++++++++++ src/Mjolnir.ts | 16 ++++++++++++++-- src/config.ts | 4 ++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/config/default.yaml b/config/default.yaml index 69b53e2..01b0b6e 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -93,3 +93,20 @@ banListServer: enabled: false bind: "0.0.0.0" port: 5186 + +# Misc options for command handling and commands +commands: + # If true, Mjolnir will respond to commands like !help and !ban instead of + # requiring a prefix. This is useful if Mjolnir is the only bot running in + # your management room. + # + # Note that Mjolnir can be pinged by display name instead of having to use + # the !mjolnir prefix. For example, "my_moderator_bot: ban @spammer:example.org" + # will ban a user. + allowNoPrefix: false + + # In addition to the bot's display name, !mjolnir, and optionally no prefix + # above, the bot will respond to these names. The items here can be used either + # as display names or prefixed with exclamation points. + additionalPrefixes: + - "mjolnir_bot" diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index 0273b39..d172ec3 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -63,12 +63,24 @@ export class Mjolnir { const content = event['content']; if (content['msgtype'] === "m.text" && content['body']) { - const prefixes = [COMMAND_PREFIX, this.localpart + ":", this.displayName + ":", await client.getUserId() + ":"]; + const prefixes = [ + COMMAND_PREFIX, + this.localpart + ":", + this.displayName + ":", + await client.getUserId() + ":", + ...config.commands.additionalPrefixes.map(p => `!${p}`), + ...config.commands.additionalPrefixes.map(p => `${p}:`), + ...config.commands.additionalPrefixes, + ]; + if (config.commands.allowNoPrefix) prefixes.push("!"); + const prefixUsed = prefixes.find(p => content['body'].startsWith(p)); if (!prefixUsed) return; // rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name) - event['content']['body'] = COMMAND_PREFIX + content['body'].substring(prefixUsed.length); + let restOfBody = content['body'].substring(prefixUsed.length); + if (!restOfBody.startsWith(" ")) restOfBody = ` ${restOfBody}`; + event['content']['body'] = COMMAND_PREFIX + restOfBody; LogService.info("Mjolnir", `Command being run by ${event['sender']}: ${event['content']['body']}`); await client.sendReadReceipt(roomId, event['event_id']); diff --git a/src/config.ts b/src/config.ts index a52dbaa..b85890b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -43,6 +43,10 @@ interface IConfig { bind: string; port: number; }; + commands: { + allowNoPrefix: boolean; + additionalPrefixes: string[]; + }; /** * Config options only set at runtime. Try to avoid using the objects