2024-08-22 17:34:03 +02:00
|
|
|
import apfetch from "./auth-fetch.js";
|
2024-08-21 17:00:40 +02:00
|
|
|
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;
|
2024-08-22 18:22:52 +02:00
|
|
|
|
|
|
|
var config = client.cache.get(event.sender.roomId) ?? {};
|
|
|
|
if(config[m.name] === false) return;
|
|
|
|
|
2024-08-21 17:00:40 +02:00
|
|
|
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;
|
|
|
|
|
2024-08-25 15:05:48 +02:00
|
|
|
if((command.owner && event.sender.userId != process.env.OWNER_ID) || (command.admin && event.sender.powerLevel < 100 && event.sender.userId != process.env.OWNER_ID)) {
|
2024-08-22 18:22:52 +02:00
|
|
|
var addl = `Are you sure this command is for you?\nYour power level is ${event.sender.powerLevel}`;
|
2024-08-25 15:08:10 +02:00
|
|
|
client.reply(event, addl, '<img src="mxc://possum.city/b4Vo1BTcq49B7TbFWCqq76HQWQEdNIqq" alt="nuh uh" /><br>' + addl);
|
2024-08-21 17:00:40 +02:00
|
|
|
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);
|
|
|
|
|
2024-08-22 18:22:52 +02:00
|
|
|
event.sender = client.getRoom(event.sender.roomId).getMember(event.getSender());
|
2024-08-21 17:00:40 +02:00
|
|
|
var content = event.event.content.body;
|
|
|
|
var isCommand = false;
|
2024-08-25 15:08:10 +02:00
|
|
|
|
2024-08-21 17:00:40 +02:00
|
|
|
for(const prefix of prefixes) {
|
|
|
|
if(content.startsWith(prefix)) {
|
|
|
|
isCommand = true;
|
|
|
|
content = content.substring(prefix.length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-25 15:08:10 +02:00
|
|
|
|
2024-08-21 17:00:40 +02:00
|
|
|
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();
|