-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
63 lines (53 loc) · 2.25 KB
/
loader.ts
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
const fs = require("node:fs");
/**
* Loads all slash commands in the specified folder and sets them for the client application.
* @param {Client} client The Discord.js client object.
* @param {string} folderName The name of the folder containing the slash command files.
*/
const loadSlashCommands = async function (client: any, folderName: any) {
let slashCommands: any[] = [];
// Read all files in the folder that end with ".ts"
const commandFiles = fs
.readdirSync(`./${folderName}`)
.filter((file: string) => file.endsWith(".ts"));
// For each file, require and add the command to the client's collection
for (const file of commandFiles) {
const command = require(`./${folderName}/${file}`);
client.slashCommands.set(command.name, command);
slashCommands.push(command)
}
// Once the client is ready, set the loaded slash commands
client.on("ready", async () => {
await client.application.commands.set(slashCommands);
console.log(`Loaded ${slashCommands.length} slash commands.`);
});
};
/**
* Loads all event files in the specified folder and sets them to the client object.
* @param {Client} client The Discord.js client object.
* @param {string} folderName The name of the folder containing the event files.
*/
const loadEvents = async function (client: any, folderName: any) {
// Read all files in the folder that end with ".ts"
const eventFiles = fs
.readdirSync(`./${folderName}`)
.filter((file: string) => file.endsWith(".ts"));
// For each file, require and add the event to the client
for (const file of eventFiles) {
const event = require(`./${folderName}/${file}`);
// console.log(`Event ${file} loaded.`);
// Check if the event is a one-time event or not
if (event.once) {
// If it's a one-time event, add a listener using client.once
client.once(event.name, (...args: any) => event.execute(...args, client));
} else {
// If it's not a one-time event, add a listener using client.on
client.on(event.name, (...args: any) => event.execute(...args, client));
}
}
}
// Export the functions
module.exports = {
loadEvents,
loadSlashCommands,
};