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 { encode } from "html-entities";
import { JSDOM } from "jsdom";
import util from "util"; import util from "util";
import fs from "fs";
function execute(client, event, args) { function execute(client, event, args) {
var c = ""; var c = "";
@ -17,6 +20,6 @@ export default {
name: "eval", name: "eval",
usage: "<code>", usage: "<code>",
owner: true, owner: true,
desc: "Run JS code and reply with the result", desc: "run JS code and reply with the result",
execute execute
} }

View file

@ -9,7 +9,7 @@ function execute(client, event, args) {
if(args != "") { if(args != "") {
if(!command && !module) { 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; return;
} }

View file

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

View file

@ -1,7 +1,7 @@
import { exec } from "node:child_process"; import { exec } from "node:child_process";
function execute(client, event, args) { function execute(client, event, args) {
process.exit(255); process.exit(255);
} }
export default { 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 module = client.modules.filter(m=>m.name.toLowerCase()==name)[0];
var specific = command ?? module ?? false; var specific = command ?? module ?? false;
var config = client.cache.get(event.sender.roomId) ?? {}; var config = client.config.get(event.sender.roomId);
if(args != "") { if(args != "") {
if(!module) { if(!module) {
@ -15,23 +15,18 @@ function execute(client, event, args) {
return; return;
} }
if(config[module.name] !== undefined) config.set(module.name, !(config.get(module.name) ?? true));
config[module.name] = !config[module.name]; var state = (config.get(module.name) ? "En" : "Dis") + "abled";
else
config[module.name] = false;
var state = (config[module.name] ? "En" : "Dis") + "abled";
var reply = state + " " + module.name; var reply = state + " " + module.name;
var replyHTML = `<b>${state}</b> <code>${encode(module.name)}</code>`; var replyHTML = `<b>${state}</b> <code>${encode(module.name)}</code>`;
client.cache.set(event.sender.roomId, config);
client.reply(event, reply, replyHTML); client.reply(event, reply, replyHTML);
return; return;
} }
var modules = client.modules.map(m=>m.name); 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 reply = `enabled modules:\n${enabled.join(", ")}`;
var replyHTML = `enabled modules:<br><code>${enabled.join("</code>, <code>")}</code>`; 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 => { client.modules.forEach(m => {
if(!m) return; if(!m) return;
var config = client.cache.get(event.sender.roomId) ?? {}; var config = client.config.get(event.sender.roomId);
if(config[m.name] === false) return; 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; return true;
} }
command.execute(client, event, args); try {
command.execute(client, event, args);
} catch(err) {
client.log("[cmd]", err);
}
return true; return true;
} }

View file

@ -1,5 +1,5 @@
import { xxh64 } from "@node-rs/xxhash";
import { encode } from "html-entities"; import { encode } from "html-entities";
import forceSync from 'sync-rpc';
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
import util from "util"; import util from "util";
import fs from "fs"; 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) { client.reply = function(event, text, html) {
var content = event.getContent(); var content = event.getContent();
@ -86,8 +108,28 @@ export default function(client) {
client.error = client.log; 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) { 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) { if(!matrixUrl) {
const buffer = await fetch(url, { const buffer = await fetch(url, {
headers: { headers: {
@ -98,7 +140,7 @@ export default function(client) {
matrixUrl = uploadResponse.content_uri; matrixUrl = uploadResponse.content_uri;
client.cache.set(url, matrixUrl); client.cache.set(url, matrixUrl);
} }
return matrixUrl; return matrixUrl;*/
} }
if(fs.existsSync("data/cache.json")) { if(fs.existsSync("data/cache.json")) {

View file

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