diff --git a/commands/eval.js b/commands/eval.js index 9833c60..d2b99af 100644 --- a/commands/eval.js +++ b/commands/eval.js @@ -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: "", owner: true, - desc: "Run JS code and reply with the result", + desc: "run JS code and reply with the result", execute } diff --git a/commands/help.js b/commands/help.js index 56df75b..5dc7ee7 100644 --- a/commands/help.js +++ b/commands/help.js @@ -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; } diff --git a/commands/info.js b/commands/info.js index 93e823f..8caed69 100644 --- a/commands/info.js +++ b/commands/info.js @@ -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); } diff --git a/commands/restart.js b/commands/restart.js index 4ab9f04..491d7f3 100644 --- a/commands/restart.js +++ b/commands/restart.js @@ -1,7 +1,7 @@ import { exec } from "node:child_process"; function execute(client, event, args) { - process.exit(255); + process.exit(255); } export default { diff --git a/commands/toggle.js b/commands/toggle.js index 791b19c..05b79a5 100644 --- a/commands/toggle.js +++ b/commands/toggle.js @@ -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 = `${state} ${encode(module.name)}`; - 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:
${enabled.join(", ")}`; diff --git a/index.js b/index.js index 607b76a..2f45b12 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/lib/ext.js b/lib/ext.js index a3d6000..76f19d5 100644 --- a/lib/ext.js +++ b/lib/ext.js @@ -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")) { diff --git a/package.json b/package.json index 4f2e82c..95ceab3 100644 --- a/package.json +++ b/package.json @@ -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" } + } }