update bot with api

This commit is contained in:
VinceC
2024-09-17 14:18:27 -05:00
parent 5b22425c0e
commit f75d42d30f

View File

@@ -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 fs = require('fs');
const axios = require('axios');
require('dotenv').config(); require('dotenv').config();
// Add this at the top of your file // Add this at the top of your file
console.log('Starting bot...'); console.log('Starting bot...');
const client = new Client({ const client = new Client({
intents: [ intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.Guilds,
@@ -19,7 +17,6 @@ const client = new Client({
// Add this just before the login call // Add this just before the login call
console.log('Attempting to log in...'); console.log('Attempting to log in...');
client.once('ready', () => { client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`); console.log(`Logged in as ${client.user.tag}!`);
}); });
@@ -34,20 +31,23 @@ client.login(process.env.BOT_TOKEN).then(() => {
const rawData = fs.readFileSync('users.json'); const rawData = fs.readFileSync('users.json');
const rawUsers = JSON.parse(rawData); const rawUsers = JSON.parse(rawData);
// Remove user_id and player_id from each user object // Remove user_id and player_id from each user object
const users = rawUsers.map(({ user_id, player_id, ...rest }) => rest); const users = rawUsers.map(({ user_id, player_id, ...rest }) => rest);
// Now you can use the users array in your bot logic // Now you can use the users array in your bot logic
console.log(`Loaded ${users.length} users`); console.log(`Loaded ${users.length} users`);
// Example: Find a user by username async function findUserByUsername(username) {
function findUserByUsername(username) { try {
return users.find(user => user.username.toLowerCase() === username.toLowerCase()); 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 => {
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return; if (!interaction.isCommand()) return;
const { commandName } = interaction; const { commandName } = interaction;
@@ -56,23 +56,28 @@ function findUserByUsername(username) {
await interaction.reply('Pong!'); await interaction.reply('Pong!');
} else if (commandName === 'finduser') { } else if (commandName === 'finduser') {
const username = interaction.options.getString('username'); const username = interaction.options.getString('username');
const user = findUserByUsername(username);
if (user) { await interaction.deferReply();
const userData = await findUserByUsername(username);
if (userData && userData.success) {
const playerData = JSON.parse(userData.player_data);
const user = playerData.profile;
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle(`User: ${user.username}`) .setTitle(`User: ${playerData.username}`)
.setDescription(`Bio: ${user.profile.bio || 'No bio available'}`) .setDescription(`Bio: ${user.bio || 'No bio available'}`)
.addFields( .addFields(
{ name: 'Country', value: user.profile.country, inline: true }, { name: 'Country', value: user.country || 'Unknown', inline: true },
{ name: 'Rank', value: user.profile.rank || 'Unranked', inline: true }, { name: 'Rank', value: user.rank || 'Unranked', inline: true },
{ name: 'Level', value: user.profile.level, inline: true }, { name: 'Level', value: user.level.toString(), inline: true },
{ name: 'Prestige', value: user.profile.prestige?.toString() || 'None', inline: true }, { name: 'Prestige', value: user.prestige?.toString() || 'None', inline: true },
{ name: 'Current Level Badge', value: user.profile.current_level_badge || 'None', inline: true }, { name: 'Current Level Badge', value: user.current_level_badge || 'None', inline: true },
{ name: 'XP', value: user.profile.xp.toString(), inline: true } { name: 'XP', value: user.xp.toString(), inline: true }
) )
.setColor('#0099ff'); .setColor('#0099ff');
if (user.profile.avatar) { if (user.avatar) {
embed.setThumbnail(user.profile.avatar); embed.setThumbnail(`https://www.vrbattles.gg/avatars/${user.avatar}`);
} }
const row = new ActionRowBuilder() const row = new ActionRowBuilder()
@@ -80,17 +85,16 @@ function findUserByUsername(username) {
new ButtonBuilder() new ButtonBuilder()
.setLabel('🔵 View Profile') .setLabel('🔵 View Profile')
.setStyle(ButtonStyle.Link) .setStyle(ButtonStyle.Link)
.setURL('https://www.vrbattles.gg'), // Replace with your website URL .setURL(`https://www.vrbattles.gg/users/${playerData.username}`),
new ButtonBuilder() new ButtonBuilder()
.setLabel('🟡 Join Discord') .setLabel('🟡 Join Discord')
.setStyle(ButtonStyle.Link) .setStyle(ButtonStyle.Link)
.setURL('https://discord.gg/your-invite-code') // Replace with your Discord invite URL .setURL('https://discord.gg/your-invite-code') // Replace with your Discord invite URL
); );
await interaction.reply({ embeds: [embed], components: [row] }); await interaction.editReply({ embeds: [embed], components: [row] });
} else { } else {
await interaction.reply('User not found'); await interaction.editReply('User not found or error occurred while fetching data.');
} }
} }
}); });