Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim leading whitespace from !mjolnir command #549

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 " });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h e l p

vibes

await reply;
});
});
Loading