From 922e15148506cb374fd419b6486484f19a89924b Mon Sep 17 00:00:00 2001 From: SmileyFace799 Date: Thu, 15 Aug 2024 23:14:31 +0200 Subject: [PATCH] Bot now respects prefixes --- index.mjs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/index.mjs b/index.mjs index 78d7781..ea80246 100644 --- a/index.mjs +++ b/index.mjs @@ -27,10 +27,7 @@ client.addPreprocessor(new RichRepliesPreprocessor(false)); // We also want to make sure we can receive events - this is where we will // handle our command. client.on("room.message", handleCommand); -client.on("room.invite", (roomId, inviteEvent) => { - console.log(inviteEvent); - console.log(roomId); - console.log(rooms[0]); +client.on("room.invite", (roomId) => { if (rooms.includes(roomId)) { return client.joinRoom(roomId); } @@ -40,7 +37,7 @@ client.on("room.invite", (roomId, inviteEvent) => { // client up. This will start it syncing. client.start().then(() => console.log("Client started!")); -// This is our event handler for dealing with the `!hello` command. +// This is our event handler for dealing with commands. async function handleCommand(roomId, event) { // Don't handle events that don't have contents (they were probably redacted) if (!event["content"]) return; @@ -56,8 +53,10 @@ async function handleCommand(roomId, event) { // If not in any authorized rooms, ignore if (!rooms.includes(roomId)) return; - const text = event["content"]["body"]; - if (!text) return; + // Check that the message starts with the prefix, and get the prefix-less message + const rawText = event["content"]["body"]; + if (!rawText || !rawText.startsWith(prefix)) return; + const text = rawText.substring(prefix.length); console.log(text);