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;
|
if (!interaction.isCommand()) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.log(`Interaction received: ${interaction.id} by ${interaction.user.tag}`);
|
||||||
|
|
||||||
const { commandName } = interaction;
|
const { commandName } = interaction;
|
||||||
|
|
||||||
if (commandName === 'ping') {
|
if (commandName === 'ping') {
|
||||||
await interaction.reply('Pong!');
|
await interaction.reply('Pong!');
|
||||||
} else if (commandName === 'finduser') {
|
} else if (commandName === 'finduser') {
|
||||||
await interaction.deferReply(); // Moved up
|
await interaction.deferReply(); // Defer immediately
|
||||||
|
|
||||||
const username = interaction.options.getString('username');
|
const username = interaction.options.getString('username');
|
||||||
const gameFilter = interaction.options.getString('game');
|
const gameFilter = interaction.options.getString('game');
|
||||||
@@ -94,14 +96,62 @@ client.on('interactionCreate', async (interaction) => {
|
|||||||
// Fetch data from the API using the provided username
|
// Fetch data from the API using the provided username
|
||||||
const userData = await findUserByUsername(username);
|
const userData = await findUserByUsername(username);
|
||||||
|
|
||||||
if (userData && userData.success) {
|
if (!userData) {
|
||||||
// ... [Rest of your existing code for processing and sending the embed]
|
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] });
|
await interaction.editReply({ embeds: [embed], components: [row] });
|
||||||
} else {
|
} else {
|
||||||
await interaction.editReply(
|
await interaction.editReply('No data available to display.');
|
||||||
'User not found or an error occurred while fetching data.'
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user