|
| 1 | +import { |
| 2 | + ChatInputCommandInteraction, |
| 3 | + MessageFlags, |
| 4 | + PermissionFlagsBits, |
| 5 | + SlashCommandBuilder, |
| 6 | +} from 'discord.js' |
| 7 | +import { pool } from '../../db' |
| 8 | + |
| 9 | +export default { |
| 10 | + data: new SlashCommandBuilder() |
| 11 | + .setName('backfill-display-names') |
| 12 | + .setDescription('[ADMIN] Backfills display names for all users in the database') |
| 13 | + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), |
| 14 | + async execute(interaction: ChatInputCommandInteraction) { |
| 15 | + try { |
| 16 | + await interaction.deferReply({ flags: MessageFlags.Ephemeral }) |
| 17 | + |
| 18 | + // Get all users from the database without display names |
| 19 | + const result = await pool.query('SELECT user_id FROM users WHERE display_name IS NULL') |
| 20 | + const users = result.rows |
| 21 | + |
| 22 | + if (users.length === 0) { |
| 23 | + await interaction.editReply({ |
| 24 | + content: 'All users already have display names! Nothing to backfill.', |
| 25 | + }) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + await interaction.editReply({ |
| 30 | + content: `Found ${users.length} users without display names. Starting backfill...\nThis may take a while.`, |
| 31 | + }) |
| 32 | + |
| 33 | + let successCount = 0 |
| 34 | + let errorCount = 0 |
| 35 | + const errors: string[] = [] |
| 36 | + |
| 37 | + // Process each user |
| 38 | + for (let i = 0; i < users.length; i++) { |
| 39 | + const user = users[i] |
| 40 | + try { |
| 41 | + // Try to fetch the user from Discord |
| 42 | + const discordUser = await interaction.client.users.fetch(user.user_id) |
| 43 | + |
| 44 | + // Get display name from the current guild if possible |
| 45 | + let displayName = discordUser.username |
| 46 | + |
| 47 | + if (interaction.guild) { |
| 48 | + try { |
| 49 | + const member = await interaction.guild.members.fetch(user.user_id) |
| 50 | + if (member) { |
| 51 | + displayName = member.displayName |
| 52 | + } |
| 53 | + } catch { |
| 54 | + // User might not be in this guild, use username |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Update the database |
| 59 | + await pool.query( |
| 60 | + 'UPDATE users SET display_name = $1 WHERE user_id = $2', |
| 61 | + [displayName, user.user_id] |
| 62 | + ) |
| 63 | + |
| 64 | + successCount++ |
| 65 | + |
| 66 | + // Send progress update every 10 users |
| 67 | + if ((i + 1) % 10 === 0) { |
| 68 | + await interaction.editReply({ |
| 69 | + content: `Progress: ${i + 1}/${users.length} users processed...\nSuccessful: ${successCount}\nErrors: ${errorCount}`, |
| 70 | + }) |
| 71 | + } |
| 72 | + } catch (err: any) { |
| 73 | + errorCount++ |
| 74 | + errors.push(`${user.user_id}: ${err.message}`) |
| 75 | + console.error(`Error updating ${user.user_id}:`, err) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Final report |
| 80 | + let finalMessage = `✅ Backfill complete!\n\n**Results:**\n- Successfully updated: ${successCount}\n- Errors: ${errorCount}\n- Total processed: ${users.length}` |
| 81 | + |
| 82 | + if (errors.length > 0 && errors.length <= 5) { |
| 83 | + finalMessage += `\n\n**Errors:**\n${errors.map(e => `- ${e}`).join('\n')}` |
| 84 | + } else if (errors.length > 5) { |
| 85 | + finalMessage += `\n\n**Errors:** Too many to display (${errors.length} total). Check console logs.` |
| 86 | + } |
| 87 | + |
| 88 | + await interaction.editReply({ |
| 89 | + content: finalMessage, |
| 90 | + }) |
| 91 | + } catch (err: any) { |
| 92 | + console.error('Error during backfill:', err) |
| 93 | + const errorMsg = err.detail || err.message || 'Unknown error' |
| 94 | + if (interaction.deferred || interaction.replied) { |
| 95 | + await interaction.editReply({ |
| 96 | + content: `❌ Failed to backfill display names. Reason: ${errorMsg}`, |
| 97 | + }) |
| 98 | + } else { |
| 99 | + await interaction.reply({ |
| 100 | + content: `❌ Failed to backfill display names. Reason: ${errorMsg}`, |
| 101 | + flags: MessageFlags.Ephemeral, |
| 102 | + }) |
| 103 | + } |
| 104 | + } |
| 105 | + }, |
| 106 | +} |
0 commit comments