Skip to content

Commit 227c984

Browse files
Enhance username reminder command: improve status message updates, add periodic backup CSV updates after each batch, and refine error handling for backup file operations.
1 parent 2041e94 commit 227c984

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

bot.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,6 @@ async def send_username_reminders_command(interaction: discord.Interaction, conf
13951395
# Continue processing anyway
13961396

13971397
# Create a message directly in the channel for status updates
1398-
# that can be edited even after the interaction expires
13991398
status_channel = interaction.channel
14001399
status_message = None
14011400

@@ -1415,7 +1414,7 @@ async def send_username_reminders_command(interaction: discord.Interaction, conf
14151414

14161415
logger.info(f"Processing batch {batch_index + 1}/{total_batches} ({len(current_batch)} users)")
14171416

1418-
# Update status message every batch
1417+
# Update status message periodically
14191418
if batch_index > 0 and batch_index % 2 == 0: # Update every 2 batches to reduce API calls
14201419
progress = (batch_start / total_users) * 100
14211420
progress_embed = discord.Embed(
@@ -1435,13 +1434,11 @@ async def send_username_reminders_command(interaction: discord.Interaction, conf
14351434
# Create or update status message
14361435
try:
14371436
if status_message is None and status_channel:
1438-
# First time creating the message (only for admins)
14391437
status_message = await status_channel.send(
14401438
content=f"Status update for {interaction.user.mention}'s username reminder command:",
14411439
embed=progress_embed
14421440
)
14431441
elif status_message:
1444-
# Update existing message
14451442
await status_message.edit(embed=progress_embed)
14461443
except Exception as e:
14471444
logger.error(f"Error updating status message: {e}")
@@ -1512,7 +1509,7 @@ async def send_username_reminders_command(interaction: discord.Interaction, conf
15121509

15131510
dm_embed.add_field(
15141511
name="How to update",
1515-
value=f"Use the `/register` command with your corrected username:\n`/register {proper_format}`",
1512+
value=f"Use the `/register` command with your corrected username:\n`/register {proper_format}`\nRemember: You are supposed to edit your matcherino username on discord, not on matcherino.com!",
15161513
inline=False
15171514
)
15181515

@@ -1538,31 +1535,35 @@ async def send_username_reminders_command(interaction: discord.Interaction, conf
15381535
failed_count += 1
15391536
failed_users.append(f"{discord_username} (ID: {user_id}) - Error: {str(e)}")
15401537
entry['status'] = f'failed - {str(e)}'
1541-
1542-
# Update backup CSV with final status
1543-
with open(backup_filename, 'w', newline='', encoding='utf-8') as csvfile:
1544-
fieldnames = ['discord_id', 'discord_username', 'matcherino_username', 'suggested_format', 'status']
1545-
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
1546-
writer.writeheader()
15471538

1548-
for entry in users_needing_reminders:
1549-
user = entry['user']
1550-
participant = entry['participant']
1551-
1552-
if participant:
1553-
suggested_format = f"{participant['name']}#{participant['user_id']}"
1554-
else:
1555-
suggested_format = f"{user.get('matcherino_username', '').strip()}#userId"
1539+
# Update backup CSV after each batch to allow resuming if the process fails
1540+
try:
1541+
with open(backup_filename, 'w', newline='', encoding='utf-8') as csvfile:
1542+
fieldnames = ['discord_id', 'discord_username', 'matcherino_username', 'suggested_format', 'status']
1543+
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
1544+
writer.writeheader()
15561545

1557-
writer.writerow({
1558-
'discord_id': user.get('user_id', ''),
1559-
'discord_username': user.get('username', ''),
1560-
'matcherino_username': user.get('matcherino_username', '').strip(),
1561-
'suggested_format': suggested_format,
1562-
'status': entry.get('status', 'unknown')
1563-
})
1546+
for entry in users_needing_reminders:
1547+
user = entry['user']
1548+
participant = entry['participant']
1549+
1550+
if participant:
1551+
suggested_format = f"{participant['name']}#{participant['user_id']}"
1552+
else:
1553+
suggested_format = f"{user.get('matcherino_username', '').strip()}#userId"
1554+
1555+
writer.writerow({
1556+
'discord_id': user.get('user_id', ''),
1557+
'discord_username': user.get('username', ''),
1558+
'matcherino_username': user.get('matcherino_username', '').strip(),
1559+
'suggested_format': suggested_format,
1560+
'status': entry.get('status', 'unknown')
1561+
})
15641562

1565-
logger.info(f"Updated backup file with final status: {backup_filename}")
1563+
logger.info(f"Updated backup file after batch {batch_index+1}")
1564+
except Exception as e:
1565+
logger.error(f"Error updating backup file: {e}")
1566+
# Continue processing even if backup file update fails
15661567

15671568
# Create final result embed
15681569
result_embed = discord.Embed(

0 commit comments

Comments
 (0)