diff --git a/src/Mjolnir.ts b/src/Mjolnir.ts index c5a5ab23..c1541c28 100644 --- a/src/Mjolnir.ts +++ b/src/Mjolnir.ts @@ -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) => { diff --git a/test/integration/helloTest.ts b/test/integration/helloTest.ts index 0ebc050a..ac3f0072 100644 --- a/test/integration/helloTest.ts +++ b/test/integration/helloTest.ts @@ -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; + }); });