2021-08-19 14:19:14 +00:00
|
|
|
import util from "util";
|
|
|
|
import fs from "fs";
|
|
|
|
import { config } from "dotenv";
|
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
|
2021-08-19 14:19:14 +00:00
|
|
|
export function random(array) {
|
2021-04-23 20:03:48 +00:00
|
|
|
if (!array || array.length < 1) return null;
|
2019-09-13 20:02:41 +00:00
|
|
|
return array[Math.floor(Math.random() * array.length)];
|
2021-08-19 14:19:14 +00:00
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
|
2020-09-05 21:33:21 +00:00
|
|
|
const optionalReplace = (token) => {
|
2021-05-18 00:09:30 +00:00
|
|
|
return token === undefined || token === "" ? "" : (token === "true" || token === "false" ? token : "<redacted>");
|
2020-09-05 21:33:21 +00:00
|
|
|
};
|
|
|
|
|
2019-09-13 20:02:41 +00:00
|
|
|
// clean(text) to clean message of any private info or mentions
|
2021-08-19 14:19:14 +00:00
|
|
|
export async function clean(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;
|
2019-11-13 00:09:06 +00:00
|
|
|
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
|
2021-01-18 20:11:28 +00:00
|
|
|
.replaceAll("`", `\`${String.fromCharCode(8203)}`)
|
2021-04-14 21:37:40 +00:00
|
|
|
.replaceAll("@", `@${String.fromCharCode(8203)}`);
|
|
|
|
|
2021-08-19 14:19:14 +00:00
|
|
|
const { parsed } = 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-12-18 04:44:53 +00:00
|
|
|
text = optionalReplace(server);
|
|
|
|
text = optionalReplace(auth);
|
2021-07-31 18:36:00 +00:00
|
|
|
}
|
2021-04-14 21:37:40 +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;
|
2021-08-19 14:19:14 +00:00
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
|
|
|
|
// regexEscape(string) to escape characters in a string for use in a regex
|
2021-08-19 14:19:14 +00:00
|
|
|
export function regexEscape(string) {
|
2019-09-13 20:02:41 +00:00
|
|
|
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
2021-08-19 14:19:14 +00:00
|
|
|
}
|
2019-09-13 20:02:41 +00:00
|
|
|
|
2020-02-06 15:00:10 +00:00
|
|
|
// decodeEntities(string)
|
2021-08-19 14:19:14 +00:00
|
|
|
export function decodeEntities(string) {
|
2020-02-06 15:00:10 +00:00
|
|
|
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);
|
|
|
|
});
|
2021-08-19 14:19:14 +00:00
|
|
|
}
|