@@ -2,14 +2,15 @@ import { AllyClient } from "./interfaces/AllyClient.js";
22import { config } from "./config.js" ; // Not ready to do this yet
33
44import {
5- GatewayIntentBits ,
6- Partials ,
7- Collection ,
8- Interaction ,
9- CacheType ,
5+ GatewayIntentBits ,
6+ Partials ,
7+ Collection ,
8+ Interaction
109} from "discord.js" ;
1110import * as fs from "fs" ;
1211import { Sequelize } from "sequelize" ;
12+ import { Listeners } from "./interfaces/Listeners" ;
13+ import path from "path" ;
1314
1415const client = new AllyClient ( {
1516 intents : config . intents as GatewayIntentBits [ ] ,
@@ -19,70 +20,80 @@ const client = new AllyClient({
1920client . commands = new Collection ( ) ;
2021client . DB = new Sequelize ( "sqlite::memory" ) ;
2122
22- fs . readdir (
23- "./dist/commands" ,
24- async ( err : NodeJS . ErrnoException | null , items : string [ ] ) => {
25- if ( err ) throw err ;
26- var baseFiles = items . filter ( ( files ) => files . split ( "." ) . pop ( ) === "js" ) ;
27- var folders = items . filter ( ( files ) => files . split ( "." ) . pop ( ) != "js" ) ;
23+ const commandsDirectory = path . join ( __dirname , "commands" ) ;
2824
29- // Load all commands from the base commands folder
30- baseFiles . forEach ( ( fileName ) => {
31- import ( `./commands/ ${ fileName } ` ) . then ( ( properties ) => {
32- let commandName = properties . SlashCommand . name . toLowerCase ( ) ;
25+ fs . readdir ( commandsDirectory , async ( err : NodeJS . ErrnoException | null , items : string [ ] ) => {
26+ if ( err ) throw err ;
27+ const baseFiles = filterNonJsFiles ( items ) ;
28+ const folders = items . filter ( ( files ) => files . split ( "." ) . pop ( ) != "js" ) ;
3329
34- client . commands . set ( commandName , properties . SlashCommand ) ;
30+ loadCommands ( baseFiles , commandsDirectory ) ;
3531
36- console . log ( `${ fileName } command loaded` ) ;
37- } ) ;
38- } ) ;
39-
40- // Load all commands from the subfolders
41- folders . forEach ( ( folderName ) => {
42- fs . readdir (
43- `./dist/commands/${ folderName } ` ,
44- async ( err : NodeJS . ErrnoException | null , subfolderItems : string [ ] ) => {
32+ folders . forEach ( ( folderName ) => {
33+ fs . readdir ( path . join ( commandsDirectory , folderName ) , async ( err : NodeJS . ErrnoException | null , subFolderItems : string [ ] ) => {
4534 if ( err ) throw err ;
46- var Files = subfolderItems . filter (
47- ( files ) => files . split ( "." ) . pop ( ) === "js"
48- ) ;
49-
50- Files . forEach ( ( fileName ) => {
51- import ( `./commands/${ folderName } /${ fileName } ` ) . then (
52- ( properties ) => {
53- let commandName : String =
54- properties . SlashCommand . name . toLowerCase ( ) ;
55-
56- client . commands . set ( commandName , properties . SlashCommand ) ;
57-
58- console . log ( `${ fileName } command loaded` ) ;
59- }
60- ) ;
61- } ) ;
35+ const Files = filterNonJsFiles ( subFolderItems ) ;
36+
37+ loadCommands ( Files , path . join ( commandsDirectory , folderName ) ) ;
6238 }
6339 ) ;
6440 } ) ;
6541 }
6642) ;
6743
44+ const listenersDirectory = path . join ( __dirname , "listeners" ) ;
45+
46+ fs . readdir ( listenersDirectory , async ( err : NodeJS . ErrnoException | null , items : string [ ] ) => {
47+ if ( err ) throw err ;
48+ const listeners = items . filter ( ( files ) => files . split ( "." ) . pop ( ) === "js" ) ;
49+
50+ listeners . forEach ( ( fileName : string ) => {
51+ import ( path . join ( listenersDirectory , fileName ) ) . then ( ( properties ) => {
52+ let listener :Listeners < any > = properties . Listener ;
53+
54+ client . on ( listener . event , ( args ) => {
55+ listener . run ( client , args ) ;
56+ } ) ;
57+ console . log ( `${ listener . purpose } listener was loaded` ) ;
58+ } ) ;
59+ } ) ;
60+ } ) ;
61+
6862client . on ( "ready" , ( ) => {
6963 console . log ( `Logged in as ${ client . user ?. tag } !` ) ;
7064} ) ;
7165
72- client . on ( "interactionCreate" , ( interaction : Interaction < CacheType > ) => {
66+ client . on ( "interactionCreate" , ( interaction : Interaction ) => {
7367 if ( ! interaction . isCommand ( ) ) return ;
7468
7569 let commandFile = client . commands . get ( interaction . commandName ) ;
7670
7771 if ( commandFile ) commandFile . run ( client , interaction ) ;
7872} ) ;
7973
80- client . on ( "interactionCreate" , ( interaction : Interaction < CacheType > ) => {
74+ client . on ( "interactionCreate" , ( interaction : Interaction ) => {
8175 if ( ! interaction . isButton ( ) ) return ;
8276
8377 let buttonFollowUp = client . commands . get ( interaction . customId . split ( "." ) [ 0 ] ) ;
8478
8579 if ( buttonFollowUp ) buttonFollowUp . followup ( client , interaction ) ;
8680} ) ;
8781
82+
8883client . login ( config . token ) ;
84+
85+ const loadCommands = ( files : string [ ] , location : string ) => {
86+ files . forEach ( ( fileName ) => {
87+ import ( path . join ( location , fileName ) ) . then ( ( properties ) => {
88+ let commandName = properties . SlashCommand . name . toLowerCase ( ) ;
89+
90+ client . commands . set ( commandName , properties . SlashCommand ) ;
91+
92+ console . log ( `${ fileName } command loaded` ) ;
93+ } ) ;
94+ } ) ;
95+ } ;
96+
97+ const filterNonJsFiles = ( items : string [ ] ) => {
98+ return items . filter ( ( files ) => files . split ( "." ) . pop ( ) === "js" ) ;
99+ } ;
0 commit comments