3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00
zeppelin/src/index.ts

133 lines
3.7 KiB
TypeScript
Raw Normal View History

import path from "path";
import yaml from "js-yaml";
import _fs from "fs";
const fs = _fs.promises;
2018-12-14 06:27:41 +02:00
import { SimpleError } from "./SimpleError";
2018-07-01 03:35:51 +03:00
require("dotenv").config();
process.on("unhandledRejection", (reason, p) => {
// tslint:disable-next-line
console.error(reason);
2018-08-02 03:46:29 +03:00
process.exit(1);
2018-07-01 03:35:51 +03:00
});
2018-08-03 19:25:00 +03:00
process.on("uncaughtException", err => {
if (err.message && err.message.startsWith("DiscordHTTPError")) {
console.error(err);
return;
} else {
console.error(err);
process.exit(1);
}
});
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()) {
if (part < requiredParts[i]) {
throw new SimpleError(`Unsupported Node.js version! Must be at least ${REQUIRED_NODE_VERSION}`);
}
}
// Always use UTC
import moment from "moment-timezone";
moment.tz.setDefault("UTC");
2018-07-01 03:35:51 +03:00
import { Client } from "eris";
2018-07-01 04:31:24 +03:00
import { Knub, logger } from "knub";
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";
import { LogsPlugin } from "./plugins/Logs";
import { PostPlugin } from "./plugins/Post";
2018-07-29 15:18:26 +03:00
import { ReactionRolesPlugin } from "./plugins/ReactionRoles";
import { CensorPlugin } from "./plugins/Censor";
2018-07-30 23:35:44 +03:00
import { PersistPlugin } from "./plugins/Persist";
import { SpamPlugin } from "./plugins/Spam";
2018-08-05 01:32:59 +03:00
import { TagsPlugin } from "./plugins/Tags";
import { MessageSaverPlugin } from "./plugins/MessageSaver";
import { CasesPlugin } from "./plugins/Cases";
import { MutesPlugin } from "./plugins/Mutes";
2018-12-15 17:04:04 +02:00
import { SlowmodePlugin } from "./plugins/Slowmode";
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");
connect().then(async conn => {
await conn.runMigrations();
2018-07-31 20:58:48 +03:00
const client = new Client(process.env.TOKEN, {
getAllUsers: true
});
client.setMaxListeners(100);
2018-07-01 03:35:51 +03:00
const basePlugins = ["message_saver", "cases", "mutes"];
2018-07-01 03:35:51 +03:00
const bot = new Knub(client, {
plugins: {
// Base plugins (always enabled)
message_saver: MessageSaverPlugin,
cases: CasesPlugin,
mutes: MutesPlugin,
// Regular plugins
2018-07-01 03:35:51 +03:00
utility: UtilityPlugin,
mod_actions: ModActionsPlugin,
logs: LogsPlugin,
2018-07-29 15:18:26 +03:00
post: PostPlugin,
reaction_roles: ReactionRolesPlugin,
2018-07-30 23:35:44 +03:00
censor: CensorPlugin,
persist: PersistPlugin,
2018-08-05 01:32:59 +03:00
spam: SpamPlugin,
2018-12-15 17:04:04 +02:00
tags: TagsPlugin,
slowmode: SlowmodePlugin
2018-07-01 03:35:51 +03:00
},
globalPlugins: {
bot_control: BotControlPlugin,
log_server: LogServerPlugin
},
options: {
getEnabledPlugins(guildId, guildConfig): string[] {
const plugins = guildConfig.plugins || {};
const keys: string[] = Array.from(this.plugins.keys());
return keys.filter(pluginName => {
return basePlugins.includes(pluginName) || (plugins[pluginName] && plugins[pluginName].enabled !== false);
});
},
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;
console.log(`[${level.toUpperCase()}] ${msg}`);
}
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();
});