DiscordTickets/src/index.js

85 lines
3.3 KiB
JavaScript

/**
* Discord Tickets
* Copyright (C) 2022 Isaac Saunders
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @name discord-tickets/bot
* @description An open-source Discord bot for ticket management
* @copyright 2022 Isaac Saunders
* @license GNU-GPLv3
*/
/* eslint-disable no-console */
const pkg = require('../package.json');
const banner = require('./lib/banner');
banner(pkg.version); // print big title
const semver = require('semver');
const { colours } = require('leeks.js');
const path = require('path');
// check node version
if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
console.log('\x07' + colours.redBright(`Error: Your current Node.js version, ${process.versions.node}, does not meet the requirement "${pkg.engines.node}". Please update to version ${semver.minVersion(pkg.engines.node).version} or higher.`));
process.exit(1);
}
const base_dir = path.resolve(path.join(__dirname, '../'));
const cwd = path.resolve(process.cwd());
if (base_dir !== cwd) {
console.log('\x07' + colours.yellowBright('Warning: The current working directory is not the same as the base directory.'));
if (!process.env.DOCKER) {
console.log(colours.yellowBright('This may result in unexpected behaviour, particularly with missing environment variables.'));
}
console.log(' Base directory: ' + colours.gray(base_dir));
console.log(' Current directory: ' + colours.gray(cwd));
console.log(colours.blueBright(' Learn more at https://lnk.earth/dt-cwd.'));
}
// this could be done first, but then there would be no banner :(
process.env.NODE_ENV ??= 'production'; // make sure NODE_ENV is set
require('./env').load(); // load and check environment variables
const fs = require('fs');
const YAML = require('yaml');
const logger = require('./lib/logger');
const Client = require('./client');
const http = require('./http');
// user directory may or may not exist depending on if sqlite is being used
// so the config file could be missing even though the directory exists
if (!fs.existsSync('./user/config.yml')) {
console.log('The config file does not exist, copying defaults...');
fs.cpSync(path.join(__dirname, 'user'), './user', { recursive: true });
console.log('Created user directory at', path.join(cwd, 'user'));
}
const config = YAML.parse(fs.readFileSync('./user/config.yml', 'utf8'));
const log = logger(config);
process.on('uncaughtException', (error, origin) => {
log.notice(`Discord Tickets v${pkg.version} on Node.js ${process.version} (${process.platform})`);
log.warn(origin === 'uncaughtException' ? 'Uncaught exception' : 'Unhandled promise rejection' + ` (${error.name})`);
log.error(error);
});
process.on('warning', warning => log.warn(warning.stack || warning));
const client = new Client(config, log);
client.login().then(() => {
http(client);
});