-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.js
More file actions
278 lines (240 loc) · 10.9 KB
/
Copy pathindex.js
File metadata and controls
278 lines (240 loc) · 10.9 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(async () => {
const Discord = require("discord.js");
const dotenv = require("dotenv");
dotenv.config({ quiet: true });
require("module-alias/register");
const cron = require("cron");
const Sentry = require("@sentry/node");
if (process.env.SERVER == "prod" && process.env.SENTRY_DSN) {
const { version } = require("@root/package.json");
const packageJSONVersion = version.split(".");
Sentry.init({
dsn: process.env.SENTRY_DSN,
release: packageJSONVersion,
});
}
const logger = require("@utils/log");
console.warner = console.warn;
console.logger = console.log;
console.log = (message, ...args) => logger.console(message, ...args);
console.warn = (message, ...args) => logger.warning(message, ...args);
console.error = (message, ...args) => logger.error(message, ...args);
console.info = (message, ...args) => logger.info(message, ...args);
console.debug = (message, ...args) => logger.debug(message, ...args);
console.log("Logger instanciated");
const { Client, GatewayIntentBits, Partials } = require("discord.js");
const { getPermissionArrayNames } = require("@functions/discordFunctions");
const config = require("@utils/config/configUtils");
const fs = require("fs").promises;
const path = require("path");
const neededIntents = {
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildExpressions,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
],
partials: [
Partials.Message,
Partials.Channel,
Partials.Reaction,
],
};
const client = new Client({
...neededIntents,
shards: "auto",
allowedMentions: { repliedUser: false },
});
const { registerExtractors, initPlayer, reload } = require("@root/utils/helpers/player/registerExtractors");
const player = await initPlayer(client);
await registerExtractors(player);
console.log(`Daily reregistering ${config.get("discordPlayer")?.dailyReregister ? "enabled" : "disabled"}`);
new cron.CronJob(config.get("cronJobs").dailyReregister, async () => {
if (config.get("discordPlayer")?.dailyReregister) await reload(player);
}, null, true, config.get("timeZone"));
global.wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
// Add array.equals()
Array.prototype.equals = function(otherArray) {
return this.length === otherArray.length && this.every((value, index) => value === otherArray[index]);
};
// Add array.shuffle()
Array.prototype.shuffle = function() {
for (let i = this.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this[i], this[j]] = [this[j], this[i]];
}
return this;
};
console.log("Variables loaded");
module.exports = { client };
const EmbedGenerator = require("@utils/helpers/embedGenerator");
// Collections creation
client.commands = new Discord.Collection();
client.slashcommands = new Discord.Collection();
client.contextcommands = new Discord.Collection();
client.consoleCommands = new Discord.Collection();
client.TextCooldowns = new Map();
client.SlashCooldowns = new Map();
const permissionBitFields = [];
/**
* Asynchronously load JavaScript files from a folder (recursive)
*
* @param {string} folder The folder to load files from
* @param {Function} callback The callback function (can be async)
*
* @returns {Promise<void>}
*/
async function loadFiles(folder, callback) {
const commandFiles = await fs.readdir(folder);
while (commandFiles.length > 0) {
const file = commandFiles.shift();
const fullPath = path.join(folder, file);
const absolutePath = path.resolve(fullPath);
const stat = await fs.lstat(absolutePath);
if (stat.isDirectory()) {
const newFiles = await fs.readdir(absolutePath);
newFiles.forEach(f => commandFiles.push(path.join(file, f)));
} else if (file.endsWith(".js")) {
try {
const loaded = require(absolutePath);
loaded.filePath = absolutePath;
if (loaded.loadFileIgnore) continue;
await callback(loaded, file);
} catch (err) {
console.error(`Failed to load ${absolutePath}:`, err);
}
}
}
}
await loadFiles("./utils/validators/", async (validator) => {
if (!validator.execute) {
logger.severe(`Validator [${validator.filePath}] is missing an execute function`);
process.exit(0);
}
try {
await validator.execute();
} catch (error) {
logger.severe(`Validator [${validator.filePath}] failed to execute`);
logger.error(error);
process.exit(0);
}
});
// Slash command handler
client.discoveredCommands = [];
await loadFiles("./commands/slash/", (slashcommand, fileName) => {
if ("name" in slashcommand && "execute" in slashcommand && "description" in slashcommand) {
if (client.slashcommands.get(slashcommand.name)) throw new Error(`Slash command or alias [${slashcommand.name}] already exists`);
client.slashcommands.set(slashcommand.name, slashcommand);
client.discoveredCommands.push(slashcommand);
} else {
logger.error(`[WARNING] The (/) command ${fileName} is missing a required "name", "execute", or "type" property.`);
}
});
// Text command handler
await loadFiles("./commands/text/", (command) => {
try {
if (command.description.length > 100) throw new Error(`Text command [${command.name}] description is too long (${command.description.length} characters, max 100)\n${command.filePath}`);
command.isAlias = false;
command.lastExecutionTime = 1000;
if (client.commands.get(command.name)) throw new Error(`Text command [${command.name}] already exists\n${command.filePath}`);
client.commands.set(command.name, command);
if (!command.cooldown) command.cooldown = config.get("baseCommandCooldown") || 3000;
if (command.aliases && Array.isArray(command.aliases)) {
command.isAlias = true;
command.aliases.forEach(alias => {
if (client.commands.get(alias)) throw new Error(`Text command alias [${alias}] already exists\n${command.filePath}`);
client.commands.set(alias, command);
});
}
} catch (error) {
logger.error(`Failed to load text command [${command.name}]: ${error.message}`);
process.exit(0);
}
if (command.permissions) permissionBitFields.push(...command.permissions);
});
console.logger([
"--------------------------------------------------",
"Required permissions:",
"--------------------------------------------------",
`${getPermissionArrayNames(permissionBitFields).join("\n")}`,
"--------------------------------------------------",
].join("\n"));
// Context menu command handler
await loadFiles("./commands/context/", (contextcommand, fileName) => {
if ("name" in contextcommand && "execute" in contextcommand && "type" in contextcommand) {
if (client.contextcommands.get(contextcommand.name)) throw new Error(`Context command or alias [${contextcommand.name}] already exists`);
client.contextcommands.set(contextcommand.name, contextcommand);
client.discoveredCommands.push(contextcommand);
} else {
logger.error(`[WARNING] The (ctx) command ${fileName} is missing a required "name", "execute", or "type" property.`);
}
});
// Event handler
await loadFiles("./events/client/", (event) => {
if (event.once) {
client.once(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
} else {
client.on(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
}
});
await loadFiles("./events/process/", (event) => {
process.on(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
});
await loadFiles("./events/rest/", (event) => {
client.rest.on(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
});
await loadFiles("./events/player/", (event) => {
player.on(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
});
await loadFiles("./events/playerEvents/", (event) => {
player.events.on(event.name, async (...args) => {
if (event.log) logger.event(`Event: [${event.name}] fired.`);
await event.execute(client, logger, ...args);
});
});
process.stdin.setEncoding("utf8");
await loadFiles("./events/console/", (event) => {
if (client.consoleCommands.get(event.name)) throw new Error(`Command or alias [${event.name}] already exists`);
client.consoleCommands.set(event.name, event);
});
process.stdin.on("data", async (input) => {
const trimmedInput = input.trim();
process.stdout.write(trimmedInput + "\n");
const args = trimmedInput.split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.consoleCommands.get(commandName);
if (!command) return;
process.stdout.write("\u001b[1A\u001b[2K");
await console.logger(`
---------------------------
Executing [${trimmedInput}]
by [CONSOLE]
---------------------------`
.replace(/^\s+/gm, ""));
await command.execute(client, logger, args);
});
// Logins with the token
await client.login(process.env.SERVER === "dev" && process.env.DEV_TOKEN ? process.env.DEV_TOKEN : process.env.TOKEN);
})();