2021-08-19 14:19:14 +00:00
|
|
|
import util from "util";
|
|
|
|
import fs from "fs";
|
2022-09-21 05:05:03 +00:00
|
|
|
import pm2 from "pm2";
|
2021-08-19 14:19:14 +00:00
|
|
|
import { config } from "dotenv";
|
2022-10-25 17:37:55 +00:00
|
|
|
import db from "./database.js";
|
2020-02-19 22:46:50 +00:00
|
|
|
|
2022-09-21 05:05:03 +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
|
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
|
2022-09-11 04:18:44 +00:00
|
|
|
export function clean(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)}`);
|
|
|
|
|
2022-09-23 22:24:28 +00:00
|
|
|
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
|
|
|
|
2022-07-23 21:02:04 +00:00
|
|
|
if (imageServers?.length !== 0) {
|
2022-01-08 19:06:35 +00:00
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2022-09-11 04:48:44 +00:00
|
|
|
// textEncode(string) to encode characters for image processing
|
|
|
|
export function textEncode(string) {
|
2022-12-19 01:19:53 +00:00
|
|
|
return string.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("\\n", "\n").replaceAll("\\:", ":").replaceAll("\\,", ",");
|
2022-09-21 05:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set activity (a.k.a. the gamer code)
|
2022-09-24 03:25:16 +00:00
|
|
|
export async function activityChanger(bot) {
|
2022-09-21 05:05:03 +00:00
|
|
|
if (!broadcast) {
|
2022-09-24 03:25:16 +00:00
|
|
|
await bot.editStatus("dnd", [{
|
2022-09-24 16:26:56 +00:00
|
|
|
type: 0,
|
|
|
|
name: random(messages) + (types.classic ? ` | @${bot.user.username} help` : "")
|
2022-09-24 03:25:16 +00:00
|
|
|
}]);
|
2022-09-21 05:05:03 +00:00
|
|
|
}
|
|
|
|
setTimeout(() => activityChanger(bot), 900000);
|
|
|
|
}
|
|
|
|
|
2022-10-25 17:37:55 +00:00
|
|
|
export async function checkBroadcast(bot) {
|
2022-12-12 17:15:10 +00:00
|
|
|
if (!db) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-25 17:37:55 +00:00
|
|
|
const message = await db.getBroadcast();
|
|
|
|
if (message) {
|
2022-09-21 05:05:03 +00:00
|
|
|
startBroadcast(bot, message);
|
2022-10-25 17:37:55 +00:00
|
|
|
}
|
2022-09-21 05:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function startBroadcast(bot, message) {
|
2022-09-24 03:25:16 +00:00
|
|
|
bot.editStatus("dnd", [{
|
2022-09-24 16:26:56 +00:00
|
|
|
type: 0,
|
|
|
|
name: message + (types.classic ? ` | @${bot.user.username} help` : "")
|
2022-09-24 03:25:16 +00:00
|
|
|
}]);
|
2022-09-21 05:05:03 +00:00
|
|
|
broadcast = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function endBroadcast(bot) {
|
2022-09-24 03:25:16 +00:00
|
|
|
bot.editStatus("dnd", [{
|
2022-09-24 16:26:56 +00:00
|
|
|
type: 0,
|
|
|
|
name: random(messages) + (types.classic ? ` | @${bot.user.username} help` : "")
|
2022-09-24 03:25:16 +00:00
|
|
|
}]);
|
2022-09-21 05:05:03 +00:00
|
|
|
broadcast = false;
|
|
|
|
}
|
|
|
|
|
2022-09-23 22:41:28 +00:00
|
|
|
export function getServers(bot) {
|
2022-09-21 05:05:03 +00:00
|
|
|
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);
|
|
|
|
});
|
2022-10-31 04:16:03 +00:00
|
|
|
pm2.list((err, list) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const managerProc = list.filter((v) => v.name === "esmBot-manager")[0];
|
|
|
|
pm2.sendDataToProcessId(managerProc.pm_id, {
|
|
|
|
id: managerProc.pm_id,
|
|
|
|
type: "process:msg",
|
|
|
|
data: {
|
|
|
|
type: "getCount"
|
|
|
|
},
|
|
|
|
topic: true
|
|
|
|
}, (err) => {
|
|
|
|
if (err) reject(err);
|
|
|
|
});
|
2022-09-21 05:05:03 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-09-23 22:41:28 +00:00
|
|
|
resolve(bot.guilds.size);
|
2022-09-21 05:05:03 +00:00
|
|
|
}
|
|
|
|
});
|
2022-09-24 03:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// copied from eris
|
2022-10-24 01:27:32 +00:00
|
|
|
export function cleanMessage(message, content) {
|
|
|
|
let cleanContent = content && content.replace(/<a?(:\w+:)[0-9]+>/g, "$1") || "";
|
2022-09-24 03:25:16 +00:00
|
|
|
|
2022-10-25 17:37:55 +00:00
|
|
|
const author = message.author ?? message.member ?? message.user;
|
2022-10-24 22:12:10 +00:00
|
|
|
let authorName = author.username;
|
2022-10-24 01:27:32 +00:00
|
|
|
if (message.member?.nick) {
|
|
|
|
authorName = message.member.nick;
|
2022-09-24 03:25:16 +00:00
|
|
|
}
|
2022-10-24 22:12:10 +00:00
|
|
|
cleanContent = cleanContent.replace(new RegExp(`<@!?${author.id}>`, "g"), `@${authorName}`);
|
2022-09-24 03:25:16 +00:00
|
|
|
|
|
|
|
if (message.mentions) {
|
|
|
|
for (const mention of message.mentions.members) {
|
2022-10-24 01:27:32 +00:00
|
|
|
if (mention.nick) {
|
|
|
|
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.nick}`);
|
2022-09-24 03:25:16 +00:00
|
|
|
}
|
2022-10-24 01:27:32 +00:00
|
|
|
cleanContent = cleanContent.replace(new RegExp(`<@!?${mention.id}>`, "g"), `@${mention.username}`);
|
2022-09-24 03:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.guildID && message.mentions.roles) {
|
|
|
|
for (const roleID of message.mentions.roles) {
|
|
|
|
const role = message.guild.roles.get(roleID);
|
|
|
|
const roleName = role ? role.name : "deleted-role";
|
2022-10-24 01:27:32 +00:00
|
|
|
cleanContent = cleanContent.replace(new RegExp(`<@&${roleID}>`, "g"), `@${roleName}`);
|
2022-09-24 03:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const id of message.mentions.channels) {
|
|
|
|
const channel = message.client.getChannel(id);
|
|
|
|
if (channel && channel.name && channel.mention) {
|
|
|
|
cleanContent = cleanContent.replace(channel.mention, `#${channel.name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 01:27:32 +00:00
|
|
|
return textEncode(cleanContent);
|
2022-09-11 04:48:44 +00:00
|
|
|
}
|