Skip to content

Commit 7564cd8

Browse files
committed
Update handler
1 parent 0f68137 commit 7564cd8

File tree

2 files changed

+110
-57
lines changed

2 files changed

+110
-57
lines changed

handler/index.js

Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,101 @@
1-
const { glob } = require("glob");
2-
const { promisify } = require("util");
3-
const { Client } = require("discord.js");
4-
const globPromise = promisify(glob);
1+
const fs = require("fs");
2+
const chalk = require("chalk");
53

6-
// This logs commands and events
7-
8-
module.exports = async (client) => {
9-
// Load Commands
10-
const commandFiles = await globPromise(`${process.cwd()}/commands/**/*.js`);
11-
commandFiles.map((value) => {
12-
const file = require(value);
13-
const splitted = value.split("/");
14-
const directory = splitted[splitted.length - 2];
4+
/**
5+
* Load Events
6+
*/
7+
const loadEvents = async function (client) {
8+
const eventFolders = fs.readdirSync("./events");
9+
for (const folder of eventFolders) {
10+
const eventFiles = fs
11+
.readdirSync(`./event/${folder}`)
12+
.filter((file) => file.endsWith(".js"));
13+
14+
for (const file of eventFiles) {
15+
const event = require(`../event/${folder}/${file}`);
16+
17+
if (event.name) {
18+
console.log(chalk.bgBlueBright.black(` ✔️ => Event ${file} is being loaded `));
19+
} else {
20+
console.log(chalk.bgRedBright.black(` ❌ => Event ${file} missing a help.name or help.name is not in string `));
21+
continue;
22+
}
23+
24+
if (event.once) {
25+
client.once(event.name, (...args) => event.execute(...args, client));
26+
} else {
27+
client.on(event.name, (...args) => event.execute(...args, client));
28+
}
29+
}
30+
}
31+
}
1532

16-
if (file.name) {
17-
const properties = { directory, ...file };
18-
client.commands.set(file.name, properties);
33+
/**
34+
* Load Prefix Commands
35+
*/
36+
const loadCommands = async function (client) {
37+
const commandFolders = fs.readdirSync("./commands");
38+
for (const folder of commandFolders) {
39+
const commandFiles = fs
40+
.readdirSync(`./commands/${folder}`)
41+
.filter((file) => file.endsWith(".js"));
42+
43+
for (const file of commandFiles) {
44+
const command = require(`../commands/${folder}/${file}`);
45+
46+
if (command.name) {
47+
client.commands.set(command.name, command);
48+
console.log(chalk.bgBlueBright.black(` ✔️ => Prefix Command ${file} is being loaded `));
49+
} else {
50+
console.log(chalk.bgRedBright.black(` ❌ => Prefix Command ${file} missing a help.name or help.name is not in string `));
51+
continue;
52+
}
53+
54+
if (command.aliases && Array.isArray(command))
55+
command.aliases.forEach((alias) => client.aliases.set(alias, command.name));
1956
}
20-
});
57+
}
58+
}
2159

22-
// Load Events
23-
const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
24-
eventFiles.map((value) => require(value));
60+
/**
61+
* Load SlashCommands
62+
*/
63+
const loadSlashCommands = async function (client) {
64+
let slash = []
2565

26-
// Load SlashCommands
27-
const slashCommands = await globPromise(
28-
`${process.cwd()}/slashcommands/**/*.js`
29-
);
66+
const commandFolders = fs.readdirSync("./slashCommands");
67+
for (const folder of commandFolders) {
68+
const commandFiles = fs
69+
.readdirSync(`./slashCommands/${folder}`)
70+
.filter((file) => file.endsWith(".js"));
71+
72+
for (const file of commandFiles) {
73+
const command = require(`../slashCommands/${folder}/${file}`);
74+
75+
if (command.name) {
76+
client.slash.set(command.name, command);
77+
slash.push(command)
78+
console.log(chalk.bgBlueBright.black(` ✔️ => SlashCommand ${file} is being loaded `));
79+
} else {
80+
console.log(chalk.bgRedBright.black(` ❌ => SlashCommand ${file} missing a help.name or help.name is not in string `));
81+
continue;
82+
}
83+
}
84+
}
3085

31-
const arrayOfSlashCommands = [];
32-
slashCommands.map((value) => {
33-
const file = require(value);
34-
if (!file.name) return;
35-
client.slashCommands.set(file.name, file);
36-
arrayOfSlashCommands.push(file);
37-
});
38-
client.on("ready", async () => {
86+
client.on("ready", async() => {
3987
// Register Slash Commands for a single guild
4088
// await client.guilds.cache
4189
// .get("YOUR_GUILD_ID")
42-
// .commands.set(arrayOfSlashCommands);
90+
// .commands.set(slash);
4391

4492
// Register Slash Commands for all the guilds
45-
await client.application.commands.set(arrayOfSlashCommands);
46-
});
47-
};
93+
await client.application.commands.set(slash)
94+
})
95+
}
96+
97+
module.exports = {
98+
loadEvents,
99+
loadCommands,
100+
loadSlashCommands
101+
}

index.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
const {
2-
Client,
3-
Collection,
4-
Intents
5-
} = require('discord.js');
1+
const { Client, Collection, Intents } = require('discord.js');
2+
const handler = require("./handler/index");
63

74
const client = new Client({
85
intents: [
9-
"GUILDS",
10-
"GUILD_MEMBERS",
11-
"GUILD_BANS",
12-
"GUILD_INTEGRATIONS",
13-
"GUILD_WEBHOOKS",
14-
"GUILD_INVITES",
15-
"GUILD_VOICE_STATES",
16-
"GUILD_PRESENCES",
17-
"GUILD_MESSAGES",
18-
"GUILD_MESSAGE_REACTIONS",
19-
"GUILD_MESSAGE_TYPING",
20-
"DIRECT_MESSAGES",
21-
"DIRECT_MESSAGE_REACTIONS",
22-
"DIRECT_MESSAGE_TYPING",
6+
Intents.FLAGS.GUILDS,
7+
Intents.FLAGS.GUILD_MEMBERS,
8+
Intents.FLAGS.GUILD_BANS,
9+
Intents.FLAGS.GUILD_INTEGRATIONS,
10+
Intents.FLAGS.GUILD_WEBHOOKS,
11+
Intents.FLAGS.GUILD_INVITES,
12+
Intents.FLAGS.GUILD_VOICE_STATES,
13+
Intents.FLAGS.GUILD_PRESENCES,
14+
Intents.FLAGS.GUILD_MESSAGES,
15+
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
16+
Intents.FLAGS.GUILD_MESSAGE_TYPING,
17+
Intents.FLAGS.DIRECT_MESSAGES,
18+
Intents.FLAGS.DIRECT_MESSAGE_REACTIONS,
19+
Intents.FLAGS.DIRECT_MESSAGE_TYPING,
2320
],
2421
});
2522

@@ -33,11 +30,13 @@ module.exports = client;
3330
// Global Variables
3431
client.discord = Discord;
3532
client.commands = new Collection();
36-
client.slashCommands = new Collection();
33+
client.slash = new Collection();
3734
client.config = require('./config.json')
3835

3936
// Records commands and events
40-
require("./handler")(client);
37+
handler.loadEvents(client);
38+
handler.loadCommands(client);
39+
handler.loadSlashCommands(client);
4140

4241
// Login Discord Bot Token
4342
client.login(process.env.TOKEN);

0 commit comments

Comments
 (0)