@@ -318,37 +318,56 @@ async def remove_unmatched_command(self, interaction: discord.Interaction, categ
318318 participants , db_users
319319 )
320320
321+ # Track all matched Discord IDs to avoid removing matched users
322+ matched_discord_ids = set ()
323+
324+ # Add exact matches
325+ for match in exact_matches :
326+ matched_discord_ids .add (match ["discord_id" ])
327+
328+ # Add name-only matches - these are also valid matches
329+ for match in name_only_matches :
330+ matched_discord_ids .add (match ["discord_id" ])
331+
321332 users_to_remove = []
322333 description = ""
323334
324335 # Build list of users to remove based on category
325336 if category == "all" :
326- # All unmatched = unmatched DB users + ambiguous matches
327- users_to_remove .extend ([u ["discord_id" ] for u in unmatched_db_users ])
328- for match in ambiguous_matches :
329- for potential in match ["potential_matches" ]:
330- users_to_remove .append (potential ["discord_id" ])
331- description = "Removing all unmatched users and ambiguous matches"
337+ # Only include users that aren't in exact or name-only matches
338+ for user in db_users :
339+ if user ["user_id" ] not in matched_discord_ids :
340+ users_to_remove .append (user ["user_id" ])
341+ description = "Removing all unmatched users"
332342 elif category == "unmatched_db" :
333- users_to_remove .extend ([u ["discord_id" ] for u in unmatched_db_users ])
343+ # Only include users that aren't in exact or name-only matches
344+ for user in unmatched_db_users :
345+ if user ["discord_id" ] not in matched_discord_ids :
346+ users_to_remove .append (user ["discord_id" ])
334347 description = "Removing unmatched database users"
335348 elif category == "unmatched_participants" :
336- # These are in unmatched_participants but we need to find their discord IDs from db
337- participant_names = {p ["name" ].lower () for p in unmatched_participants }
349+ # Get the Discord IDs of users who are registered with usernames that match unmatched participants
350+ participant_names = {p ["name" ].lower (). split ( '#' )[ 0 ] for p in unmatched_participants }
338351 for user in db_users :
339- if user .get ("matcherino_username" , "" ).lower () in participant_names :
352+ user_matcherino = user .get ("matcherino_username" , "" ).lower ().split ('#' )[0 ]
353+ if user_matcherino in participant_names and user ["user_id" ] not in matched_discord_ids :
340354 users_to_remove .append (user ["user_id" ])
341355 description = "Removing unmatched Matcherino participants"
342356 elif category == "ambiguous" :
357+ # Only include ambiguous matches if they're not exact or name-only matches
343358 for match in ambiguous_matches :
344359 for potential in match ["potential_matches" ]:
345- users_to_remove .append (potential ["discord_id" ])
360+ if potential ["discord_id" ] not in matched_discord_ids :
361+ users_to_remove .append (potential ["discord_id" ])
346362 description = "Removing users with ambiguous matches"
347363
348364 if not users_to_remove :
349365 await interaction .followup .send ("No users found to remove for the selected category." , ephemeral = True )
350366 return
351367
368+ # Log what we're about to do
369+ logger .info (f"{ description } . Found { len (users_to_remove )} users to remove." )
370+
352371 # Remove users from database and update roles
353372 removed_count = 0
354373 guild = interaction .guild
@@ -361,7 +380,7 @@ async def remove_unmatched_command(self, interaction: discord.Interaction, categ
361380 member = await guild .fetch_member (user_id )
362381 if member :
363382 roles_to_remove = [role for role in member .roles
364- if role .name .lower () in ["registered" ]]
383+ if role .name .lower () in ["registered" , "team member" ]]
365384 if roles_to_remove :
366385 await member .remove_roles (* roles_to_remove )
367386 removed_count += 1
0 commit comments