From f75d42d30fee7283774e95b1fcccfdad9d739d98 Mon Sep 17 00:00:00 2001 From: VinceC <33974776+VinceC3@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:18:27 -0500 Subject: [PATCH] update bot with api --- index.js | 104 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 50 deletions(-) diff --git a/index.js b/index.js index c3704bd..e53e589 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,11 @@ -const { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js'); +const { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js'); const fs = require('fs'); +const axios = require('axios'); require('dotenv').config(); - - // Add this at the top of your file console.log('Starting bot...'); - const client = new Client({ intents: [ GatewayIntentBits.Guilds, @@ -19,7 +17,6 @@ const client = new Client({ // Add this just before the login call console.log('Attempting to log in...'); - client.once('ready', () => { console.log(`Logged in as ${client.user.tag}!`); }); @@ -34,66 +31,73 @@ client.login(process.env.BOT_TOKEN).then(() => { const rawData = fs.readFileSync('users.json'); const rawUsers = JSON.parse(rawData); - // Remove user_id and player_id from each user object const users = rawUsers.map(({ user_id, player_id, ...rest }) => rest); // Now you can use the users array in your bot logic console.log(`Loaded ${users.length} users`); -// Example: Find a user by username -function findUserByUsername(username) { - return users.find(user => user.username.toLowerCase() === username.toLowerCase()); +async function findUserByUsername(username) { + try { + const response = await axios.get(`https://www.vrbattles.gg/api/get_player_data_by_username/${username}`); + return response.data; + } catch (error) { + console.error('Error fetching user data:', error); + return null; } +} +client.on('interactionCreate', async interaction => { + if (!interaction.isCommand()) return; - client.on('interactionCreate', async interaction => { - if (!interaction.isCommand()) return; + const { commandName } = interaction; - const { commandName } = interaction; + if (commandName === 'ping') { + await interaction.reply('Pong!'); + } else if (commandName === 'finduser') { + const username = interaction.options.getString('username'); + + await interaction.deferReply(); - if (commandName === 'ping') { - await interaction.reply('Pong!'); - } else if (commandName === 'finduser') { - const username = interaction.options.getString('username'); - const user = findUserByUsername(username); - if (user) { - const embed = new EmbedBuilder() - .setTitle(`User: ${user.username}`) - .setDescription(`Bio: ${user.profile.bio || 'No bio available'}`) - .addFields( - { name: 'Country', value: user.profile.country, inline: true }, - { name: 'Rank', value: user.profile.rank || 'Unranked', inline: true }, - { name: 'Level', value: user.profile.level, inline: true }, - { name: 'Prestige', value: user.profile.prestige?.toString() || 'None', inline: true }, - { name: 'Current Level Badge', value: user.profile.current_level_badge || 'None', inline: true }, - { name: 'XP', value: user.profile.xp.toString(), inline: true } - ) - .setColor('#0099ff'); + const userData = await findUserByUsername(username); + if (userData && userData.success) { + const playerData = JSON.parse(userData.player_data); + const user = playerData.profile; + const embed = new EmbedBuilder() + .setTitle(`User: ${playerData.username}`) + .setDescription(`Bio: ${user.bio || 'No bio available'}`) + .addFields( + { name: 'Country', value: user.country || 'Unknown', inline: true }, + { name: 'Rank', value: user.rank || 'Unranked', inline: true }, + { name: 'Level', value: user.level.toString(), inline: true }, + { name: 'Prestige', value: user.prestige?.toString() || 'None', inline: true }, + { name: 'Current Level Badge', value: user.current_level_badge || 'None', inline: true }, + { name: 'XP', value: user.xp.toString(), inline: true } + ) + .setColor('#0099ff'); - if (user.profile.avatar) { - embed.setThumbnail(user.profile.avatar); - } + if (user.avatar) { + embed.setThumbnail(`https://www.vrbattles.gg/avatars/${user.avatar}`); + } - const row = new ActionRowBuilder() - .addComponents( - new ButtonBuilder() - .setLabel('🔵 View Profile') - .setStyle(ButtonStyle.Link) - .setURL('https://www.vrbattles.gg'), // Replace with your website URL - new ButtonBuilder() - .setLabel('🟡 Join Discord') - .setStyle(ButtonStyle.Link) - .setURL('https://discord.gg/your-invite-code') // Replace with your Discord invite URL - ); + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setLabel('🔵 View Profile') + .setStyle(ButtonStyle.Link) + .setURL(`https://www.vrbattles.gg/users/${playerData.username}`), + new ButtonBuilder() + .setLabel('🟡 Join Discord') + .setStyle(ButtonStyle.Link) + .setURL('https://discord.gg/your-invite-code') // Replace with your Discord invite URL + ); - await interaction.reply({ embeds: [embed], components: [row] }); - - } else { - await interaction.reply('User not found'); - } + await interaction.editReply({ embeds: [embed], components: [row] }); + } else { + await interaction.editReply('User not found or error occurred while fetching data.'); } + } }); // Use the bot token here -client.login(process.env.BOT_TOKEN); +client.login(process.env.BOT_TOKEN); \ No newline at end of file