-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
113 lines (97 loc) · 3.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
___ _
/ _ \ (_)
/ /_\ \_ __ ___ _ __ _ _ _ __ ___ _ ___ ___
| _ | '_ \ / _ \| '_ \| | | | '_ ` _ \| |/ __/ _ \
| | | | | | | (_) | | | | |_| | | | | | | | (_| __/
\_| |_/_| |_|\___/|_| |_|\__, |_| |_| |_|_|\___\___|
__/ |
|___/
______ _ _ ______ _
| _ (_) | | | ___ \ | |
| | | |_ ___ ___ ___ _ __ __| | | |_/ / ___ | |_
| | | | / __|/ __/ _ \| '__/ _` | | ___ \/ _ \| __|
| |/ /| \__ \ (_| (_) | | | (_| | | |_/ / (_) | |_
|___/ |_|___/\___\___/|_| \__,_| \____/ \___/ \__|
*/
/*##############################################################################
# File: index.js #
# Project: Anonymice - Discord Bot #
# Author(s): Oliver Renner (@_orenner) & slingn.eth (@slingncrypto) #
# © 2021 #
###############################################################################*/
const config = require("./src/config");
const logger = require("./src/utils/logger");
const banner = require("./src/utils/banner");
const mongoose = require("mongoose");
const app = require("./src/app");
const DiscordBot = require("./src/discordBot");
const Synchronizer = require("./src/synchronizer");
logger.info(banner);
logger.info(`Starting ${config.application.name}...`)
let server;
mongoose.connect(config.mongodb.url, config.mongoose.options)
.then(async () => {
logger.info("Connected to MongoDB");
//start the web server
server = await app.listen(config.application.port, () => {
logger.info(
`${config.application.name} is running at port ${config.application.port}`
);
});
//todo: migrate to another node hosting script
//start the discord bot
await DiscordBot.start();
//todo: migrate to another node hosting script
//start the daily role verification sync process
await Synchronizer.start();
if(config.sync.syncOnStartup && config.sync.syncOnStartup === "true"){
logger.info(`Synchronize on startup as been enabled, executing first synchronization cycle immediately...`)
await Synchronizer.execute();
}
})
.catch(error => {
exitHandler(error)
});
//todo: deprecate once other services are moved to another node hosting script
const stopServices = (shouldLog) => {
if (server) {
server.close();
logger.info("Server closed");
}
if (DiscordBot) {
DiscordBot.stop();
logger.info("Discord Client closed");
}
if (Synchronizer) {
Synchronizer.stop();
logger.info("Synchronizer stopped");
}
};
const exitHandler = (error) => {
stopServices(true);
if(error) {
logger.error(error.message);
logger.error(error.stack);
process.exit(-1);
}
process.exit(0);
}
const unexpectedErrorHandler = (error) => {
logger.error(error.message);
logger.error(error.stack)
};
process.on("uncaughtException", unexpectedErrorHandler);
process.on("unhandledRejection", unexpectedErrorHandler);
process.on("SIGINT", () => {
logger.info("SIGNINT received");
logger.info(`Stopping ${config.application.name}`);
stopServices();
process.exit(0);
});
process.on("SIGTERM", () => {
logger.info("SIGNTERM received");
logger.info(`Stopping ${config.application.name}`);
stopServices();
process.exit(0);
});