@@ -236,108 +236,124 @@ export async function handleTwoPlayerMatchVoting(
236236 ) => { } ,
237237 } ,
238238) {
239- const embed = interaction . message . embeds [ 0 ]
240- if ( ! embed ) return console . error ( 'No embed found in message' )
241- const fields = embed . data . fields
242- if ( ! fields ) return console . error ( 'No fields found in embed' )
243-
244- const winMatchData : string [ ] = interaction . values [ 0 ] . split ( '_' )
245- const winMatchTeamId = parseInt ( winMatchData [ 2 ] )
246- const matchId = await getMatchIdFromMessage ( interaction )
247- if ( ! matchId ) return console . error ( 'No match found for this channel' )
248-
249- // Restrict to allowed voters
250- if ( participants . length && ! participants . includes ( interaction . user . id ) ) {
251- return interaction . reply ( {
252- content : `You are not allowed to vote in this poll.` ,
253- flags : MessageFlags . Ephemeral ,
254- } )
255- }
256-
257- // Get current user vote from database
258- const currentVote = await getUserVote ( matchId , interaction . user . id )
259-
260- // Check if user already voted for this team
261- const userAlreadyVotedForThisTeam =
262- currentVote &&
263- currentVote . vote_type === 'win' &&
264- currentVote . vote_value === winMatchTeamId
265-
266- // If user already voted for this team, remove the vote
267- if ( userAlreadyVotedForThisTeam ) {
268- await removeUserVote ( matchId , interaction . user . id )
269- } else {
270- // Otherwise, update their vote (this will replace any existing vote)
271- await setUserVote ( matchId , interaction . user . id , 'win' , winMatchTeamId )
272- }
273-
274- // Get updated votes from database grouped by team
275- const votesByTeam = await getVotesForMatchByTeam ( matchId , 'win' )
276-
277- // Update the embed for display
278- const voteArray : { team_id : number ; votes : string [ ] } [ ] = [ ]
279- for ( let i = 0 ; i < fields . length ; i ++ ) {
280- if (
281- fields [ i ] . name . includes ( 'Cancel Match' ) ||
282- fields [ i ] . name . includes ( 'Votes' )
283- )
284- continue
239+ try {
240+ const embed = interaction . message . embeds [ 0 ]
241+ if ( ! embed ) return console . error ( 'No embed found in message' )
242+ const fields = embed . data . fields
243+ if ( ! fields ) return console . error ( 'No fields found in embed' )
244+
245+ const winMatchData : string [ ] = interaction . values [ 0 ] . split ( '_' )
246+ const winMatchTeamId = parseInt ( winMatchData [ 2 ] )
247+ const matchId = await getMatchIdFromMessage ( interaction )
248+ if ( ! matchId ) return console . error ( 'No match found for this channel' )
249+
250+ // Restrict to allowed voters
251+ if ( participants . length && ! participants . includes ( interaction . user . id ) ) {
252+ return interaction . reply ( {
253+ content : `You are not allowed to vote in this poll.` ,
254+ flags : MessageFlags . Ephemeral ,
255+ } )
256+ }
285257
286- const lines = fields [ i ] . value ?. split ( '\n' ) || [ ]
287- const mmrLine = lines . find ( ( l ) => l . includes ( 'MMR' ) ) || ''
258+ // Get current user vote from database
259+ const currentVote = await getUserVote ( matchId , interaction . user . id )
260+
261+ // Check if user already voted for this team
262+ const userAlreadyVotedForThisTeam =
263+ currentVote &&
264+ currentVote . vote_type === 'win' &&
265+ currentVote . vote_value === winMatchTeamId
266+
267+ // If user already voted for this team, remove the vote
268+ if ( userAlreadyVotedForThisTeam ) {
269+ await removeUserVote ( matchId , interaction . user . id )
270+ } else {
271+ // Otherwise, update their vote (this will replace any existing vote)
272+ await setUserVote ( matchId , interaction . user . id , 'win' , winMatchTeamId )
273+ }
288274
289- // Get votes for this team from database (teams start at 1, array index starts at 0)
290- const teamId = i + 1
291- const teamVotes = votesByTeam . get ( teamId ) || [ ]
292- const voteLines = teamVotes . map ( ( uid ) => `<@${ uid } >` )
275+ // Get updated votes from database grouped by team
276+ const votesByTeam = await getVotesForMatchByTeam ( matchId , 'win' )
277+
278+ // Update the embed for display
279+ const voteArray : { team_id : number ; votes : string [ ] } [ ] = [ ]
280+ for ( let i = 0 ; i < fields . length ; i ++ ) {
281+ if (
282+ fields [ i ] . name . includes ( 'Cancel Match' ) ||
283+ fields [ i ] . name . includes ( 'Votes' )
284+ )
285+ continue
286+
287+ const lines = fields [ i ] . value ?. split ( '\n' ) || [ ]
288+ const mmrLine = lines . find ( ( l ) => l . includes ( 'MMR' ) ) || ''
289+
290+ // Get votes for this team from database (teams start at 1, array index starts at 0)
291+ const teamId = i + 1
292+ const teamVotes = votesByTeam . get ( teamId ) || [ ]
293+ const voteLines = teamVotes . map ( ( uid ) => `<@${ uid } >` )
294+
295+ let newValue = mmrLine
296+ if ( voteLines . length > 0 ) {
297+ newValue += `\nWin Votes`
298+ newValue += '\n' + voteLines . join ( '\n' )
299+ }
293300
294- let newValue = mmrLine
295- if ( voteLines . length > 0 ) {
296- newValue += `\nWin Votes`
297- newValue += '\n' + voteLines . join ( '\n' )
301+ fields [ i ] . value = newValue || '\u200b'
302+ voteArray . push ( { team_id : i , votes : voteLines } )
298303 }
299304
300- fields [ i ] . value = newValue || '\u200b'
301- voteArray . push ( { team_id : i , votes : voteLines } )
302- }
303-
304- interaction . message . embeds [ 0 ] = embed
305+ interaction . message . embeds [ 0 ] = embed
305306
306- // Check if all participants voted
307- const totalVotes = voteArray . reduce ( ( sum , team ) => sum + team . votes . length , 0 )
308- const allVoted = participants . length > 0 && totalVotes === participants . length
309-
310- if ( allVoted ) {
311- // Check majority
312- const majority = Math . floor ( participants . length / 2 ) + 1
313- let winner : number | undefined
314- for ( let i = 0 ; i < voteArray . length ; i ++ ) {
315- if ( voteArray [ i ] . votes . length >= majority ) {
316- winner = i + 1 // Teams start at 1
307+ // Check if all participants voted
308+ const totalVotes = voteArray . reduce (
309+ ( sum , team ) => sum + team . votes . length ,
310+ 0 ,
311+ )
312+ const allVoted =
313+ participants . length > 0 && totalVotes === participants . length
314+
315+ if ( allVoted ) {
316+ // Check majority
317+ const majority = Math . floor ( participants . length / 2 ) + 1
318+ let winner : number | undefined
319+ for ( let i = 0 ; i < voteArray . length ; i ++ ) {
320+ if ( voteArray [ i ] . votes . length >= majority ) {
321+ winner = i + 1 // Teams start at 1
322+ }
317323 }
318- }
319324
320- if ( winner ) {
321- await onComplete ( interaction , winner )
322- return
325+ if ( winner ) {
326+ await onComplete ( interaction , winner )
327+ return
328+ }
323329 }
324- }
325330
326- // Acknowledge the interaction first
327- await interaction . deferUpdate ( )
331+ // Acknowledge the interaction first
332+ await interaction . deferUpdate ( )
328333
329- // Delete the old message and send a new one
330- const channel = interaction . channel as GuildChannel
331- const components = interaction . message . components
332- await interaction . message . delete ( )
334+ // Delete the old message and send a new one
335+ const channel = interaction . channel as GuildChannel
336+ const components = interaction . message . components
337+ await interaction . message . delete ( )
333338
334- if ( channel && channel . isTextBased ( ) ) {
335- const newMessage = await channel . send ( {
336- embeds : [ embed ] ,
337- components : components ,
338- } )
339+ if ( channel && channel . isTextBased ( ) ) {
340+ const newMessage = await channel . send ( {
341+ embeds : [ embed ] ,
342+ components : components ,
343+ } )
339344
340- setLastWinVoteMessage ( channel . id , newMessage . id )
345+ setLastWinVoteMessage ( channel . id , newMessage . id )
346+ }
347+ } catch ( err ) {
348+ const channel = interaction . channel as GuildChannel
349+ if ( channel && channel . isTextBased ( ) ) {
350+ channel . send (
351+ 'Failed to handle voting. Please ping Jeff or Cas about this. Error: ' +
352+ err ,
353+ )
354+ } else {
355+ console . error ( 'Failed to handle voting. Error: ' + err )
356+ }
341357 }
342358}
343359
0 commit comments