forked from esmBot/esmBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (58 loc) · 1.87 KB
/
app.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
// load config from .env file
require("dotenv").config();
// turn fs.readdir into a promise
const readdir = require("util").promisify(require("fs").readdir);
// fancy loggings
const logger = require("./utils/logger.js");
// start the client
const client = require("./utils/client.js");
// initialize command loader
const handler = require("./utils/handler.js");
const sound = require("./utils/soundplayer.js");
// registers stuff and connects the bot
async function init() {
logger.log("info", "Starting esmBot...");
// register commands and their info
const commands = await readdir("./commands/");
const soundStatus = await sound.checkStatus();
logger.log("info", `Attempting to load ${commands.length} commands...`);
for (const commandFile of commands) {
logger.log("info", `Loading command ${commandFile}...`);
try {
await handler.load(commandFile, soundStatus);
} catch (e) {
logger.error(`Failed to register command ${commandFile.split(".")[0]}: ${e}`);
}
}
// register events
const events = await readdir("./events/");
logger.log("info", `Attempting to load ${events.length} events...`);
for (const file of events) {
logger.log("info", `Loading event ${file}...`);
const eventName = file.split(".")[0];
const event = require(`./events/${file}`);
client.on(eventName, event);
}
// login
client.connect();
// post to DBL
if (process.env.NODE_ENV === "production") {
require("./utils/dbl.js");
}
// handle ctrl+c and pm2 stop
process.on("SIGINT", () => {
logger.log("info", "SIGINT detected, shutting down...");
client.editStatus("dnd", {
name: "Restarting/shutting down..."
});
for (const command of commands) {
handler.unload(command);
}
client.disconnect();
require("./utils/database.js").connection.close(() => {
process.exit(0);
});
});
}
// launch the bot
init();