diff --git a/.gitignore b/.gitignore index 96088a0..92cb982 100644 --- a/.gitignore +++ b/.gitignore @@ -117,4 +117,5 @@ dist .yarn/install-state.gz .pnp.* -hydrus-bot.json \ No newline at end of file +bot-storage.json +config.json \ No newline at end of file diff --git a/config.json b/config.json deleted file mode 100644 index 21fc5fc..0000000 --- a/config.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "homeserver": "https://matrix.pvv.ntnu.no/", - "username": "bot_doorbell", - "password": "Abc@123@Abc", - "prefix": "!", - "activeRooms": [] -} \ No newline at end of file diff --git a/index.mjs b/index.mjs index 3d20c3d..78d7781 100644 --- a/index.mjs +++ b/index.mjs @@ -1,7 +1,8 @@ import { - MatrixAuth, + MatrixClient, + SimpleFsStorageProvider, AutojoinUpgradedRoomsMixin, - RichRepliesPreprocessor + RichRepliesPreprocessor, } from "matrix-bot-sdk"; import axios from "axios"; @@ -9,12 +10,17 @@ import axios from "axios"; import config from "./config.json" assert {type: "json"}; const homeserverUrl = config.homeserver; -const username = config.username; -const password = config.password; +const token = config.token; const prefix = config.prefix; +const rooms = config.rooms; + +// We'll want to make sure the bot doesn't have to do an initial sync every +// time it restarts, so we need to prepare a storage provider. Here we use +// a simple JSON database. +const storage = new SimpleFsStorageProvider("bot-storage.json"); // Now we can create the client and set it up to automatically join rooms. -const client = await new MatrixAuth(homeserverUrl).passwordLogin(username, password); +const client = await new MatrixClient(homeserverUrl, token, storage); AutojoinUpgradedRoomsMixin.setupOnClient(client); client.addPreprocessor(new RichRepliesPreprocessor(false)); @@ -23,8 +29,11 @@ client.addPreprocessor(new RichRepliesPreprocessor(false)); client.on("room.message", handleCommand); client.on("room.invite", (roomId, inviteEvent) => { console.log(inviteEvent); - if (inviteEvent.sender !== "@henrkgr:pvv.ntnu.no") return; - return client.joinRoom(roomId); + console.log(roomId); + console.log(rooms[0]); + if (rooms.includes(roomId)) { + return client.joinRoom(roomId); + } }); // Now that the client is all set up and the event handler is registered, start the @@ -44,6 +53,9 @@ async function handleCommand(roomId, event) { if (event["sender"] === await client.getUserId()) return; // Make sure that the event looks like a command we're expecting + // If not in any authorized rooms, ignore + if (!rooms.includes(roomId)) return; + const text = event["content"]["body"]; if (!text) return;