2018-10-26 06:41:20 +03:00
|
|
|
import path from "path";
|
|
|
|
import yaml from "js-yaml";
|
|
|
|
|
|
|
|
import _fs from "fs";
|
|
|
|
const fs = _fs.promises;
|
|
|
|
|
2019-01-15 01:05:12 +02:00
|
|
|
import {Knub, logger, PluginError} from "knub";
|
2018-12-14 06:27:41 +02:00
|
|
|
import { SimpleError } from "./SimpleError";
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
require("dotenv").config();
|
|
|
|
|
2019-01-15 01:05:12 +02:00
|
|
|
let recentPluginErrors = 0;
|
2019-01-13 16:52:00 +02:00
|
|
|
const RECENT_ERROR_EXIT_THRESHOLD = 5;
|
2019-01-15 01:05:12 +02:00
|
|
|
setInterval(() => (recentPluginErrors = Math.max(0, recentPluginErrors - 1)), 2500);
|
2019-01-13 16:52:00 +02:00
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
process.on("unhandledRejection", (reason, p) => {
|
|
|
|
console.error(reason);
|
2019-01-15 01:05:12 +02:00
|
|
|
|
|
|
|
if (reason instanceof PluginError) {
|
|
|
|
if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1);
|
|
|
|
} else {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
});
|
|
|
|
|
2018-08-03 19:25:00 +03:00
|
|
|
process.on("uncaughtException", err => {
|
2019-01-13 16:52:00 +02:00
|
|
|
console.error(err);
|
2019-01-15 01:05:12 +02:00
|
|
|
|
|
|
|
if (err instanceof PluginError) {
|
|
|
|
if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1);
|
|
|
|
} else {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-08-03 19:25:00 +03:00
|
|
|
});
|
|
|
|
|
2018-12-14 06:27:41 +02:00
|
|
|
// Verify required Node.js version
|
|
|
|
const REQUIRED_NODE_VERSION = "10.14.2";
|
|
|
|
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
|
|
|
// Always use UTC
|
2018-08-02 00:48:48 +03:00
|
|
|
import moment from "moment-timezone";
|
2018-07-07 15:39:56 +03:00
|
|
|
moment.tz.setDefault("UTC");
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
import { Client } from "eris";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { connect } from "./data/db";
|
2018-08-05 01:32:59 +03:00
|
|
|
|
|
|
|
// Global plugins
|
2018-07-01 03:35:51 +03:00
|
|
|
import { BotControlPlugin } from "./plugins/BotControl";
|
2018-08-05 01:32:59 +03:00
|
|
|
import { LogServerPlugin } from "./plugins/LogServer";
|
|
|
|
|
|
|
|
// Guild plugins
|
2018-07-01 03:35:51 +03:00
|
|
|
import { ModActionsPlugin } from "./plugins/ModActions";
|
|
|
|
import { UtilityPlugin } from "./plugins/Utility";
|
2018-07-09 02:48:36 +03:00
|
|
|
import { LogsPlugin } from "./plugins/Logs";
|
2018-07-14 20:54:48 +03:00
|
|
|
import { PostPlugin } from "./plugins/Post";
|
2018-07-29 15:18:26 +03:00
|
|
|
import { ReactionRolesPlugin } from "./plugins/ReactionRoles";
|
2018-07-30 01:44:03 +03:00
|
|
|
import { CensorPlugin } from "./plugins/Censor";
|
2018-07-30 23:35:44 +03:00
|
|
|
import { PersistPlugin } from "./plugins/Persist";
|
2018-07-31 02:42:45 +03:00
|
|
|
import { SpamPlugin } from "./plugins/Spam";
|
2018-08-05 01:32:59 +03:00
|
|
|
import { TagsPlugin } from "./plugins/Tags";
|
2018-11-24 14:01:06 +02:00
|
|
|
import { MessageSaverPlugin } from "./plugins/MessageSaver";
|
2018-11-25 17:04:26 +02:00
|
|
|
import { CasesPlugin } from "./plugins/Cases";
|
|
|
|
import { MutesPlugin } from "./plugins/Mutes";
|
2018-12-15 17:04:04 +02:00
|
|
|
import { SlowmodePlugin } from "./plugins/Slowmode";
|
2018-12-15 23:01:45 +02:00
|
|
|
import { StarboardPlugin } from "./plugins/Starboard";
|
2019-01-06 15:27:51 +02:00
|
|
|
import { NameHistoryPlugin } from "./plugins/NameHistory";
|
2019-01-12 13:42:11 +02:00
|
|
|
import { AutoReactions } from "./plugins/AutoReactions";
|
2019-01-12 14:09:11 +02:00
|
|
|
import { PingableRoles } from "./plugins/PingableRoles";
|
2018-07-01 03:35:51 +03:00
|
|
|
|
|
|
|
// Run latest database migrations
|
2018-07-01 04:31:24 +03:00
|
|
|
logger.info("Running database migrations");
|
2018-10-26 06:41:20 +03:00
|
|
|
connect().then(async conn => {
|
|
|
|
await conn.runMigrations();
|
|
|
|
|
2018-07-31 20:58:48 +03:00
|
|
|
const client = new Client(process.env.TOKEN, {
|
|
|
|
getAllUsers: true
|
|
|
|
});
|
2018-07-31 02:42:45 +03:00
|
|
|
client.setMaxListeners(100);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2019-01-06 15:27:51 +02:00
|
|
|
const basePlugins = ["message_saver", "name_history", "cases", "mutes"];
|
2018-11-25 17:04:26 +02:00
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
const bot = new Knub(client, {
|
2019-01-03 06:15:28 +02:00
|
|
|
plugins: [
|
2018-11-25 17:04:26 +02:00
|
|
|
// Base plugins (always enabled)
|
2019-01-03 06:15:28 +02:00
|
|
|
MessageSaverPlugin,
|
2019-01-06 15:27:51 +02:00
|
|
|
NameHistoryPlugin,
|
2019-01-03 06:15:28 +02:00
|
|
|
CasesPlugin,
|
|
|
|
MutesPlugin,
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
// Regular plugins
|
2019-01-03 06:15:28 +02:00
|
|
|
UtilityPlugin,
|
|
|
|
ModActionsPlugin,
|
|
|
|
LogsPlugin,
|
|
|
|
PostPlugin,
|
|
|
|
ReactionRolesPlugin,
|
|
|
|
CensorPlugin,
|
|
|
|
PersistPlugin,
|
|
|
|
SpamPlugin,
|
|
|
|
TagsPlugin,
|
|
|
|
SlowmodePlugin,
|
2019-01-12 13:42:11 +02:00
|
|
|
StarboardPlugin,
|
2019-01-12 14:09:11 +02:00
|
|
|
AutoReactions,
|
|
|
|
PingableRoles
|
2019-01-03 06:15:28 +02:00
|
|
|
],
|
|
|
|
|
2019-01-06 15:27:51 +02:00
|
|
|
globalPlugins: [BotControlPlugin, LogServerPlugin],
|
2018-07-31 19:00:17 +03:00
|
|
|
|
|
|
|
options: {
|
|
|
|
getEnabledPlugins(guildId, guildConfig): string[] {
|
|
|
|
const plugins = guildConfig.plugins || {};
|
|
|
|
const keys: string[] = Array.from(this.plugins.keys());
|
|
|
|
return keys.filter(pluginName => {
|
2018-11-25 17:04:26 +02:00
|
|
|
return basePlugins.includes(pluginName) || (plugins[pluginName] && plugins[pluginName].enabled !== false);
|
2018-07-31 19:00:17 +03:00
|
|
|
});
|
2018-10-26 06:41:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
async getConfig(id) {
|
|
|
|
const configFile = id ? `${id}.yml` : "global.yml";
|
|
|
|
const configPath = path.join("config", configFile);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fs.access(configPath);
|
|
|
|
} catch (e) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const yamlString = await fs.readFile(configPath, { encoding: "utf8" });
|
|
|
|
return yaml.safeLoad(yamlString);
|
|
|
|
},
|
|
|
|
|
|
|
|
logFn: (level, msg) => {
|
|
|
|
if (level === "debug") return;
|
2019-01-13 23:29:44 +02:00
|
|
|
const ts = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
|
|
console.log(`[${ts}] [${level.toUpperCase()}] ${msg}`);
|
2018-07-31 19:00:17 +03:00
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-01 04:31:24 +03:00
|
|
|
logger.info("Starting the bot");
|
2018-07-01 03:35:51 +03:00
|
|
|
bot.run();
|
|
|
|
});
|