possumbot/lib/ext.js

163 lines
4.6 KiB
JavaScript
Raw Normal View History

2024-08-27 21:52:32 +02:00
import { xxh64 } from "@node-rs/xxhash";
2024-08-21 17:00:40 +02:00
import { encode } from "html-entities";
import { JSDOM } from "jsdom";
import util from "util";
import fs from "fs";
Array.prototype.random = function(){return this[Math.floor(Math.random() * this.length)]}
var cache = {};
var log = null;
export default function(client) {
client.logEvent = null;
client.cache = {
get: (key) => {
return cache[key] ?? null;
},
set: (key, value) => {
cache[key] = value;
fs.writeFileSync("data/cache.json", JSON.stringify(cache));
}
}
2024-08-27 21:52:32 +02:00
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;
}
}
2024-08-21 17:00:40 +02:00
client.reply = function(event, text, html) {
var content = event.getContent();
2024-08-21 17:00:40 +02:00
var mcontent = {
2024-08-27 17:18:30 +02:00
format: "org.matrix.custom.html",
formatted_body: "<mx-reply><blockquote><a href=\"https://m.posm.gay/#/" + event.event.room_id + "/" + event.event.event_id + "?via=possum.city\">In reply to</a> <a href=\"https://m.posm.gay/#/" + event.sender.userId + "\">" + event.sender.userId + "</a><br>" + (content.formatted_body ?? content.body) + "</blockquote></mx-reply>" + (html ?? text).trim(),
body: "> <" + event.sender.userId + "> " + content.body.split("\n")[0] + "\n\n" + text.trim(),
2024-08-21 17:00:40 +02:00
msgtype: "m.notice",
"m.relates_to": {
"m.in_reply_to": {
"event_id": event.event.event_id
}
}
};
2024-08-21 17:00:40 +02:00
client.sendEvent(event.event.room_id, "m.room.message", mcontent, "", (err, res) => {
console.log(err);
});
}
client.logData = [];
client.startup = new Date();
client.updateLog = async function() {
if(log && (log.body == client.logData.join("\n") || log["m.new_content"]?.body == client.logData.join("\n"))) {
setTimeout(client.updateLog, 1000);
return;
}
var mcontent = {
format: "org.matrix.custom.html",
formatted_body: `<pre><code>[client:logger] started: ${client.startup}\n${encode(client.logData.join("\n"))}</code></pre>`,
body: client.logData.join("\n"),
msgtype: "m.notice"
}
if(!client.logEvent) {
log = mcontent;
} else {
log["m.new_content"] = mcontent;
log["m.relates_to"] = {
rel_type: "m.replace",
event_id: client.logEvent
}
}
var x = await client.sendEvent(process.env.LOG_CHANNEL, "m.room.message", log, "", (err, res) => {
console.log("[client:log]", res);
console.error(err);
});
client.logEvent = client.logEvent ?? x.event_id;
setTimeout(client.updateLog, 1000);
}
client.log = function() {
var data = util.format.apply(null, arguments);
client.logData.push(data);
console.log(data);
}
client.error = client.log;
2024-08-27 21:52:32 +02:00
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;
}
2024-08-22 17:34:03 +02:00
client.uploadMedia = async function(url) {
2024-08-27 21:52:32 +02:00
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);
2024-08-22 17:34:03 +02:00
if(!matrixUrl) {
const buffer = await fetch(url, {
headers: {
"User-Agent": "PossumBot/1.0 (+https://bot.possum.city/)",
},
}).then((res) => res.arrayBuffer()).then((buf) => Buffer.from(buf));
const uploadResponse = await client.uploadContent(buffer, { rawResponse: false });
matrixUrl = uploadResponse.content_uri;
client.cache.set(url, matrixUrl);
}
2024-08-27 21:52:32 +02:00
return matrixUrl;*/
2024-08-22 17:34:03 +02:00
}
2024-08-21 17:00:40 +02:00
if(fs.existsSync("data/cache.json")) {
var data = fs.readFileSync("data/cache.json");
try {
cache = JSON.parse(data);
client.log("[sdkext:cache]", 'loaded', Object.keys(cache).length, 'cache entries!');
} catch(err) {
client.error("[sdkext:cache]", err);
}
}
process.on('uncaughtException', function (err) {
console.error('[core:err]', err);
});
client.updateLog();
}