change how configs are handled

This commit is contained in:
Ashley 2024-08-27 19:52:32 +00:00
parent 328c817136
commit cca482079c
8 changed files with 96 additions and 43 deletions

View file

@ -1,5 +1,8 @@
import { xxh64 } from "@node-rs/xxhash";
import { encode } from "html-entities";
import { JSDOM } from "jsdom";
import util from "util";
import fs from "fs";
function execute(client, event, args) {
var c = "";
@ -17,6 +20,6 @@ export default {
name: "eval",
usage: "<code>",
owner: true,
desc: "Run JS code and reply with the result",
desc: "run JS code and reply with the result",
execute
}

View file

@ -9,7 +9,7 @@ function execute(client, event, args) {
if(args != "") {
if(!command && !module) {
client.reply(event, `Section "${args}" not found.\nRun "${process.env.PREFIX}help" for a list of commands and modules.`);
client.reply(event, `section "${args}" not found.\nrun "${process.env.PREFIX}help" for a list of commands and modules.`);
return;
}

View file

@ -1,9 +1,9 @@
function execute(client, event, args) {
var info = `Source code: https://git.lgbt/root/possumbot\n`;
info += `Commands: ${client.commands.length}\n`;
info += `Modules: ${client.modules.length}\n`;
info += `Prefix: ${process.env.PREFIX} (you can also mention the bot!)\n`;
info += `Owner: ${process.env.OWNER_ID}`;
var info = `source code: https://git.lgbt/root/possumbot\n`;
info += `commands: ${client.commands.length}\n`;
info += `modules: ${client.modules.length}\n`;
info += `prefix: ${process.env.PREFIX} (you can also mention the bot!)\n`;
info += `owner: ${process.env.OWNER_ID}`;
client.reply(event, info);
}

View file

@ -1,7 +1,7 @@
import { exec } from "node:child_process";
function execute(client, event, args) {
process.exit(255);
process.exit(255);
}
export default {

View file

@ -7,7 +7,7 @@ function execute(client, event, args) {
var module = client.modules.filter(m=>m.name.toLowerCase()==name)[0];
var specific = command ?? module ?? false;
var config = client.cache.get(event.sender.roomId) ?? {};
var config = client.config.get(event.sender.roomId);
if(args != "") {
if(!module) {
@ -15,23 +15,18 @@ function execute(client, event, args) {
return;
}
if(config[module.name] !== undefined)
config[module.name] = !config[module.name];
else
config[module.name] = false;
var state = (config[module.name] ? "En" : "Dis") + "abled";
config.set(module.name, !(config.get(module.name) ?? true));
var state = (config.get(module.name) ? "En" : "Dis") + "abled";
var reply = state + " " + module.name;
var replyHTML = `<b>${state}</b> <code>${encode(module.name)}</code>`;
client.cache.set(event.sender.roomId, config);
client.reply(event, reply, replyHTML);
return;
}
var modules = client.modules.map(m=>m.name);
var enabled = modules.filter(m=>config[m]!==false);
var enabled = modules.filter(m=>config.get(m)!==false);
var reply = `enabled modules:\n${enabled.join(", ")}`;
var replyHTML = `enabled modules:<br><code>${enabled.join("</code>, <code>")}</code>`;

View file

@ -64,10 +64,14 @@ function doModule(client, event) {
client.modules.forEach(m => {
if(!m) return;
var config = client.cache.get(event.sender.roomId) ?? {};
if(config[m.name] === false) return;
var config = client.config.get(event.sender.roomId);
if(config.get(m.name) === false) return;
m.hooks?.message(client, event);
try {
m.hooks?.message(client, event);
} catch(err) {
client.log("[hook]", err);
}
});
}
@ -83,7 +87,11 @@ function doCommand(client, event, cmd, args) {
return true;
}
command.execute(client, event, args);
try {
command.execute(client, event, args);
} catch(err) {
client.log("[cmd]", err);
}
return true;
}

View file

@ -1,5 +1,5 @@
import { xxh64 } from "@node-rs/xxhash";
import { encode } from "html-entities";
import forceSync from 'sync-rpc';
import { JSDOM } from "jsdom";
import util from "util";
import fs from "fs";
@ -22,6 +22,28 @@ export default function(client) {
}
}
client.config = {
get: (key) => {
var configFile = "data/" + xxh64(key).toString(16) + ".json";
var configData = {};
if(fs.existsSync(configFile)) {
configData = JSON.parse(fs.readFileSync(configFile));
}
var configObject = {
get: (key) => {
return configData[key] ?? null;
},
set: (key, value) => {
configData[key] = value;
fs.writeFileSync(configFile, JSON.stringify(configData));
}
};
return configObject;
}
}
client.reply = function(event, text, html) {
var content = event.getContent();
@ -86,8 +108,28 @@ export default function(client) {
client.error = client.log;
client.uploadBuffer = async function(buffer) {
var hash = xxh64(buffer).toString(16);
var matrixUrl = client.cache.get(hash);
if(!matrixUrl) {
const uploadResponse = await client.uploadContent(buffer, { rawResponse: false });
matrixUrl = uploadResponse.content_uri;
client.cache.set(hash, matrixUrl);
}
return matrixUrl;
}
client.uploadMedia = async function(url) {
var matrixUrl = client.cache.get(url);
const buffer = await fetch(url, {
headers: {
"User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)"
}
}).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf));
return client.uploadBuffer(buffer);
/* var matrixUrl = client.cache.get(url);
if(!matrixUrl) {
const buffer = await fetch(url, {
headers: {
@ -98,7 +140,7 @@ export default function(client) {
matrixUrl = uploadResponse.content_uri;
client.cache.set(url, matrixUrl);
}
return matrixUrl;
return matrixUrl;*/
}
if(fs.existsSync("data/cache.json")) {

View file

@ -1,22 +1,27 @@
{
"name": "possumbot",
"type": "module",
"version": "0.0.0",
"description": "General purpose Matrix bot",
"main": "index.js",
"author": "Ashley Graves",
"license": "GPL-v3",
"dependencies": {
"@matrix-org/olm": "^3.2.15",
"@peertube/http-signature": "^1.7.0",
"cli-color": "^2.0.4",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"html-entities": "^2.5.2",
"jsdom": "^24.1.1",
"matrix-js-sdk": "^32.0.0",
"node-fetch": "^3.3.2",
"node-localstorage": "^3.0.5",
"sync-rpc": "^1.3.6"
"name": "possumbot",
"type": "module",
"version": "0.0.0",
"description": "General purpose Matrix bot",
"main": "index.js",
"author": "Ashley Graves",
"license": "GPL-v3",
"dependencies": {
"@matrix-org/olm": "^3.2.15",
"@node-rs/xxhash": "^1.7.3",
"@peertube/http-signature": "^1.7.0",
"cli-color": "^2.0.4",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"html-entities": "^2.5.2",
"jsdom": "^25.0.0",
"matrix-js-sdk": "^32.4.0",
"node-fetch": "^3.3.2",
"node-localstorage": "^3.0.5"
},
"pnpm": {
"overrides": {
"jsdom>tough-cookie": "^5.0.0-rc.4"
}
}
}