Skip to content

Commit

Permalink
Merge pull request #549 from matrix-org/hs/fix-mjolnir-trim-command
Browse files Browse the repository at this point in the history
Trim leading whitespace from !mjolnir command
  • Loading branch information
turt2live authored Oct 16, 2024
2 parents 0019aad + 62b1a54 commit 54635ae
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
70 changes: 38 additions & 32 deletions src/Mjolnir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,40 +200,46 @@ export class Mjolnir {
matrixEmitter.on("room.event", this.handleEvent.bind(this));

matrixEmitter.on("room.message", async (roomId, event) => {
const eventContent = event.content;
if (roomId !== this.managementRoomId) return;
if (!event["content"]) return;

const content = event["content"];
if (content["msgtype"] === "m.text" && content["body"]) {
const prefixes = [
COMMAND_PREFIX,
this.localpart + ":",
this.displayName + ":",
(await client.getUserId()) + ":",
this.localpart + " ",
this.displayName + " ",
(await client.getUserId()) + " ",
...config.commands.additionalPrefixes.map((p) => `!${p}`),
...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"].toLowerCase().startsWith(p.toLowerCase()));
if (!prefixUsed) return;

// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
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"]}`);

client.sendReadReceipt(roomId, event["event_id"]).catch((e: any) => {
LogService.warn("Mjolnir", "Error sending read receipt: ", e);
});
return handleCommand(roomId, event, this);
if (typeof eventContent !== "object") return;

const { msgtype, body: originalBody, sender, event_id } = eventContent;
if (msgtype !== "m.text" || typeof originalBody !== "string") {
return;
}

const prefixes = [
COMMAND_PREFIX,
this.localpart + ":",
this.displayName + ":",
(await client.getUserId()) + ":",
this.localpart + " ",
this.displayName + " ",
(await client.getUserId()) + " ",
...config.commands.additionalPrefixes.map((p) => `!${p}`),
...config.commands.additionalPrefixes.map((p) => `${p}:`),
...config.commands.additionalPrefixes.map((p) => `${p} `),
...config.commands.additionalPrefixes,
];
if (config.commands.allowNoPrefix) prefixes.push("!");

const sanitizedBody = originalBody.toLowerCase().trim();

const prefixUsed = prefixes.find((p) => sanitizedBody.startsWith(p.toLowerCase()));
if (!prefixUsed) return;

// rewrite the event body to make the prefix uniform (in case the bot has spaces in its display name)
let restOfBody = originalBody.trim().substring(prefixUsed.length);
if (!restOfBody.startsWith(" ")) restOfBody = ` ${restOfBody}`;

eventContent.body = COMMAND_PREFIX + restOfBody;
LogService.info("Mjolnir", `Command being run by ${sender}: ${eventContent.body}`);

client.sendReadReceipt(roomId, event_id).catch((e: any) => {
LogService.warn("Mjolnir", "Error sending read receipt: ", e);
});
return handleCommand(roomId, event, this);
});

matrixEmitter.on("room.join", (roomId: string, event: any) => {
Expand Down
18 changes: 18 additions & 0 deletions test/integration/helloTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,22 @@ describe("Test: !help command", function () {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: "m.text", body: "!mjolnir help" });
await reply;
});
it("Mjolnir responded to !mjolnir help with extra spaces", async function () {
this.timeout(30000);
// send a messgage
await client.joinRoom(this.config.managementRoom);
// listener for getting the event reply
let reply = new Promise((resolve, reject) => {
client.on(
"room.message",
noticeListener(this.mjolnir.managementRoomId, (event) => {
if (event.content.body.includes("Print status information")) {
resolve(event);
}
}),
);
});
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: "m.text", body: " !mjolnir help " });
await reply;
});
});

0 comments on commit 54635ae

Please sign in to comment.