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,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")) {