mrmBot-Matrix/utils/misc.js
Essem 40223ec8b5
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively

* Sort commands

* Missed a couple of spots

* missed even more spots apparently

* Ported commands in "fun" category to new class-based format, added babel eslint plugin

* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of

* Missed a spot

* Removed unnecessary abort-controller package, add deprecation warning for mongo database

* Added imagereload, clarified premature end message

* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts

* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner

* Converted music/soundboard commands to class format

* Cleanup unnecessary logs

* awful tag command class port

* I literally somehow just learned that you can leave out the constructor in classes

* Pass client directly to commands/events, cleaned up command handler

* Migrated bot to eris-sharder, fixed some error handling stuff

* Remove unused modules

* Fixed type returning

* Switched back to Eris stable

* Some fixes and cleanup

* might wanna correct this

* Implement image command ratelimiting

* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 11:16:12 -05:00

68 lines
2.3 KiB
JavaScript

const util = require("util");
// random(array) to select a random entry in array
exports.random = (array) => {
return array[Math.floor(Math.random() * array.length)];
};
const optionalReplace = (token) => {
return token === "" ? "" : "<redacted>";
};
// clean(text) to clean message of any private info or mentions
exports.clean = async (text) => {
if (text && text.constructor.name == "Promise")
text = await text;
if (typeof text !== "string")
text = util.inspect(text, { depth: 1 });
text = text
.replaceAll("`", `\`${String.fromCharCode(8203)}`)
.replaceAll("@", `@${String.fromCharCode(8203)}`)
.replaceAll(process.env.TOKEN, optionalReplace(process.env.TOKEN))
.replaceAll(process.env.MASHAPE, optionalReplace(process.env.MASHAPE))
.replaceAll(process.env.CAT, optionalReplace(process.env.CAT))
.replaceAll(process.env.GOOGLE, optionalReplace(process.env.GOOGLE))
.replaceAll(process.env.DBL, optionalReplace(process.env.DBL))
.replaceAll(process.env.MONGO, optionalReplace(process.env.MONGO))
.replaceAll(process.env.TWITTER_KEY, optionalReplace(process.env.TWITTER_KEY))
.replaceAll(process.env.CONSUMER_SECRET, optionalReplace(process.env.CONSUMER_SECRET))
.replaceAll(process.env.ACCESS_TOKEN, optionalReplace(process.env.ACCESS_TOKEN))
.replaceAll(process.env.ACCESS_SECRET, optionalReplace(process.env.ACCESS_SECRET));
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);
});
};
// define defaults for prefixes and tags
exports.defaults = {
prefix: process.env.PREFIX
};
exports.tagDefaults = {
help: {
content: "https://projectlounge.pw/esmBot/help.html",
author: "198198681982205953"
}
};