forked from Cartrigger/QCXRSupport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.js
192 lines (149 loc) · 5.78 KB
/
bot.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const path = require('path');
const fs = require("fs");
const {Client, GatewayIntentBits, Partials, Collection, REST, Routes } = require("discord.js");
const { token, client_id } = require("./config.json");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
partials: [Partials.Channel]
});
// Event Handler
client.events = new Collection();
function readDirectoryRecursively(directory) {
const files = [];
for (const file of fs.readdirSync(directory)) {
const filePath = `${directory}/${file}`;
if (fs.statSync(filePath).isFile() && filePath.endsWith(".js")) {
files.push(filePath);
} else if (fs.statSync(filePath).isDirectory()) {
files.push(...readDirectoryRecursively(filePath));
}
}
return files;
}
client.commands = new Collection();
client.slashCommands = new Collection();
client.buttonCommands = new Collection();
client.selectCommands = new Collection();
client.contextCommands = new Collection();
client.modalCommands = new Collection();
client.cooldowns = new Collection();
client.autocompleteInteractions = new Collection();
client.triggers = new Collection();
const eventFiles = readDirectoryRecursively("./events");
for (const file of eventFiles) {
const event = require(file);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
} else {
client.on(event.name,
async (...args) => await event.execute(...args, client));
}
client.events.set(event.name, event);
}
// Registration of Slash-Command Interactions.
const slashCommands = getSlashCommands("./interactions/slash");
function getSlashCommands(path) {
let slashCommands = [];
const commandFiles = fs.readdirSync(path);
for (const commandFile of commandFiles) {
const fullCommandPath = `${path}/${commandFile}`;
const commandStats = fs.statSync(fullCommandPath);
if (commandStats.isDirectory()) {
slashCommands = slashCommands.concat(getSlashCommands(fullCommandPath));
} else if (commandFile.endsWith(".js")) {
const command = require(fullCommandPath);
client.slashCommands.set(command.data.name, command);
slashCommands.push(fullCommandPath);
}
}
return slashCommands;
}
// Registration of Context-Menu Interactions
const contextMenus = fs.readdirSync("./interactions/context-menus");
// Loop through all files and store context-menus in contextMenus collection.
for (const folder of contextMenus) {
const files = fs
.readdirSync(`./interactions/context-menus/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of files) {
const menu = require(`./interactions/context-menus/${folder}/${file}`);
const keyName = `${folder.toUpperCase()} ${menu.data.name}`;
client.contextCommands.set(keyName, menu);
}
}
// Registration of Button-Command Interactions.
const buttonCommands = [];
// Define a function that recursively loops through all files in a directory and its subdirectories.
function walk(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const path = `${dir}/${file}`;
if (fs.statSync(path).isDirectory()) {
walk(path);
} else if (file.endsWith(".js")) {
const command = require(path);
buttonCommands.push(command);
}
}
}
// Start the recursive loop at the root of the button commands directory.
walk("./interactions/buttons");
// Register each button command with the client.
for (const command of buttonCommands) {
client.buttonCommands.set(command.id, command);
}
// Registration of select-menus Interactions
const selectMenus = fs.readdirSync("./interactions/select-menus");
// Loop through all files and store select-menus in selectMenus collection.
for (const module of selectMenus) {
const commandFiles = fs
.readdirSync(`./interactions/select-menus/${module}`)
.filter((file) => file.endsWith(".js"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/select-menus/${module}/${commandFile}`);
client.selectCommands.set(command.id, command);
}
}
// Registration of Slash-Commands in Discord API
const rest = new REST({ version: "9" }).setToken(token);
const commandJsonData = [
...Array.from(client.slashCommands.values()).map((c) => c.data.toJSON()),
...Array.from(client.contextCommands.values()).map((c) => c.data)
];
(async () => {
try {
console.log("Started refreshing application (/) commands.");
await rest.put(
Routes.applicationCommands(client_id),
{ body: commandJsonData }
);
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
// Registration of Message Based Chat Triggers
const triggerFolders = fs.readdirSync("./triggers");
// Loop through all files and store triggers in triggers collection.
for (const folder of triggerFolders) {
const triggerFiles = fs
.readdirSync(`./triggers/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of triggerFiles) {
const trigger = require(`./triggers/${folder}/${file}`);
client.triggers.set(trigger.name, trigger);
}
}
client.login(token);
// Anti Crash script
process.on("unhandRejection", (reason, promise) => {
console.log(`🚫 Critical Error detected! Unhandled Rejection:\n\n`, reason, promise);
});
process.on("uncaughtException", (reason, promise) => {
console.log(`🚫 Critical Error detected! Uncaught Exception:\n\n`, reason, promise);
});