120 lines
3.2 KiB
JavaScript
120 lines
3.2 KiB
JavaScript
// index.js
|
|
|
|
const {
|
|
Client,
|
|
GatewayIntentBits,
|
|
EmbedBuilder,
|
|
ActionRowBuilder,
|
|
ButtonBuilder,
|
|
ButtonStyle,
|
|
} = require('discord.js');
|
|
const axios = require('axios');
|
|
require('dotenv').config();
|
|
|
|
console.log('Starting bot...');
|
|
|
|
const client = new Client({
|
|
intents: [
|
|
GatewayIntentBits.Guilds,
|
|
GatewayIntentBits.GuildMessages,
|
|
GatewayIntentBits.MessageContent,
|
|
],
|
|
});
|
|
|
|
console.log('Attempting to log in...');
|
|
|
|
client.once('ready', () => {
|
|
console.log(`Logged in as ${client.user.tag}!`);
|
|
});
|
|
|
|
client.login(process.env.BOT_TOKEN).then(() => {
|
|
console.log('Login successful');
|
|
}).catch((error) => {
|
|
console.error('Login failed:', error);
|
|
});
|
|
|
|
// Handle client errors
|
|
client.on('error', (error) => {
|
|
console.error('The client encountered an error:', error);
|
|
});
|
|
|
|
// Function to fetch user data from the API
|
|
async function findUserByUsername(username) {
|
|
try {
|
|
const response = await axios.get(
|
|
`https://www.vrbattles.gg/api/get_player_data_by_username/${encodeURIComponent(
|
|
username
|
|
)}`
|
|
);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('Error fetching user data:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Function to get badge image URL
|
|
function getBadgeImageUrl(badgeName) {
|
|
if (!badgeName) {
|
|
return 'https://www.vrbattles.gg/assets/images/badges/xp_badges/A/BADGE_A1_72p.png';
|
|
}
|
|
|
|
badgeName = badgeName.toUpperCase().replace(/ /g, '_');
|
|
let badgeUrl = 'https://www.vrbattles.gg/assets/images/badges/xp_badges/';
|
|
|
|
if (badgeName.startsWith('A')) {
|
|
badgeUrl += 'A/';
|
|
} else if (badgeName.startsWith('B')) {
|
|
badgeUrl += 'B/';
|
|
} else if (badgeName.startsWith('C')) {
|
|
badgeUrl += 'C/';
|
|
} else {
|
|
badgeUrl += 'A/';
|
|
}
|
|
|
|
badgeUrl += `BADGE_${badgeName}_72p.png`;
|
|
return badgeUrl;
|
|
}
|
|
|
|
// Interaction handler
|
|
client.on('interactionCreate', async (interaction) => {
|
|
if (!interaction.isCommand()) return;
|
|
|
|
try {
|
|
const { commandName } = interaction;
|
|
|
|
if (commandName === 'ping') {
|
|
await interaction.reply('Pong!');
|
|
} else if (commandName === 'finduser') {
|
|
await interaction.deferReply(); // Moved up
|
|
|
|
const username = interaction.options.getString('username');
|
|
const gameFilter = interaction.options.getString('game');
|
|
|
|
// 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]
|
|
|
|
await interaction.editReply({ embeds: [embed], components: [row] });
|
|
} else {
|
|
await interaction.editReply(
|
|
'User not found or an error occurred while fetching data.'
|
|
);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error handling interaction:', error);
|
|
|
|
try {
|
|
if (interaction.deferred || interaction.replied) {
|
|
await interaction.followUp({ content: 'An error occurred while processing your request.', ephemeral: true });
|
|
} else {
|
|
await interaction.reply({ content: 'An error occurred while processing your request.', ephemeral: true });
|
|
}
|
|
} catch (replyError) {
|
|
console.error('Error sending error message:', replyError);
|
|
}
|
|
}
|
|
}); |