@@ -22,17 +22,33 @@ const client = new Client({
2222} ) ;
2323startServer ( Number ( port ) ) ;
2424
25- // Load commands dynamically
2625const commands : Record < string , ( msg : Message ) => void > = { } ;
2726const commandsPath = path . join ( __dirname , "commands" ) ;
28- fs . readdirSync ( commandsPath ) . forEach ( ( file ) => {
27+
28+ // Initial load
29+ const loadCommand = ( file : string ) => {
2930 if ( / \. j s $ | \. t s $ / . test ( file ) ) {
30- const commandModule = require ( path . join ( commandsPath , file ) ) ;
31+ const filePath = path . join ( commandsPath , file ) ;
32+ delete require . cache [ require . resolve ( filePath ) ] ;
33+ const commandModule = require ( filePath ) ;
3134 if ( commandModule . command && typeof commandModule . default === "function" ) {
3235 commands [ commandModule . command ] = commandModule . default ;
3336 log . info ( "Loader" , `Loaded command: ${ commandModule . command } ` ) ;
3437 }
3538 }
39+ }
40+
41+ fs . readdirSync ( commandsPath ) . forEach ( loadCommand ) ;
42+
43+ // Watch for changes
44+ fs . watch ( commandsPath , ( eventType , filename ) => {
45+ if ( filename && / \. j s $ | \. t s $ / . test ( filename ) ) {
46+ try {
47+ loadCommand ( filename ) ;
48+ } catch ( err ) {
49+ log . error ( "Loader" , `Failed to reload command: ${ filename } ` , err ) ;
50+ }
51+ }
3652} ) ;
3753
3854client . on ( "qr" , ( qr ) => {
@@ -76,3 +92,5 @@ const messageEvent = (msg: Message) => {
7692 if ( debug ) log . info ( "Message" , msg . body . slice ( 0 , 255 ) ) ;
7793 handler ( msg ) ;
7894} ;
95+
96+ export { commands }
0 commit comments