From c560e3d6a1ee6a718586b1af6276daeed73b129e Mon Sep 17 00:00:00 2001 From: VinceC <33974776+VinceC3@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:19:33 -0500 Subject: [PATCH] Update index.js --- index.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 5117dc6..1f8daaa 100644 --- a/index.js +++ b/index.js @@ -81,12 +81,14 @@ client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; try { + console.log(`Interaction received: ${interaction.id} by ${interaction.user.tag}`); + const { commandName } = interaction; if (commandName === 'ping') { await interaction.reply('Pong!'); } else if (commandName === 'finduser') { - await interaction.deferReply(); // Moved up + await interaction.deferReply(); // Defer immediately const username = interaction.options.getString('username'); const gameFilter = interaction.options.getString('game'); @@ -94,14 +96,62 @@ client.on('interactionCreate', async (interaction) => { // Fetch data from the API using the provided username const userData = await findUserByUsername(username); - if (userData && userData.success) { - // ... [Rest of your existing code for processing and sending the embed] + if (!userData) { + await interaction.editReply('An error occurred while fetching data.'); + return; + } + let playerData; + try { + if (typeof userData.player_data === 'string') { + playerData = JSON.parse(userData.player_data); + } else { + playerData = userData.player_data; + } + } catch (parseError) { + console.error('Error parsing player data:', parseError); + await interaction.editReply('Error parsing player data.'); + return; + } + + if (!playerData || !playerData.profile) { + await interaction.editReply( + 'User found but profile data is unavailable. They may need to log in to VRBattles to update their profile.' + ); + return; + } + + const user = playerData.profile || {}; + const badgeImageUrl = getBadgeImageUrl(user.current_level_badge); + + // Declare 'embed' at the appropriate scope + let embed; + + // Create the embed builder + embed = new EmbedBuilder() + .setTitle(`User: ${playerData.username || 'Unknown'}`) + .setDescription(`Bio: ${user.bio || 'No bio available'}`) + .setColor('#0099ff'); + + // Rest of your code... + + // Add action buttons + const row = new ActionRowBuilder().addComponents( + new ButtonBuilder() + .setLabel('🔵 View Profile') + .setStyle(ButtonStyle.Link) + .setURL(`https://www.vrbattles.gg/profile/${playerData.username}`), + new ButtonBuilder() + .setLabel('🟡 Join Discord') + .setStyle(ButtonStyle.Link) + .setURL('https://discord.gg/j3DKVATPGQ') // Replace with your Discord invite URL + ); + + // Ensure 'embed' is defined before using it + if (embed) { await interaction.editReply({ embeds: [embed], components: [row] }); } else { - await interaction.editReply( - 'User not found or an error occurred while fetching data.' - ); + await interaction.editReply('No data available to display.'); } } } catch (error) {