2020-09-16 22:32:43 +03:00
|
|
|
import "./loadEnv";
|
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
import path from "path";
|
|
|
|
import yaml from "js-yaml";
|
|
|
|
|
2019-01-15 01:07:02 +02:00
|
|
|
import fs from "fs";
|
2020-07-22 22:56:21 +03:00
|
|
|
import { Knub, PluginError } from "knub";
|
2018-12-14 06:27:41 +02:00
|
|
|
import { SimpleError } from "./SimpleError";
|
|
|
|
|
2019-06-22 18:52:24 +03:00
|
|
|
import { Configs } from "./data/Configs";
|
2020-07-22 22:56:21 +03:00
|
|
|
// Always use UTC internally
|
|
|
|
// This is also enforced for the database in data/db.ts
|
|
|
|
import moment from "moment-timezone";
|
2020-12-17 03:51:59 +02:00
|
|
|
import { Client, DiscordHTTPError, TextChannel } from "eris";
|
2020-07-22 22:56:21 +03:00
|
|
|
import { connect } from "./data/db";
|
2020-07-30 22:23:18 +03:00
|
|
|
import { baseGuildPlugins, globalPlugins, guildPlugins } from "./plugins/availablePlugins";
|
2020-07-31 11:16:07 +03:00
|
|
|
import { errorMessage, isDiscordHTTPError, isDiscordRESTError, MINUTES, successMessage } from "./utils";
|
2020-07-22 22:56:21 +03:00
|
|
|
import { startUptimeCounter } from "./uptime";
|
|
|
|
import { AllowedGuilds } from "./data/AllowedGuilds";
|
2020-08-10 00:24:06 +03:00
|
|
|
import { ZeppelinGlobalConfig, ZeppelinGuildConfig } from "./types";
|
2020-07-22 22:56:21 +03:00
|
|
|
import { RecoverablePluginError } from "./RecoverablePluginError";
|
|
|
|
import { GuildLogs } from "./data/GuildLogs";
|
|
|
|
import { LogType } from "./data/LogType";
|
|
|
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
|
|
|
import { logger } from "./logger";
|
2020-07-30 20:40:00 +03:00
|
|
|
import { PluginLoadError } from "knub/dist/plugins/PluginLoadError";
|
2020-07-22 22:56:21 +03:00
|
|
|
|
|
|
|
const fsp = fs.promises;
|
2019-06-22 18:52:24 +03:00
|
|
|
|
2020-09-16 22:32:43 +03:00
|
|
|
if (!process.env.KEY) {
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
console.error("Project root .env with KEY is required!");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2020-07-06 01:53:58 +03:00
|
|
|
declare global {
|
|
|
|
// This is here so TypeScript doesn't give an error when importing twemoji
|
|
|
|
// since one of the signatures of twemoji.parse() takes an HTMLElement but
|
|
|
|
// we're not in a browser environment so including the DOM lib would not make
|
|
|
|
// sense
|
|
|
|
type HTMLElement = unknown;
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:54:16 +03:00
|
|
|
// Error handling
|
2019-01-15 01:05:12 +02:00
|
|
|
let recentPluginErrors = 0;
|
2019-04-23 05:29:53 +03:00
|
|
|
const RECENT_PLUGIN_ERROR_EXIT_THRESHOLD = 5;
|
2019-01-13 16:52:00 +02:00
|
|
|
|
2019-04-23 05:29:53 +03:00
|
|
|
let recentDiscordErrors = 0;
|
|
|
|
const RECENT_DISCORD_ERROR_EXIT_THRESHOLD = 5;
|
2019-01-15 01:05:12 +02:00
|
|
|
|
2019-04-23 05:29:53 +03:00
|
|
|
setInterval(() => (recentPluginErrors = Math.max(0, recentPluginErrors - 1)), 2500);
|
|
|
|
setInterval(() => (recentDiscordErrors = Math.max(0, recentDiscordErrors - 1)), 2500);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2020-01-12 10:28:38 +02:00
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
|
const errorHandler = err => {
|
2020-09-13 22:46:59 +03:00
|
|
|
const guildName = err.guild?.name || "Global";
|
|
|
|
const guildId = err.guild?.id || "0";
|
|
|
|
|
2020-01-29 02:44:11 +02:00
|
|
|
if (err instanceof RecoverablePluginError) {
|
|
|
|
// Recoverable plugin errors can be, well, recovered from.
|
|
|
|
// Log it in the console as a warning and post a warning to the guild's log.
|
|
|
|
|
|
|
|
// tslint:disable:no-console
|
2020-09-13 22:46:59 +03:00
|
|
|
console.warn(`${guildName}: [${err.code}] ${err.message}`);
|
2020-01-29 02:44:11 +02:00
|
|
|
|
2020-09-13 22:46:59 +03:00
|
|
|
if (err.guild) {
|
|
|
|
const logs = new GuildLogs(err.guild.id);
|
|
|
|
logs.log(LogType.BOT_ALERT, { body: `\`[${err.code}]\` ${err.message}` });
|
|
|
|
}
|
2020-01-29 02:44:11 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:40:00 +03:00
|
|
|
if (err instanceof PluginLoadError) {
|
|
|
|
// tslint:disable:no-console
|
2020-09-13 22:46:59 +03:00
|
|
|
console.warn(`${guildName} (${guildId}): Failed to load plugin '${err.pluginName}': ${err.message}`);
|
2020-07-30 20:40:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-17 03:51:59 +02:00
|
|
|
if (err instanceof DiscordHTTPError && err.code === 500) {
|
|
|
|
// Don't need stack traces on HTTP 500 errors
|
|
|
|
console.error(err.message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-12 10:28:38 +02:00
|
|
|
// tslint:disable:no-console
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
if (err instanceof PluginError) {
|
|
|
|
// Tolerate a few recent plugin errors before crashing
|
|
|
|
if (++recentPluginErrors >= RECENT_PLUGIN_ERROR_EXIT_THRESHOLD) {
|
|
|
|
console.error(`Exiting after ${RECENT_PLUGIN_ERROR_EXIT_THRESHOLD} plugin errors`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-05-08 18:29:17 +03:00
|
|
|
} else if (isDiscordRESTError(err) || isDiscordHTTPError(err)) {
|
2020-01-12 10:28:38 +02:00
|
|
|
// Discord API errors, usually safe to just log instead of crash
|
|
|
|
// We still bail if we get a ton of them in a short amount of time
|
|
|
|
if (++recentDiscordErrors >= RECENT_DISCORD_ERROR_EXIT_THRESHOLD) {
|
|
|
|
console.error(`Exiting after ${RECENT_DISCORD_ERROR_EXIT_THRESHOLD} API errors`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// On other errors, crash immediately
|
2019-04-23 05:38:48 +03:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-01-12 10:28:38 +02:00
|
|
|
// tslint:enable:no-console
|
|
|
|
};
|
2019-04-23 05:29:53 +03:00
|
|
|
|
2020-01-12 10:28:38 +02:00
|
|
|
process.on("uncaughtException", errorHandler);
|
|
|
|
}
|
2018-08-03 19:25:00 +03:00
|
|
|
|
2018-12-14 06:27:41 +02:00
|
|
|
// Verify required Node.js version
|
2020-07-05 05:00:54 +03:00
|
|
|
const REQUIRED_NODE_VERSION = "14.0.0";
|
2018-12-14 06:27:41 +02:00
|
|
|
const requiredParts = REQUIRED_NODE_VERSION.split(".").map(v => parseInt(v, 10));
|
|
|
|
const actualVersionParts = process.versions.node.split(".").map(v => parseInt(v, 10));
|
|
|
|
for (const [i, part] of actualVersionParts.entries()) {
|
2019-01-03 03:47:52 +02:00
|
|
|
if (part > requiredParts[i]) break;
|
|
|
|
if (part === requiredParts[i]) continue;
|
|
|
|
throw new SimpleError(`Unsupported Node.js version! Must be at least ${REQUIRED_NODE_VERSION}`);
|
2018-12-14 06:27:41 +02:00
|
|
|
}
|
|
|
|
|
2018-07-07 15:39:56 +03:00
|
|
|
moment.tz.setDefault("UTC");
|
|
|
|
|
2019-05-25 21:23:09 +03:00
|
|
|
logger.info("Connecting to database");
|
2020-06-30 17:48:18 +03:00
|
|
|
connect().then(async () => {
|
2019-04-13 03:17:09 +03:00
|
|
|
const client = new Client(`Bot ${process.env.TOKEN}`, {
|
2019-08-04 13:42:39 +03:00
|
|
|
getAllUsers: false,
|
2019-04-13 03:17:09 +03:00
|
|
|
restMode: true,
|
2020-08-19 00:57:44 +03:00
|
|
|
compress: false,
|
2020-10-10 14:53:47 +03:00
|
|
|
guildCreateTimeout: 0,
|
2020-07-30 22:10:17 +03:00
|
|
|
intents: [
|
|
|
|
// Privileged
|
|
|
|
"guildMembers",
|
2020-10-10 05:13:20 +03:00
|
|
|
// "guildPresences",
|
2020-10-10 05:09:59 +03:00
|
|
|
"guildMessageTyping",
|
2020-07-30 22:10:17 +03:00
|
|
|
|
|
|
|
// Regular
|
|
|
|
"directMessages",
|
|
|
|
"guildBans",
|
|
|
|
"guildEmojis",
|
|
|
|
"guildInvites",
|
|
|
|
"guildMessageReactions",
|
|
|
|
"guildMessages",
|
|
|
|
"guilds",
|
|
|
|
"guildVoiceStates",
|
|
|
|
],
|
2018-07-31 20:58:48 +03:00
|
|
|
});
|
2020-07-30 13:51:36 +03:00
|
|
|
client.setMaxListeners(200);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2019-02-07 20:44:26 +02:00
|
|
|
client.on("debug", message => {
|
|
|
|
if (message.includes(" 429 ")) {
|
2020-05-28 03:15:06 +03:00
|
|
|
logger.info(`[429] ${message}`);
|
2019-02-07 20:44:26 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-13 19:49:31 +03:00
|
|
|
client.on("error", err => {
|
|
|
|
logger.error(`[ERIS] ${String(err)}`);
|
|
|
|
});
|
|
|
|
|
2019-06-22 18:52:24 +03:00
|
|
|
const allowedGuilds = new AllowedGuilds();
|
|
|
|
const guildConfigs = new Configs();
|
|
|
|
|
2020-08-10 00:24:06 +03:00
|
|
|
const bot = new Knub<ZeppelinGuildConfig, ZeppelinGlobalConfig>(client, {
|
2020-07-06 01:53:58 +03:00
|
|
|
guildPlugins,
|
|
|
|
globalPlugins,
|
2018-07-31 19:00:17 +03:00
|
|
|
|
|
|
|
options: {
|
2019-06-22 18:52:24 +03:00
|
|
|
canLoadGuild(guildId): Promise<boolean> {
|
|
|
|
return allowedGuilds.isAllowed(guildId);
|
|
|
|
},
|
|
|
|
|
2019-05-07 19:54:16 +03:00
|
|
|
/**
|
|
|
|
* Plugins are enabled if they...
|
|
|
|
* - are base plugins, i.e. always enabled, or
|
|
|
|
* - are explicitly enabled in the guild config
|
2020-07-05 05:00:54 +03:00
|
|
|
* Dependencies are also automatically loaded by Knub.
|
2019-05-07 19:54:16 +03:00
|
|
|
*/
|
2020-07-30 22:00:08 +03:00
|
|
|
async getEnabledGuildPlugins(ctx, plugins): Promise<string[]> {
|
2020-08-28 02:01:35 +03:00
|
|
|
if (!ctx.config || !ctx.config.plugins) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const configuredPlugins = ctx.config.plugins;
|
2020-07-30 22:23:18 +03:00
|
|
|
const basePluginNames = baseGuildPlugins.map(p => p.name);
|
2019-04-20 17:36:28 +03:00
|
|
|
|
2020-07-30 22:00:08 +03:00
|
|
|
return Array.from(plugins.keys()).filter(pluginName => {
|
2020-07-30 22:23:18 +03:00
|
|
|
if (basePluginNames.includes(pluginName)) return true;
|
2019-04-20 17:36:28 +03:00
|
|
|
return configuredPlugins[pluginName] && configuredPlugins[pluginName].enabled !== false;
|
2018-07-31 19:00:17 +03:00
|
|
|
});
|
2018-10-26 06:41:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
async getConfig(id) {
|
2019-06-22 18:52:24 +03:00
|
|
|
const key = id === "global" ? "global" : `guild-${id}`;
|
|
|
|
const row = await guildConfigs.getActiveByKey(key);
|
|
|
|
if (row) {
|
|
|
|
return yaml.safeLoad(row.config);
|
2018-10-26 06:41:20 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 18:52:24 +03:00
|
|
|
logger.warn(`No config with key "${key}"`);
|
|
|
|
return {};
|
2018-10-26 06:41:20 +03:00
|
|
|
},
|
|
|
|
|
2019-05-04 10:58:57 +03:00
|
|
|
logFn: (level, msg) => {
|
|
|
|
if (level === "debug") return;
|
2020-07-22 22:56:21 +03:00
|
|
|
|
|
|
|
if (logger[level]) {
|
|
|
|
logger[level](msg);
|
|
|
|
} else {
|
|
|
|
logger.log(`[${level.toUpperCase()}] ${msg}`);
|
|
|
|
}
|
2019-05-04 10:58:57 +03:00
|
|
|
},
|
|
|
|
|
2019-02-08 21:04:04 +02:00
|
|
|
performanceDebug: {
|
2019-05-04 10:52:59 +03:00
|
|
|
enabled: false,
|
2019-02-08 21:04:04 +02:00
|
|
|
size: 30,
|
2019-02-19 00:02:46 +02:00
|
|
|
threshold: 200,
|
|
|
|
},
|
2019-04-05 20:05:37 +03:00
|
|
|
|
2020-01-12 22:19:10 +11:00
|
|
|
sendSuccessMessageFn(channel, body) {
|
2020-01-12 13:44:31 +02:00
|
|
|
const guildId = channel instanceof TextChannel ? channel.guild.id : undefined;
|
2020-11-09 20:03:57 +02:00
|
|
|
const emoji = guildId ? bot.getLoadedGuild(guildId)!.config.success_emoji : undefined;
|
2020-01-12 22:19:10 +11:00
|
|
|
channel.createMessage(successMessage(body, emoji));
|
2019-04-19 12:25:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
sendErrorMessageFn(channel, body) {
|
2020-01-12 13:44:31 +02:00
|
|
|
const guildId = channel instanceof TextChannel ? channel.guild.id : undefined;
|
2020-11-09 20:03:57 +02:00
|
|
|
const emoji = guildId ? bot.getLoadedGuild(guildId)!.config.error_emoji : undefined;
|
2020-01-12 13:44:31 +02:00
|
|
|
channel.createMessage(errorMessage(body, emoji));
|
2019-04-19 12:25:25 +03:00
|
|
|
},
|
2019-02-19 00:02:46 +02:00
|
|
|
},
|
2018-07-01 03:35:51 +03:00
|
|
|
});
|
|
|
|
|
2019-04-23 05:58:50 +03:00
|
|
|
client.once("ready", () => {
|
|
|
|
startUptimeCounter();
|
|
|
|
});
|
|
|
|
|
2018-07-01 04:31:24 +03:00
|
|
|
logger.info("Starting the bot");
|
2018-07-01 03:35:51 +03:00
|
|
|
bot.run();
|
|
|
|
});
|