mrmBot-Matrix/utils/misc.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

import util from "util";
import fs from "fs";
import pm2 from "pm2";
import { config } from "dotenv";
2020-02-19 22:46:50 +00:00
// playing messages
const { messages } = JSON.parse(fs.readFileSync(new URL("../config/messages.json", import.meta.url)));
const { types } = JSON.parse(fs.readFileSync(new URL("../config/commands.json", import.meta.url)));
let broadcast = false;
2019-09-13 20:02:41 +00:00
// random(array) to select a random entry in array
export function random(array) {
if (!array || array.length < 1) return null;
2019-09-13 20:02:41 +00:00
return array[Math.floor(Math.random() * array.length)];
}
2019-09-13 20:02:41 +00:00
const optionalReplace = (token) => {
return token === undefined || token === "" ? "" : (token === "true" || token === "false" ? token : "<redacted>");
};
2019-09-13 20:02:41 +00:00
// clean(text) to clean message of any private info or mentions
2022-09-11 04:18:44 +00:00
export function clean(text) {
if (typeof text !== "string")
2020-02-19 22:46:50 +00:00
text = util.inspect(text, { depth: 1 });
2019-09-13 20:02:41 +00:00
text = text
.replaceAll("`", `\`${String.fromCharCode(8203)}`)
.replaceAll("@", `@${String.fromCharCode(8203)}`);
let { parsed } = config();
if (!parsed) parsed = process.env;
2022-07-26 15:38:42 +00:00
const imageServers = JSON.parse(fs.readFileSync(new URL("../config/servers.json", import.meta.url), { encoding: "utf8" })).image;
2021-07-31 18:36:00 +00:00
if (imageServers?.length !== 0) {
for (const { server, auth } of imageServers) {
text = text.replaceAll(server, optionalReplace(server));
text = text.replaceAll(auth, optionalReplace(auth));
}
2021-07-31 18:36:00 +00:00
}
for (const env of Object.keys(parsed)) {
text = text.replaceAll(parsed[env], optionalReplace(parsed[env]));
}
2019-09-13 20:02:41 +00:00
return text;
}
2019-09-13 20:02:41 +00:00
2022-09-11 04:48:44 +00:00
// textEncode(string) to encode characters for image processing
export function textEncode(string) {
return string.replaceAll("&", "&amp;").replaceAll(">", "&gt;").replaceAll("<", "&lt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;").replaceAll("\\n", "\n").replaceAll("\\:", ":");
}
// set activity (a.k.a. the gamer code)
export function activityChanger(bot) {
if (!broadcast) {
bot.editStatus("dnd", {
name: random(messages) + (types.classic ? ` | @${bot.user.username} help` : ""),
});
}
setTimeout(() => activityChanger(bot), 900000);
}
export function checkBroadcast(bot) {
/*if () {
startBroadcast(bot, message);
}*/
}
export function startBroadcast(bot, message) {
bot.editStatus("dnd", {
name: message + (types.classic ? ` | @${bot.user.username} help` : ""),
});
broadcast = true;
}
export function endBroadcast(bot) {
bot.editStatus("dnd", {
name: random(messages) + (types.classic ? ` | @${bot.user.username} help` : ""),
});
broadcast = false;
}
export function getServers() {
return new Promise((resolve, reject) => {
if (process.env.PM2_USAGE) {
pm2.launchBus((err, pm2Bus) => {
const listener = (packet) => {
if (packet.data?.type === "countResponse") {
resolve(packet.data.serverCount);
pm2Bus.off("process:msg");
}
};
pm2Bus.on("process:msg", listener);
});
pm2.sendDataToProcessId(0, {
id: 0,
type: "process:msg",
data: {
type: "getCount"
},
topic: true
}, (err) => {
if (err) reject(err);
});
} else {
resolve(0);
}
});
2022-09-11 04:48:44 +00:00
}