@@ -87,41 +87,18 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
8787 // Pipe the output of MatchMaker to a file
8888 const schedulePath = path . join ( process . cwd ( ) , "schedule.txt" ) ;
8989 const out = fsSync . createWriteStream ( schedulePath ) ;
90- matchMaker . stdout . pipe ( out as unknown as NodeJS . WritableStream ) ;
90+ matchMaker . stdout . pipe ( out as any ) ;
9191
92- // Wait for MatchMaker to finish or timeout
93- const TIMEOUT_MS = 15000 ;
92+ // Wait for MatchMaker to finish or timeout after 15 seconds
93+ await new Promise ( ( resolve ) => {
94+ const timeout = setTimeout ( ( ) => {
95+ resolve ( undefined ) ;
96+ } , 15000 ) ;
9497
95- // Resolve when the process exits/closes/errors
96- const waitForExit = new Promise < void > ( ( resolve ) => {
97- const done = ( ) => resolve ( ) ;
98- matchMaker . once ( "exit" , done ) ;
99- matchMaker . once ( "close" , done ) ;
100- matchMaker . once ( "error" , done ) ;
101- } ) ;
102-
103- // Hard timeout that force-kills the child on Windows/macOS/Linux
104- const timeout = new Promise < void > ( ( resolve ) => {
105- const t = setTimeout ( ( ) => {
106- if ( ! matchMaker . killed ) {
107- matchMaker . kill ( "SIGKILL" ) ;
108- }
109- resolve ( ) ;
110- } , TIMEOUT_MS ) ;
111- void waitForExit . then ( ( ) => clearTimeout ( t ) ) ;
112- } ) ;
113-
114- await Promise . race ( [ waitForExit , timeout ] ) ;
115-
116- // Ensure the output file stream has flushed and closed before reading
117- await new Promise < void > ( ( resolve , reject ) => {
118- out . once ( "finish" , ( ) => resolve ( ) ) ;
119- out . once ( "error" , ( err ) => reject ( err ) ) ;
120- } ) ;
121- await new Promise < void > ( ( resolve ) => {
122- // If already closed, resolve immediately
123- if ( out . closed === true ) return resolve ( ) ;
124- out . once ( "close" , ( ) => resolve ( ) ) ;
98+ matchMaker . once ( "close" , ( ) => {
99+ clearTimeout ( timeout ) ;
100+ resolve ( undefined ) ;
101+ } ) ;
125102 } ) ;
126103
127104 interaction . editReply (
@@ -130,29 +107,27 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
130107
131108 // Handle the output of MatchMaker
132109 const schedule = await fs . readFile ( schedulePath , "utf-8" ) ;
133- const lines = schedule . split ( / \r ? \n / ) ;
110+ const lines = schedule . split ( "\n" ) ;
134111
135112 let i = 0 ;
136113
137114 // Skip over lines until we get to the schedule
138- while ( ( lines [ i ] ?? "" ) . trim ( ) !== "--------------" ) {
115+ while ( lines [ i ] !== "--------------" ) {
139116 i ++ ;
140- if ( i >= lines . length ) break ;
141117 }
142118 i ++ ;
143119
144120 // Read each line of the schedule
145121 const players = [ ...participantRole . members . values ( ) ] ;
146122 const matches = [ ] as { number : number ; teams : string [ ] } [ ] ;
147- while ( ( lines [ i ] ?? "" ) . trim ( ) !== "" ) {
148- const match = ( lines [ i ] ?? "" ) . trim ( ) . split ( / \s + / ) ;
123+ while ( lines [ i ] !== "" ) {
124+ const match = lines [ i ] . trim ( ) . split ( / \s + / ) ;
149125
150126 const number = parseInt ( match . shift ( ) ?. replace ( ":" , "" ) ?? "0" ) ;
151127 const teams = match . map ( ( team ) => players [ parseInt ( team ) - 1 ] . id ) ;
152128
153129 matches . push ( { number, teams } ) ;
154130 i ++ ;
155- if ( i >= lines . length ) break ;
156131 }
157132
158133 // Delete the schedule file
@@ -182,20 +157,18 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
182157 ) ;
183158
184159 // Post the schedule to Google Sheets
185- try {
186- await postSchedule (
187- interaction . client . scheduleSheet ,
188- matches ,
189- playerIds ,
190- playerNames
191- ) ;
192- } catch ( err ) {
160+ await postSchedule (
161+ interaction . client . scheduleSheet ,
162+ matches ,
163+ playerIds ,
164+ playerNames
165+ ) . catch ( ( err ) => {
193166 logger . error ( err ) ;
194- await interaction . editReply (
167+ interaction . editReply (
195168 "Failed to save the schedule to Google Sheets (check the logs)"
196169 ) ;
197170 return ;
198- }
171+ } ) ;
199172
200173 const sheetsUrl = `https://docs.google.com/spreadsheets/d/${ process . env . GOOGLE_SHEET_DOC_ID } ` ;
201174 await interaction . editReply (
0 commit comments