forked from AnIdiotsGuide/guidebot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
75 lines (63 loc) · 2.75 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
// This will check if the node version you are running is the required
// Node version, if it isn't it will throw the following error to inform
// you.
if (Number(process.version.slice(1).split(".")[0]) < 12) throw new Error("Node 12.0.0 or higher is required. Update Node on your system.");
// Load up the discord.js library
const Discord = require("discord.js");
// We also load the rest of the things we need in this file:
const {promisify} = require("util");
const readdir = promisify(require("fs").readdir);
const Enmap = require("enmap");
const config = require("./config.js");
const client = new Discord.Client({
ws: {
intents: config.intents
}
});
client.config = config
client.logger = require("./modules/Logger");
client.defaultSettings = require("./default.js");
require("./modules/functions.js")(client);
// Aliases and commands are put in collections where they can be read from,
// catalogued, listed, etc.
client.commands = new Enmap();
client.aliases = new Enmap();
client.settings = new Enmap({name: "settings"});
const init = async () => {
// Here we load **commands** into memory, as a collection, so they're accessible
// here and everywhere else.
const cmdFiles = await readdir("./commands/");
client.logger.log(`Loading a total of ${cmdFiles.length} commands.`);
cmdFiles.forEach(f => {
if (!f.endsWith(".js")) return;
const response = client.loadCommand(f);
if (response) console.log(response);
});
// Then we load events, which will include our message and ready event.
const evtFiles = await readdir("./events/");
client.logger.log(`Loading a total of ${evtFiles.length} events.`);
evtFiles.forEach(file => {
const eventName = file.split(".")[0];
client.logger.log(`Loading Event: ${eventName}`);
const event = require(`./events/${file}`);
// Bind the client to any event, before the existing arguments
// provided by the discord.js event.
client.on(eventName, event.bind(null, client));
});
require("./modules/QuestHandler.js").extension(client);
// Here we login the client.
await client.login(client.config.token);
};
init();
client.on("disconnect", () => client.logger.warn("Bot is disconnecting..."))
.on("reconnecting", () => client.logger.log("Bot reconnecting...", "log"))
.on("error", e => client.logger.error(e))
.on("warn", info => client.logger.warn(info));
process.on("uncaughtException", (err) => {
const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, "g"), "./");
console.error("Uncaught Exception: ", errorMsg);
process.exit(0);
});
process.on("unhandledRejection", err => {
console.error("Uncaught Promise Error: ", err);
});