@@ -7,6 +7,7 @@ import util from "util";
77import { Message } from "../../../types/message" ;
88const execPromise = util . promisify ( exec ) ;
99const basePath = path . join ( __dirname , ".." , ".." , ".." , "commands" ) ;
10+ import * as Sentry from "@sentry/node" ;
1011
1112export const commandDirs = [ basePath , path . join ( basePath , "private" ) ] ;
1213export const commands : Record <
@@ -41,6 +42,7 @@ async function ensureDependencies(
4142 if ( stdout ) log . info ( "npm" , stdout ) ;
4243 if ( stderr ) log . error ( "npm" , stderr ) ;
4344 } catch ( err ) {
45+ Sentry . captureException ( err ) ;
4446 log . error (
4547 "Loader" ,
4648 `Failed to install ${ dep . name } @${ dep . version } ` ,
@@ -55,92 +57,102 @@ export default async function loader(
5557 file : string ,
5658 customPath : string ,
5759) : Promise < void > {
58- if ( / \. j s $ | \. t s $ / . test ( file ) ) {
59- const filePath = path . join ( customPath , file ) ;
60+ try {
61+ if ( / \. j s $ | \. t s $ / . test ( file ) ) {
62+ const filePath = path . join ( customPath , file ) ;
6063
61- const resolvedPath = path . resolve ( filePath ) ;
62- if ( require . cache [ resolvedPath ] ) {
63- delete require . cache [ resolvedPath ] ;
64- }
65-
66- try {
67- await fs . access ( resolvedPath ) ;
68- } catch {
69- return ;
70- }
71-
72- const commandModule = await import ( filePath ) ;
64+ const resolvedPath = path . resolve ( filePath ) ;
65+ if ( require . cache [ resolvedPath ] ) {
66+ delete require . cache [ resolvedPath ] ;
67+ }
7368
74- if (
75- typeof commandModule . default === "function" &&
76- commandModule . info &&
77- commandModule . info . command
78- ) {
79- if ( Array . isArray ( commandModule . info . dependencies ) ) {
80- await ensureDependencies ( commandModule . info . dependencies ) ;
69+ try {
70+ await fs . access ( resolvedPath ) ;
71+ } catch {
72+ return ;
8173 }
8274
83- commands [ commandModule . info . command ] = {
84- command : commandModule . info . command ,
85- description : commandModule . info . description || "No description" ,
86- usage : commandModule . info . usage || "No usage" ,
87- example : commandModule . info . example || "No example" ,
88- role : commandModule . info . role || "user" ,
89- cooldown : commandModule . info . cooldown || 5000 ,
90- exec : commandModule . default ,
91- } ;
75+ const commandModule = await import ( filePath ) ;
76+
77+ if (
78+ typeof commandModule . default === "function" &&
79+ commandModule . info &&
80+ commandModule . info . command
81+ ) {
82+ if ( Array . isArray ( commandModule . info . dependencies ) ) {
83+ await ensureDependencies ( commandModule . info . dependencies ) ;
84+ }
85+
86+ commands [ commandModule . info . command ] = {
87+ command : commandModule . info . command ,
88+ description : commandModule . info . description || "No description" ,
89+ usage : commandModule . info . usage || "No usage" ,
90+ example : commandModule . info . example || "No example" ,
91+ role : commandModule . info . role || "user" ,
92+ cooldown : commandModule . info . cooldown || 5000 ,
93+ exec : commandModule . default ,
94+ } ;
95+ }
9296 }
97+ } catch ( err ) {
98+ Sentry . captureException ( err ) ;
99+ log . error ( "Loader" , `Failed to load: ${ file } ` , err ) ;
93100 }
94101}
95102
96103export async function mapCommands ( ) : Promise < void > {
97104 let allFiles : [ string , string ] [ ] = [ ] ;
98105
99- for ( const dir of commandDirs ) {
100- try {
101- await fs . access ( dir ) ;
102- const files = await fs . readdir ( dir ) ;
106+ try {
107+ for ( const dir of commandDirs ) {
108+ try {
109+ await fs . access ( dir ) ;
110+ const files = await fs . readdir ( dir ) ;
103111
104- const validFiles = files . filter (
105- ( f ) => f . endsWith ( ".js" ) || f . endsWith ( ".ts" ) ,
106- ) ;
112+ const validFiles = files . filter (
113+ ( f ) => f . endsWith ( ".js" ) || f . endsWith ( ".ts" ) ,
114+ ) ;
107115
108- const tuples : [ string , string ] [ ] = validFiles . map (
109- ( f ) => [ f , dir ] as [ string , string ] ,
110- ) ;
116+ const tuples : [ string , string ] [ ] = validFiles . map (
117+ ( f ) => [ f , dir ] as [ string , string ] ,
118+ ) ;
111119
112- allFiles = [ ...allFiles , ...tuples ] ;
113- } catch ( err : any ) {
114- if ( err . code === "ENOENT" ) {
115- console . log ( "" ) ;
116- log . warn ( "Loader" , `Directory not found: ${ dir } ` ) ;
117- } else {
118- console . log ( "" ) ;
119- log . warn ( "Loader" , `Error reading directory ${ dir } :` , err ) ;
120+ allFiles = [ ...allFiles , ...tuples ] ;
121+ } catch ( err : any ) {
122+ if ( err . code === "ENOENT" ) {
123+ console . log ( "" ) ;
124+ log . warn ( "Loader" , `Directory not found: ${ dir } ` ) ;
125+ } else {
126+ console . log ( "" ) ;
127+ log . warn ( "Loader" , `Error reading directory ${ dir } :` , err ) ;
128+ }
120129 }
121130 }
122- }
123131
124- const total = allFiles . length ;
125- if ( total === 0 ) {
126- log . info ( "Loader" , "No commands found." ) ;
127- return ;
128- }
132+ const total = allFiles . length ;
133+ if ( total === 0 ) {
134+ log . info ( "Loader" , "No commands found." ) ;
135+ return ;
136+ }
129137
130- const bar = LoadingBar (
131- "Loading Commands | {bar} | {value}/{total} {command}" ,
132- ) ;
133- bar . start ( total , 0 , { command : "" } ) ;
138+ const bar = LoadingBar (
139+ "Loading Commands | {bar} | {value}/{total} {command}" ,
140+ ) ;
141+ bar . start ( total , 0 , { command : "" } ) ;
134142
135- for ( const [ file , dir ] of allFiles ) {
136- try {
137- await loader ( file , dir ) ;
138- bar . increment ( { command : file } ) ;
139- } catch ( err ) {
140- log . error ( "Loader" , `Failed to load ${ file } ` , err ) ;
141- bar . increment ( { command : file } ) ;
143+ for ( const [ file , dir ] of allFiles ) {
144+ try {
145+ await loader ( file , dir ) ;
146+ bar . increment ( { command : file } ) ;
147+ } catch ( err ) {
148+ log . error ( "Loader" , `Failed to load ${ file } ` , err ) ;
149+ bar . increment ( { command : file } ) ;
150+ }
142151 }
143- }
144152
145- bar . stop ( ) ;
153+ bar . stop ( ) ;
154+ } catch ( err ) {
155+ Sentry . captureException ( err ) ;
156+ log . error ( "Loader" , "Failed to map commands:" , err ) ;
157+ }
146158}
0 commit comments