update bot with api

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

104
index.js
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,66 +31,73 @@ 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 => {
if (!interaction.isCommand()) return;
client.on('interactionCreate', async interaction => { const { commandName } = interaction;
if (!interaction.isCommand()) return;
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') { const userData = await findUserByUsername(username);
await interaction.reply('Pong!'); if (userData && userData.success) {
} else if (commandName === 'finduser') { const playerData = JSON.parse(userData.player_data);
const username = interaction.options.getString('username'); const user = playerData.profile;
const user = findUserByUsername(username); const embed = new EmbedBuilder()
if (user) { .setTitle(`User: ${playerData.username}`)
const embed = new EmbedBuilder() .setDescription(`Bio: ${user.bio || 'No bio available'}`)
.setTitle(`User: ${user.username}`) .addFields(
.setDescription(`Bio: ${user.profile.bio || 'No bio available'}`) { name: 'Country', value: user.country || 'Unknown', inline: true },
.addFields( { name: 'Rank', value: user.rank || 'Unranked', inline: true },
{ name: 'Country', value: user.profile.country, inline: true }, { name: 'Level', value: user.level.toString(), inline: true },
{ name: 'Rank', value: user.profile.rank || 'Unranked', inline: true }, { name: 'Prestige', value: user.prestige?.toString() || 'None', inline: true },
{ name: 'Level', value: user.profile.level, inline: true }, { name: 'Current Level Badge', value: user.current_level_badge || 'None', inline: true },
{ name: 'Prestige', value: user.profile.prestige?.toString() || 'None', inline: true }, { name: 'XP', value: user.xp.toString(), 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');
)
.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()
.addComponents( .addComponents(
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.editReply('User not found or error occurred while fetching data.');
await interaction.reply('User not found');
}
} }
}
}); });
// Use the bot token here // Use the bot token here
client.login(process.env.BOT_TOKEN); client.login(process.env.BOT_TOKEN);