Bot works, kinda

This commit is contained in:
Henrik Kjernmoen Gran 2024-08-15 22:37:15 +02:00
parent 68736384a8
commit f58db13fe2
3 changed files with 21 additions and 15 deletions

3
.gitignore vendored
View File

@ -117,4 +117,5 @@ dist
.yarn/install-state.gz
.pnp.*
hydrus-bot.json
bot-storage.json
config.json

View File

@ -1,7 +0,0 @@
{
"homeserver": "https://matrix.pvv.ntnu.no/",
"username": "bot_doorbell",
"password": "Abc@123@Abc",
"prefix": "!",
"activeRooms": []
}

View File

@ -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;