@@ -2,14 +2,15 @@ import { AllyClient } from "./interfaces/AllyClient.js";
2
2
import { config } from "./config.js" ; // Not ready to do this yet
3
3
4
4
import {
5
- GatewayIntentBits ,
6
- Partials ,
7
- Collection ,
8
- Interaction ,
9
- CacheType ,
5
+ GatewayIntentBits ,
6
+ Partials ,
7
+ Collection ,
8
+ Interaction
10
9
} from "discord.js" ;
11
10
import * as fs from "fs" ;
12
11
import { Sequelize } from "sequelize" ;
12
+ import { Listeners } from "./interfaces/Listeners" ;
13
+ import path from "path" ;
13
14
14
15
const client = new AllyClient ( {
15
16
intents : config . intents as GatewayIntentBits [ ] ,
@@ -19,70 +20,80 @@ const client = new AllyClient({
19
20
client . commands = new Collection ( ) ;
20
21
client . DB = new Sequelize ( "sqlite::memory" ) ;
21
22
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" ) ;
28
24
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" ) ;
33
29
34
- client . commands . set ( commandName , properties . SlashCommand ) ;
30
+ loadCommands ( baseFiles , commandsDirectory ) ;
35
31
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 [ ] ) => {
45
34
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 ) ) ;
62
38
}
63
39
) ;
64
40
} ) ;
65
41
}
66
42
) ;
67
43
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
+
68
62
client . on ( "ready" , ( ) => {
69
63
console . log ( `Logged in as ${ client . user ?. tag } !` ) ;
70
64
} ) ;
71
65
72
- client . on ( "interactionCreate" , ( interaction : Interaction < CacheType > ) => {
66
+ client . on ( "interactionCreate" , ( interaction : Interaction ) => {
73
67
if ( ! interaction . isCommand ( ) ) return ;
74
68
75
69
let commandFile = client . commands . get ( interaction . commandName ) ;
76
70
77
71
if ( commandFile ) commandFile . run ( client , interaction ) ;
78
72
} ) ;
79
73
80
- client . on ( "interactionCreate" , ( interaction : Interaction < CacheType > ) => {
74
+ client . on ( "interactionCreate" , ( interaction : Interaction ) => {
81
75
if ( ! interaction . isButton ( ) ) return ;
82
76
83
77
let buttonFollowUp = client . commands . get ( interaction . customId . split ( "." ) [ 0 ] ) ;
84
78
85
79
if ( buttonFollowUp ) buttonFollowUp . followup ( client , interaction ) ;
86
80
} ) ;
87
81
82
+
88
83
client . 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