|
| 1 | +"use strict"; |
| 2 | +// apps/bot/commands/badges.ts |
| 3 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 4 | +exports.execute = exports.command = void 0; |
| 5 | +const discord_js_1 = require("discord.js"); |
| 6 | +const supabase_bot_1 = require("../utils/supabase.bot"); |
| 7 | +//Slash command |
| 8 | +const slashCommand = new discord_js_1.SlashCommandBuilder() |
| 9 | + .setName("badges") |
| 10 | + .setDescription("View your badges or award a badge (admin only).") |
| 11 | + .addUserOption((opt) => opt |
| 12 | + .setName("user") |
| 13 | + .setDescription("Target user to view or award badges to.") |
| 14 | + .setRequired(false)) |
| 15 | + .addStringOption((opt) => opt |
| 16 | + .setName("badge") |
| 17 | + .setDescription("Badge ID to award (admin only)") |
| 18 | + .setRequired(false)); |
| 19 | +// Export the command |
| 20 | +exports.command = { |
| 21 | + name: "badges", |
| 22 | + data: slashCommand, |
| 23 | +}; |
| 24 | +// Execute function |
| 25 | +const execute = async (interaction) => { |
| 26 | + try { |
| 27 | + await interaction.deferReply(); |
| 28 | + const userOption = interaction.options.getUser("user"); |
| 29 | + const badgeId = interaction.options.getString("badge"); |
| 30 | + const targetUser = userOption || interaction.user; |
| 31 | + // Ensure users_discord record exists for target user |
| 32 | + const { error: userError } = await supabase_bot_1.supabaseBot |
| 33 | + .from("users_discord") |
| 34 | + .upsert({ |
| 35 | + id: targetUser.id, |
| 36 | + username: targetUser.username, |
| 37 | + avatar_url: targetUser.displayAvatarURL(), |
| 38 | + discriminator: targetUser.discriminator || "0", |
| 39 | + }, { onConflict: "id" }); |
| 40 | + if (userError) { |
| 41 | + console.error("❌ Error ensuring user record:", userError.message); |
| 42 | + } |
| 43 | + // ===== ADMIN AWARD FLOW ===== |
| 44 | + if (badgeId) { |
| 45 | + // Permission check |
| 46 | + if (!interaction.memberPermissions?.has(discord_js_1.PermissionFlagsBits.ManageGuild)) { |
| 47 | + await interaction.editReply({ |
| 48 | + content: "❌ You do not have permission to award badges. (Requires **Manage Server** permission.)", |
| 49 | + }); |
| 50 | + return; |
| 51 | + } |
| 52 | + // Validate badgeId |
| 53 | + const badgeIdNum = parseInt(badgeId); |
| 54 | + if (isNaN(badgeIdNum)) { |
| 55 | + await interaction.editReply({ |
| 56 | + content: "❌ Invalid badge ID. Please provide a valid **number**.", |
| 57 | + }); |
| 58 | + return; |
| 59 | + } |
| 60 | + // Check if badge exists |
| 61 | + const { data: badge, error: badgeError } = await supabase_bot_1.supabaseBot |
| 62 | + .from("badges_discord") |
| 63 | + .select("id, badge_name") |
| 64 | + .eq("id", badgeIdNum) |
| 65 | + .single(); |
| 66 | + if (badgeError || !badge) { |
| 67 | + await interaction.editReply({ |
| 68 | + content: `❌ No badge found with ID **${badgeIdNum}**.`, |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + // Check if user already has this badge |
| 73 | + const { data: existingBadge } = await supabase_bot_1.supabaseBot |
| 74 | + .from("user_badges_discord") |
| 75 | + .select("id") |
| 76 | + .eq("user_id", targetUser.id) |
| 77 | + .eq("badge_id", badgeIdNum) |
| 78 | + .maybeSingle(); |
| 79 | + if (existingBadge) { |
| 80 | + await interaction.editReply({ |
| 81 | + content: `⚠️ <@${targetUser.id}> already has the badge **${badge.badge_name}**.`, |
| 82 | + }); |
| 83 | + return; |
| 84 | + } |
| 85 | + // Award the badge |
| 86 | + const { error: insertError } = await supabase_bot_1.supabaseBot |
| 87 | + .from("user_badges_discord") |
| 88 | + .insert({ |
| 89 | + user_id: targetUser.id, |
| 90 | + badge_id: badgeIdNum, |
| 91 | + }); |
| 92 | + if (insertError) { |
| 93 | + console.error("❌ Error awarding badge:", insertError.message); |
| 94 | + await interaction.editReply({ |
| 95 | + content: "⚠️ Failed to award badge. Please try again later.", |
| 96 | + }); |
| 97 | + return; |
| 98 | + } |
| 99 | + await interaction.editReply({ |
| 100 | + content: `🏅 **${badge.badge_name}** has been awarded to <@${targetUser.id}>!`, |
| 101 | + }); |
| 102 | + console.log(`✅ Badge ${badgeIdNum} awarded to ${targetUser.username} by ${interaction.user.username}`); |
| 103 | + return; |
| 104 | + } |
| 105 | + // ===== VIEW BADGES FLOW ===== |
| 106 | + const { data: userBadges, error: ubError } = await supabase_bot_1.supabaseBot |
| 107 | + .from("user_badges_discord") |
| 108 | + .select("badge_id, awarded_at") |
| 109 | + .eq("user_id", targetUser.id); |
| 110 | + if (ubError) { |
| 111 | + console.error("❌ Error fetching user badges:", ubError.message); |
| 112 | + await interaction.editReply({ |
| 113 | + content: "⚠️ Failed to fetch user badges. Please try again.", |
| 114 | + }); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (!userBadges || userBadges.length === 0) { |
| 118 | + await interaction.editReply({ |
| 119 | + content: `<@${targetUser.id}> has no badges yet. 🏆`, |
| 120 | + }); |
| 121 | + return; |
| 122 | + } |
| 123 | + // Fetch all badge details from badges_discord |
| 124 | + const { data: allBadges, error: allBadgesError } = await supabase_bot_1.supabaseBot |
| 125 | + .from("badges_discord") |
| 126 | + .select("id, badge_name, badge_icon_url, description"); |
| 127 | + if (allBadgesError || !allBadges) { |
| 128 | + console.error("❌ Error fetching badge details:", allBadgesError.message); |
| 129 | + await interaction.editReply({ |
| 130 | + content: "⚠️ Failed to load badge details. Please try again.", |
| 131 | + }); |
| 132 | + return; |
| 133 | + } |
| 134 | + // Merge badge info |
| 135 | + const badgesWithDetails = userBadges |
| 136 | + .map((ub) => { |
| 137 | + const badge = allBadges.find((b) => b.id === ub.badge_id); |
| 138 | + if (!badge) |
| 139 | + return null; |
| 140 | + return { |
| 141 | + badge_name: badge.badge_name, |
| 142 | + badge_icon_url: badge.badge_icon_url, |
| 143 | + description: badge.description, |
| 144 | + awarded_at: ub.awarded_at, |
| 145 | + }; |
| 146 | + }) |
| 147 | + .filter((badge) => badge !== null); |
| 148 | + // ===== CREATE EMBED ===== |
| 149 | + const embed = new discord_js_1.EmbedBuilder() |
| 150 | + .setTitle(`${targetUser.username}'s Badges`) |
| 151 | + .setColor("#FFD700") |
| 152 | + .setThumbnail(targetUser.displayAvatarURL()) |
| 153 | + .setFooter({ text: `Total Badges: ${badgesWithDetails.length}` }) |
| 154 | + .setTimestamp(); |
| 155 | + if (badgesWithDetails.length > 0) { |
| 156 | + embed.setDescription(badgesWithDetails |
| 157 | + .map((b) => `🏅 **${b.badge_name}**\n${b.description ?? "No description."}`) |
| 158 | + .join("\n\n")); |
| 159 | + } |
| 160 | + else { |
| 161 | + embed.setDescription("No badges found."); |
| 162 | + } |
| 163 | + await interaction.editReply({ embeds: [embed] }); |
| 164 | + } |
| 165 | + catch (error) { |
| 166 | + console.error("❌ Error executing badges command:", error); |
| 167 | + const errorMessage = { |
| 168 | + content: "❌ An unexpected error occurred while processing this command. Please try again later.", |
| 169 | + }; |
| 170 | + if (interaction.deferred) { |
| 171 | + await interaction.editReply(errorMessage); |
| 172 | + } |
| 173 | + else if (!interaction.replied) { |
| 174 | + await interaction.reply({ |
| 175 | + ...errorMessage, |
| 176 | + flags: [discord_js_1.MessageFlags.Ephemeral], |
| 177 | + }); |
| 178 | + } |
| 179 | + } |
| 180 | +}; |
| 181 | +exports.execute = execute; |
0 commit comments