Skip to content

Commit b76dd9b

Browse files
Refactor remove_unmatched command to simplify logic; ensure only users with no matches are removed
1 parent a0dde43 commit b76dd9b

File tree

2 files changed

+18
-56
lines changed

2 files changed

+18
-56
lines changed

cogs/matcherino_cog.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,17 @@ async def match_participants_with_db_users(self, participants, db_users):
164164
processed_participants.add(participant_name.lower())
165165
continue
166166

167-
# If no exact match, try name-only match with O(1) lookup
167+
# If no exact match, try name-only match
168168
name_only = participant_name.split('#')[0].strip().lower()
169169
potential_matches = name_match_dict.get(name_only, [])
170170

171171
# Filter out already matched users
172172
potential_matches = [user for user in potential_matches if user['user_id'] not in matched_discord_ids]
173173

174-
# Process potential matches
175174
if len(potential_matches) == 1:
176175
# Single name match found
177176
match = potential_matches[0]
177+
logger.info(f"Found name-only match: '{match.get('matcherino_username', '')}' base name matches with '{participant_name}'")
178178
name_only_matches.append({
179179
'participant': participant_name,
180180
'participant_tag': game_username,
@@ -221,8 +221,8 @@ async def match_participants_with_db_users(self, participants, db_users):
221221
]
222222

223223
return (
224-
exact_matches,
225-
name_only_matches,
224+
exact_matches,
225+
name_only_matches,
226226
ambiguous_matches,
227227
unmatched_participants,
228228
unmatched_db_users

cogs/teams_cog.py

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,10 @@ async def debug_team_match(self, interaction: discord.Interaction):
278278
logger.error(f"Error in debug-team-match command: {e}", exc_info=True)
279279
await interaction.followup.send(f"An error occurred: {str(e)}", ephemeral=True)
280280

281-
@app_commands.command(name="remove-unmatched", description="Remove unmatched users from database and Discord roles")
282-
@app_commands.choices(category=[
283-
app_commands.Choice(name="Unmatched database users", value="unmatched_db"),
284-
app_commands.Choice(name="Unmatched participants", value="unmatched_participants"),
285-
app_commands.Choice(name="Ambiguous matches", value="ambiguous"),
286-
app_commands.Choice(name="All unmatched", value="all")
287-
])
281+
@app_commands.command(name="remove-unmatched", description="Remove users who have no matches at all (neither exact nor loose name matches)")
288282
@app_commands.default_permissions(administrator=True)
289-
async def remove_unmatched_command(self, interaction: discord.Interaction, category: str):
290-
"""Remove unmatched users based on specified category"""
283+
async def remove_unmatched_command(self, interaction: discord.Interaction):
284+
"""Remove users who have no matches at all in Matcherino"""
291285
if not self.bot.TOURNAMENT_ID:
292286
await interaction.response.send_message("MATCHERINO_TOURNAMENT_ID is not set.", ephemeral=True)
293287
return
@@ -315,60 +309,28 @@ async def remove_unmatched_command(self, interaction: discord.Interaction, categ
315309
await interaction.followup.send("MatcherinoCog not found.", ephemeral=True)
316310
return
317311

318-
# Match participants with database users
312+
# Use the exact same matching logic as match-free-agents
319313
(exact_matches, name_only_matches, ambiguous_matches,
320314
unmatched_participants, unmatched_db_users) = await matcherino_cog.match_participants_with_db_users(
321315
participants, db_users
322316
)
323317

324-
# Track all matched Discord IDs to avoid removing matched users
325-
matched_discord_ids = set()
326-
327-
# Add exact matches
328-
for match in exact_matches:
329-
matched_discord_ids.add(match["discord_id"])
330-
331-
# Add name-only matches - these are also valid matches
332-
for match in name_only_matches:
333-
matched_discord_ids.add(match["discord_id"])
318+
# Track all matched Discord IDs (both exact and name-only matches)
319+
matched_discord_ids = {match["discord_id"] for match in exact_matches}
320+
matched_discord_ids.update(match["discord_id"] for match in name_only_matches)
334321

322+
# Users to remove are ONLY those who have no matches at all
335323
users_to_remove = []
336-
description = ""
337-
338-
# Build list of users to remove based on category
339-
if category == "all":
340-
# Only include users that aren't in exact or name-only matches
341-
for user in db_users:
342-
if user["user_id"] not in matched_discord_ids:
343-
users_to_remove.append(user["user_id"])
344-
description = "Removing all unmatched users"
345-
elif category == "unmatched_db":
346-
# Only include users that aren't in exact or name-only matches
347-
for user in unmatched_db_users:
348-
if user["discord_id"] not in matched_discord_ids:
349-
users_to_remove.append(user["discord_id"])
350-
description = "Removing unmatched database users"
351-
elif category == "unmatched_participants":
352-
# Get the Discord IDs of users who are registered with usernames that match unmatched participants
353-
participant_names = {p["name"].lower().split('#')[0] for p in unmatched_participants}
354-
for user in db_users:
355-
user_matcherino = user.get("matcherino_username", "").lower().split('#')[0]
356-
if user_matcherino in participant_names and user["user_id"] not in matched_discord_ids:
357-
users_to_remove.append(user["user_id"])
358-
description = "Removing unmatched Matcherino participants"
359-
elif category == "ambiguous":
360-
# Only include ambiguous matches if they're not exact or name-only matches
361-
for match in ambiguous_matches:
362-
for potential in match["potential_matches"]:
363-
if potential["discord_id"] not in matched_discord_ids:
364-
users_to_remove.append(potential["discord_id"])
365-
description = "Removing users with ambiguous matches"
324+
for user in db_users:
325+
if user["user_id"] not in matched_discord_ids:
326+
users_to_remove.append(user["user_id"])
366327

367328
if not users_to_remove:
368-
await interaction.followup.send("No users found to remove for the selected category.", ephemeral=True)
329+
await interaction.followup.send("No completely unmatched users found to remove.", ephemeral=True)
369330
return
370331

371332
# Log what we're about to do
333+
description = "Removing users who have no matches at all"
372334
logger.info(f"{description}. Found {len(users_to_remove)} users to remove.")
373335

374336
# Remove users from database and update roles
@@ -393,7 +355,7 @@ async def remove_unmatched_command(self, interaction: discord.Interaction, categ
393355
logger.error(f"Error removing roles from user {user_id}: {e}")
394356

395357
await interaction.followup.send(
396-
f"{description}\nSuccessfully removed {removed_count} out of {len(users_to_remove)} users.",
358+
f"{description}\nSuccessfully removed {removed_count} out of {len(users_to_remove)} users who had no matches at all.",
397359
ephemeral=True
398360
)
399361

0 commit comments

Comments
 (0)