possumbot/index.js
2024-08-22 15:34:03 +00:00

127 lines
3.7 KiB
JavaScript

import apfetch from "./auth-fetch.js";
import { encode } from "html-entities";
import * as sdk from "matrix-js-sdk";
import sdkExt from "./lib/ext.js";
import { resolve } from "node:path";
import fs from "node:fs";
import util from "util";
import fetch from "node-fetch";
global.fetch = fetch;
import Olm from "@matrix-org/olm";
global.Olm = Olm;
import env from "dotenv";
env.config();
const client = sdk.createClient({
baseUrl: process.env.BASE_URL,
accessToken: process.env.ACCESS_TOKEN,
deviceId: process.env.DEVICE_ID,
userId: process.env.USER_ID
});
sdkExt(client);
var prefixes = [process.env.PREFIX];
client.initialized = false;
client.modules = [];
client.commands = [];
for (const file of fs.readdirSync(resolve("modules"))) {
try {
if(file.startsWith(".")) continue;
var module = (await import(resolve("modules", file))).default;
client.modules.push(module);
client.log("[load:modules]", `loaded ${module.name}`);
} catch (err) {
client.error("[load:modules]", `failed to load "${file}":\n`, err);
}
}
for (const file of fs.readdirSync(resolve("commands"))) {
try {
if(file.startsWith(".")) continue;
var command = (await import(resolve("commands", file))).default;
command.usage = process.env.PREFIX + command.command + (command.usage ? " " + command.usage : '');
client.commands.push(command);
client.log("[load:commands]", `loaded ${command.name}`);
} catch (err) {
client.error("[load:commands]", `failed to load "${file}":`, err);
}
}
function doModule(client, event) {
client.modules.forEach(m => {
if(!m) return;
m.hooks?.message(client, event);
});
}
function doCommand(client, event, cmd, args) {
var command;
command = client.commands.filter(c=>c.command==cmd)[0];
if(!command)
return false;
if(command.owner && event.sender.userId != process.env.OWNER_ID) {
client.reply(event, "nuh uh", '<img src="mxc://possum.city/cu5mPRM1ITiJZQRgAyzJXDz22jN6TBo9" alt="nuh uh" />');
return true;
}
command.execute(client, event, args);
return true;
}
client.once("sync", async function (state, prevState, data) {
switch (state) {
case "PREPARED":
client.name = client._store.users[client.credentials.userId].displayName;
client.log("[sdk:sync]", "connected:", client.name);
prefixes.push(client.name + ": ");
prefixes.push(client.name + " ");
client.initialized = true;
break;
}
});
client.on(sdk.RoomEvent.Timeline, async function (event, room, toStartOfTimeline) {
if (!client.initialized || toStartOfTimeline || event.getType() !== "m.room.message" || event.sender.userId == client.credentials.userId) {
return;
}
if(event.event.content["m.relates_to"] != null)
event.event.content.body = event.event.content.body.substring(event.event.content.body.indexOf("\n\n") + 2);
var content = event.event.content.body;
var isCommand = false;
for(const prefix of prefixes) {
if(content.startsWith(prefix)) {
isCommand = true;
content = content.substring(prefix.length);
break;
}
}
if(!isCommand) {
doModule(client, event);
} else {
var args = content.split(/\s/g);
var cmd = args.shift();
args = content.substring(cmd.length + 1);
if(!doCommand(client, event, cmd, args)) {
client.reply(event, `Command "${cmd}" not found.\nRun "${process.env.PREFIX}help" for a list of commands.`);
}
}
});
client.on("Room.myMembership", function (room, membership, prevMembership) {
if (membership === "invite") {
client.joinRoom(room.roomId).then(function () {
client.log("[client:autojoin] joined %s", room.roomId);
});
}
});
//await client.initCrypto();
await client.startClient();