Update index.js
This commit is contained in:
62
index.js
62
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) {
|
||||
|
||||
Reference in New Issue
Block a user