-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
61 lines (51 loc) · 2.32 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
// Load environment variables from .env
require("dotenv-defaults").config({
defaults: './.env.example'
});
// Package to patch the way discord.js handles role mentions when
// "client.options.disableMentions" is set to "everyone"
// See https://github.com/HaileyBot/sanitize-role-mentions for more info
require("@haileybot/sanitize-role-mentions")();
const fs = require("fs");
// Create "guilds" directory if it doesn't already exist
if (!fs.existsSync("./guilds")) fs.mkdirSync("./guilds");
// Load Client class (extends Discord.Client)
const Client = require("./base/Client");
const client = new Client({ disableMentions: "everyone" });
// Load Commands
const cmdFiles = fs.readdirSync("./commands/").filter(file => file.endsWith(".js"));
for (const cmdFile of cmdFiles) client.loadCommand("./commands/", cmdFile);
// Load events
const eventFiles = fs.readdirSync("./events/").filter(file => file.endsWith(".js"));
for (const eventFile of eventFiles) {
const eventName = eventFile.split(".")[0];
const event = new (require(`./events/${eventFile}`))(client);
client.on(eventName, (...args) => event.run(...args));
delete require.cache[require.resolve(`./events/${eventFile}`)];
}
// Log errors, warnings, info, etc
client
.on("error", e => client.logger.error(e))
.on("shardError", (e, id) => {
client.logger.error(`Error on shard ${id}:`);
client.logger.error(e);
})
.on("debug", info => {
// Check if "info" is reporting a shard initializing a load sequence
const loading = info.match(/\[WS => Shard (\d+)] \[CONNECT]/);
if (loading) return client.logger.log(`Loading shard ${loading[1]} . . .`);
// Check if "info" is reporting how many sessions the client has left
// - Discord clients are limited to a max of 1000 logins per day
// - See https://discord.com/developers/docs/topics/gateway#identifying
// for more info (look for the yellow box)
const sessions = info.match(/Remaining: (\d+)$/);
if (sessions) return client.logger.debug(`Session ${1000 - parseInt(sessions[1], 10)} of 1000`);
})
.on("shardReady", id => client.logger.ready(`Shard ${id} connected!`))
.on("shardResume", id => client.logger.ready(`Shard ${id} connected!`));
// Log in to Discord
client.login(client.config.token).catch(e => {
client.logger.error(e.message);
process.exit();
});
process.on("unhandledRejection", err => client.logger.error(err));