possumbot/lib/ext.js
2024-08-27 15:18:30 +00:00

120 lines
3.5 KiB
JavaScript

import { encode } from "html-entities";
import forceSync from 'sync-rpc';
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));
}
}
client.reply = function(event, text, html) {
var content = event.getContent();
var mcontent = {
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(),
msgtype: "m.notice",
"m.relates_to": {
"m.in_reply_to": {
"event_id": event.event.event_id
}
}
};
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;
client.uploadMedia = async function(url) {
var matrixUrl = client.cache.get(url);
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);
}
return matrixUrl;
}
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();
}