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+ }
0 commit comments