-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratot.js
More file actions
92 lines (81 loc) · 2.34 KB
/
Copy pathratot.js
File metadata and controls
92 lines (81 loc) · 2.34 KB
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
// Ratot - Ratot is a Discord bot made to help you administrate your server and have some fun.
// Copyright (C) 2026 Captain Ratax
// Licensed under the GNU Affero General Public License v3.0 or later
// See the LICENSE file for details.
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) {
mongoose
.connect(process.env.DBURL)
.then(() => {
infoLogger.info("Connected to MongoDB");
})
.catch((error) => {
errorLogger.error("Error when connecting to MongoDB. Errors:", error);
});
app.locals.discordClient = client;
app.locals.mongoose = mongoose;
app.use("/", api);
infoLogger.info("API routes are set up and ready to use!");
app.get("/{*splat}", function (req, res) {
infoLogger.info(
"Unhandled route hit: " + req.method + " " + req.originalUrl,
);
// Placeholder handler: return 404 so the request completes
return res.status(404).end();
});
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);
client.login(process.env.RATOT_CURRENT_TOKEN); //Starts the bot
}