-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
115 lines (101 loc) · 4.28 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// 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]) < 16)
throw new Error(
"Node 16.x or higher is required. Update Node on your system.",
);
require("dotenv").config();
// Load up the discord.js library
const { Client, Collection } = require("discord.js");
// We also load the rest of the things we need in this file:
const { readdirSync } = require("fs");
const { intents, partials, permLevels } = require("./config.js");
const logger = require("./modules/logger.js");
// const bans = require("./bans.js");
const { load } = require("dotenv");
// This is your client. Some people call it `bot`, some people call it `self`,
// some might call it `cootchie`. Either way, when you see `client.something`,
// or `bot.something`, this is what we're referring to. Your client.
const client = new Client({ intents, partials });
// Aliases, commands and slash commands are put in collections where they can be
// read from, catalogued, listed, etc.
const commands = new Collection();
const aliases = new Collection();
const slashcmds = new Collection();
// Generate a cache of client permissions for pretty perm names in commands.
const levelCache = {};
for (let i = 0; i < permLevels.length; i++) {
const thisLevel = permLevels[i];
levelCache[thisLevel.name] = thisLevel.level;
}
// To reduce client pollution we'll create a single container property
// that we can attach everything we need to.
client.container = {
commands,
aliases,
slashcmds,
levelCache,
};
// We're doing real fancy node 8 async/await stuff here, and to do that
// we need to wrap stuff in an anonymous function. It's annoying but it works.
const init = async () => {
// Here we load **commands** into memory, as a collection, so they're
// accessible here and everywhere else.
const commands = readdirSync("./commands/").filter((file) =>
file.endsWith(".js"),
);
// Log the loading of commands.
logger.log(`Loading a total of ${commands.length} commands...`);
const loadedCommands = [];
for (const file of commands) {
const props = require(`./commands/${file}`);
// logger.log(`Loading Command: ${props.help.name}. 👌`, "log");
client.container.commands.set(props.help.name, props);
loadedCommands.push(props.help.name);
props.conf.aliases.forEach((alias) => {
client.container.aliases.set(alias, props.help.name);
});
}
logger.log(`Commands Loaded: ${loadedCommands}`);
// Now we load any **slash** commands you may have in the ./slash directory.
const slashFiles = readdirSync("./slash").filter((file) =>
file.endsWith(".js"),
);
logger.log(`Loading a total of ${slashFiles.length} slash commands...`);
const loadedSlashCommands = [];
for (const file of slashFiles) {
const command = require(`./slash/${file}`);
const commandName = file.split(".")[0];
loadedSlashCommands.push(commandName);
// logger.log(`Loading Slash command: ${commandName}. 👌`, "log");
// Now set the name of the command with it's properties.
client.container.slashcmds.set(command.commandData.name, command);
}
logger.log(`Slash commands Loaded: ${loadedSlashCommands}`);
// Then we load events, which will include our message and ready event.
const eventFiles = readdirSync("./events/").filter((file) =>
file.endsWith(".js"),
);
logger.log(`Loading a total of ${eventFiles.length} events...`);
const loadedEvents = [];
for (const file of eventFiles) {
const eventName = file.split(".")[0];
loadedEvents.push(eventName);
// logger.log(`Loading Event: ${eventName}. 👌`, "log");
const event = require(`./events/${file}`);
// Bind the client to any event, before the existing arguments
// provided by the discord.js event.
// This line is awesome by the way. Just sayin'.
client.on(eventName, event.bind(null, client));
}
logger.log(`Events Loaded: ${loadedEvents}`);
// Threads are currently in BETA.
// This event will fire when a thread is created, if you want to expand
// the logic, throw this in it's own event file like the rest.
client.on("threadCreate", (thread) => thread.join());
// Here we login the client.
client.login();
// End top-level async/await function.
};
init();