mrmBot-Matrix/utils/misc.js

73 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-02-19 22:46:50 +00:00
const util = require("util");
2021-07-31 18:36:00 +00:00
const fs = require("fs");
2020-02-19 22:46:50 +00:00
2019-09-13 20:02:41 +00:00
// random(array) to select a random entry in array
exports.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)];
};
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
exports.clean = async (text) => {
2021-08-05 18:03:44 +00:00
if (text && text.constructor && text.constructor.name == "Promise")
2019-09-13 20:02:41 +00:00
text = await 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)}`);
const { parsed } = require("dotenv").config();
2021-07-31 18:36:00 +00:00
const imageServers = JSON.parse(fs.readFileSync("./servers.json", { encoding: "utf8" })).image;
2021-08-07 03:27:50 +00:00
for (const { server, auth } of imageServers) {
2021-07-31 18:36:00 +00:00
text = text.replaceAll(server, "<redacted>");
2021-08-07 03:27:50 +00:00
text = text.replaceAll(auth, "<redacted>");
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;
};
// regexEscape(string) to escape characters in a string for use in a regex
exports.regexEscape = (string) => {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
};
// decodeEntities(string)
exports.decodeEntities = (string) => {
var translate_re = /&(nbsp|amp|quot|lt|gt);/g;
var translate = {
"nbsp": " ",
"amp": "&",
"quot": "\"",
"lt": "<",
"gt": ">"
};
return string.replace(translate_re, function(match, entity) {
return translate[entity];
}).replace(/&#(\d+);/gi, function(match, numStr) {
var num = parseInt(numStr, 10);
return String.fromCharCode(num);
});
};
2019-09-13 20:02:41 +00:00
// define defaults for prefixes and tags
exports.defaults = {
prefix: process.env.PREFIX
2019-09-13 20:02:41 +00:00
};
exports.tagDefaults = {
help: {
content: "https://projectlounge.pw/esmBot/help.html",
2019-09-13 20:02:41 +00:00
author: "198198681982205953"
}
};