2022-03-18 18:27:32 +02:00
/ * *
* 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
* /
2022-10-05 21:55:52 +03:00
/* eslint-disable no-console */
2022-05-05 23:47:08 +03:00
const pkg = require ( '../package.json' ) ;
2022-10-05 21:55:08 +03:00
const banner = require ( './lib/banner' ) ;
console . log ( banner ( pkg . version ) ) ; // print big title
2022-05-05 23:29:28 +03:00
const semver = require ( 'semver' ) ;
const { colours } = require ( 'leeks.js' ) ;
2024-01-11 02:08:57 +02:00
const path = require ( 'path' ) ;
2022-03-18 18:27:32 +02:00
// check node version
if ( ! semver . satisfies ( process . versions . node , pkg . engines . node ) ) {
2023-03-07 00:09:05 +02:00
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. ` ) ) ;
2022-03-18 18:27:32 +02:00
process . exit ( 1 ) ;
}
2024-01-11 02:08:57 +02:00
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.' ) ) ;
2024-01-13 03:45:09 +02:00
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 ) ) ;
2024-01-11 02:08:57 +02:00
console . log ( colours . blueBright ( ' Learn more at https://lnk.earth/dt-cwd.' ) ) ;
}
2023-02-23 23:53:18 +02:00
// this could be done first, but then there would be no banner :(
2023-02-24 02:18:38 +02:00
process . env . NODE _ENV ? ? = 'production' ; // make sure NODE_ENV is set
2023-02-23 23:53:18 +02:00
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' ) ;
2022-03-18 18:27:32 +02:00
2024-01-11 02:08:57 +02:00
// 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
2022-07-21 16:30:03 +03:00
if ( ! fs . existsSync ( './user/config.yml' ) ) {
2024-01-11 02:08:57 +02:00
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' ) ) ;
2022-03-18 18:27:32 +02:00
}
2022-07-21 16:30:03 +03:00
const config = YAML . parse ( fs . readFileSync ( './user/config.yml' , 'utf8' ) ) ;
2022-03-18 18:27:32 +02:00
const log = logger ( config ) ;
2023-06-19 01:57:11 +03:00
process . on ( 'uncaughtException' , ( error , origin ) => {
2022-09-11 23:11:12 +03:00
log . notice ( ` Discord Tickets v ${ pkg . version } on Node.js ${ process . version } ( ${ process . platform } ) ` ) ;
2023-06-19 01:57:11 +03:00
log . warn ( origin === 'uncaughtException' ? 'Uncaught exception' : 'Unhandled promise rejection' + ` ( ${ error . name } ) ` ) ;
2022-03-18 18:27:32 +02:00
log . error ( error ) ;
} ) ;
2023-06-19 01:57:11 +03:00
process . on ( 'warning' , warning => log . warn ( warning . stack ) ) ;
2022-07-17 00:18:50 +03:00
const client = new Client ( config , log ) ;
2022-05-06 02:01:19 +03:00
client . login ( ) . then ( ( ) => {
http ( client ) ;
2023-06-19 01:57:11 +03:00
} ) ;