mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Add options to specify custom command prefixes
Fixes https://github.com/matrix-org/mjolnir/issues/29
This commit is contained in:
parent
06c09ef944
commit
9e5c87ac56
@ -93,3 +93,20 @@ banListServer:
|
|||||||
enabled: false
|
enabled: false
|
||||||
bind: "0.0.0.0"
|
bind: "0.0.0.0"
|
||||||
port: 5186
|
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"
|
||||||
|
@ -63,12 +63,24 @@ export class Mjolnir {
|
|||||||
|
|
||||||
const content = event['content'];
|
const content = event['content'];
|
||||||
if (content['msgtype'] === "m.text" && content['body']) {
|
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));
|
const prefixUsed = prefixes.find(p => content['body'].startsWith(p));
|
||||||
if (!prefixUsed) return;
|
if (!prefixUsed) return;
|
||||||
|
|
||||||
// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
|
// 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']}`);
|
LogService.info("Mjolnir", `Command being run by ${event['sender']}: ${event['content']['body']}`);
|
||||||
|
|
||||||
await client.sendReadReceipt(roomId, event['event_id']);
|
await client.sendReadReceipt(roomId, event['event_id']);
|
||||||
|
@ -43,6 +43,10 @@ interface IConfig {
|
|||||||
bind: string;
|
bind: string;
|
||||||
port: number;
|
port: number;
|
||||||
};
|
};
|
||||||
|
commands: {
|
||||||
|
allowNoPrefix: boolean;
|
||||||
|
additionalPrefixes: string[];
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config options only set at runtime. Try to avoid using the objects
|
* Config options only set at runtime. Try to avoid using the objects
|
||||||
|
Loading…
Reference in New Issue
Block a user