-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratot.js
76 lines (66 loc) · 1.84 KB
/
ratot.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
require("dotenv").config(); //Import the .env library
const { Client, GatewayIntentBits } = require("discord.js"); //Import the Discord.js library
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
}); //Create a new Discord client
const { errorLogger, warnLogger, infoLogger } = require("./src/utils/logger.js"); //Import all the custom loggers
const eventHandler = require("./src/handlers/eventHandler.js");
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var api = require("./src/api/api.js");
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
var mongoose = require("mongoose");
var serverOn;
//Code to rerun the bot when an exception occurs
var cluster = require("cluster");
if (cluster.isMaster) {
cluster.fork();
cluster.on("exit", function (worker, code, signal) {
errorLogger.error(
"FATAL ERROR! The bot stopped working and rerunned again!"
);
cluster.fork();
});
}
if (cluster.isWorker) {
try {
mongoose.connect(process.env.DBURL);
infoLogger.info("Connected to MongoDB");
} catch (error) {
errorLogger.error("Connected to MongoDB. Errors:", err);
}
var server = app
.listen(process.env.PORT, () => {
serverOn = true;
infoLogger.info(
"API Server is connected and listening on port " + server.address().port
);
})
.on("error", (error) => {
serverOn = false;
errorLogger.error(
"Error when trying to start express server! Error: ",
error
);
});
eventHandler(client);
if (serverOn) {
app.use("/", api);
app.get("*", function (req, res) {
infoLogger.info("Req: " + req + ", Res: " + res);
//To Do Later
});
}
client.login(process.env.RATOT_CURRENT_TOKEN); //Starts the bot
}