2018-02-11 14:54:30 -05:00
|
|
|
// Verify NodeJS version
|
2020-08-12 17:08:37 -04:00
|
|
|
const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10);
|
2020-08-12 20:40:34 -04:00
|
|
|
if (nodeMajorVersion < 12) {
|
|
|
|
console.error("Unsupported NodeJS version! Please install Node.js 12, 13, or 14.");
|
2018-02-11 14:54:30 -05:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify node modules have been installed
|
2020-08-12 17:08:37 -04:00
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
2018-02-11 14:54:30 -05:00
|
|
|
|
|
|
|
try {
|
2020-08-12 17:08:37 -04:00
|
|
|
fs.accessSync(path.join(__dirname, "..", "node_modules"));
|
2018-02-11 14:54:30 -05:00
|
|
|
} catch (e) {
|
2020-08-12 17:08:37 -04:00
|
|
|
console.error("Please run \"npm ci\" before starting the bot");
|
2018-03-13 01:24:01 -04:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:11:08 -04:00
|
|
|
// Error handling
|
2020-08-12 17:08:37 -04:00
|
|
|
process.on("uncaughtException", err => {
|
2019-06-05 16:11:08 -04:00
|
|
|
// Unknown message types (nitro boosting messages at the time) should be safe to ignore
|
2020-08-12 17:08:37 -04:00
|
|
|
if (err && err.message && err.message.startsWith("Unhandled MESSAGE_CREATE type")) {
|
2019-06-05 16:11:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For everything else, crash with the error
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
let testedPackage = "";
|
2018-03-13 01:24:01 -04:00
|
|
|
try {
|
2020-08-12 17:08:37 -04:00
|
|
|
const packageJson = require("../package.json");
|
2018-03-13 01:24:01 -04:00
|
|
|
const modules = Object.keys(packageJson.dependencies);
|
|
|
|
modules.forEach(mod => {
|
|
|
|
testedPackage = mod;
|
2020-08-12 17:08:37 -04:00
|
|
|
fs.accessSync(path.join(__dirname, "..", "node_modules", mod))
|
2018-03-13 01:24:01 -04:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-05-24 19:38:35 -04:00
|
|
|
console.error(`Please run "npm ci" again! Package "${testedPackage}" is missing.`);
|
2018-02-11 14:54:30 -05:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
const config = require("./cfg");
|
|
|
|
const utils = require("./utils");
|
|
|
|
const main = require("./main");
|
|
|
|
const knex = require("./knex");
|
|
|
|
const legacyMigrator = require("./legacy/legacyMigrator");
|
2017-02-09 21:56:36 -05:00
|
|
|
|
2017-09-19 13:23:55 -04:00
|
|
|
// Force crash on unhandled rejections (use something like forever/pm2 to restart)
|
2020-08-12 17:08:37 -04:00
|
|
|
process.on("unhandledRejection", err => {
|
2017-09-20 02:46:11 -04:00
|
|
|
if (err instanceof utils.BotError || (err && err.code)) {
|
|
|
|
// We ignore stack traces for BotErrors (the message has enough info) and network errors from Eris (their stack traces are unreadably long)
|
2017-09-19 13:23:55 -04:00
|
|
|
console.error(`Error: ${err.message}`);
|
|
|
|
} else {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
(async function() {
|
|
|
|
// Make sure the database is up to date
|
2020-05-24 19:45:07 -04:00
|
|
|
const [completed, newMigrations] = await knex.migrate.list();
|
|
|
|
if (newMigrations.length > 0) {
|
2020-08-12 17:08:37 -04:00
|
|
|
console.log("Updating database. This can take a while. Don't close the bot!");
|
2020-05-24 18:33:10 -04:00
|
|
|
await knex.migrate.latest();
|
2020-08-12 17:08:37 -04:00
|
|
|
console.log("Done!");
|
2020-05-24 18:33:10 -04:00
|
|
|
}
|
2017-07-23 20:27:21 -04:00
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
// Migrate legacy data if we need to
|
|
|
|
if (await legacyMigrator.shouldMigrate()) {
|
2020-08-12 17:08:37 -04:00
|
|
|
console.log("=== MIGRATING LEGACY DATA ===");
|
|
|
|
console.log("Do not close the bot!");
|
|
|
|
console.log("");
|
2017-09-19 13:23:55 -04:00
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
await legacyMigrator.migrate();
|
2017-07-23 20:27:21 -04:00
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
const relativeDbDir = (path.isAbsolute(config.dbDir) ? config.dbDir : path.resolve(process.cwd(), config.dbDir));
|
|
|
|
const relativeLogDir = (path.isAbsolute(config.logDir) ? config.logDir : path.resolve(process.cwd(), config.logDir));
|
2017-07-23 20:27:21 -04:00
|
|
|
|
2020-08-12 17:08:37 -04:00
|
|
|
console.log("");
|
|
|
|
console.log("=== LEGACY DATA MIGRATION FINISHED ===");
|
|
|
|
console.log("");
|
|
|
|
console.log("IMPORTANT: After the bot starts, please verify that all logs, threads, blocked users, and snippets are still working correctly.");
|
|
|
|
console.log("Once you've done that, the following files/directories are no longer needed. I would recommend keeping a backup of them, however.");
|
|
|
|
console.log("");
|
|
|
|
console.log("FILE: " + path.resolve(relativeDbDir, "threads.json"));
|
|
|
|
console.log("FILE: " + path.resolve(relativeDbDir, "blocked.json"));
|
|
|
|
console.log("FILE: " + path.resolve(relativeDbDir, "snippets.json"));
|
|
|
|
console.log("DIRECTORY: " + relativeLogDir);
|
|
|
|
console.log("");
|
|
|
|
console.log("Starting the bot...");
|
2017-07-23 20:27:21 -04:00
|
|
|
}
|
|
|
|
|
2017-12-24 15:04:08 -05:00
|
|
|
// Start the bot
|
2017-12-31 19:16:05 -05:00
|
|
|
main.start();
|
2017-12-24 15:04:08 -05:00
|
|
|
})();
|