From 2fbba386201f52c31cd7822e155ac4d5d6573f38 Mon Sep 17 00:00:00 2001 From: Ayden Jahola Date: Wed, 2 Oct 2024 02:47:30 +0100 Subject: [PATCH] add val stats command --- commands/stats/valorant.js | 99 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 commands/stats/valorant.js diff --git a/commands/stats/valorant.js b/commands/stats/valorant.js new file mode 100644 index 0000000..0bac61b --- /dev/null +++ b/commands/stats/valorant.js @@ -0,0 +1,99 @@ +const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); +const axios = require("axios"); + +module.exports = { + data: new SlashCommandBuilder() + .setName("valstats") + .setDescription("Fetches Valorant player stats.") + .addStringOption((option) => + option + .setName("username") + .setDescription( + "The Valorant username to fetch stats for (e.g., Shitter#1234)" + ) + .setRequired(true) + ) + .addStringOption((option) => + option + .setName("stats_type") + .setDescription("Type of stats to fetch") + .addChoices( + { name: "Current Act Stats", value: "current" }, + { name: "All Acts Stats", value: "all" } + ) + .setRequired(true) + ), + async execute(interaction) { + const username = interaction.options.getString("username"); + const statsType = interaction.options.getString("stats_type"); + + // Convert the username by replacing "#" with "%23" + const formattedUsername = username.replace("#", "%23"); + const apiKey = process.env.VALORANT_API_KEY; + + const url = `https://val-api.aydenjahola.com/player/${formattedUsername}/${statsType}`; + + try { + await interaction.deferReply(); + + const response = await axios.get(url, { + headers: { + "X-API-Key": apiKey, + }, + }); + + const data = response.data; + + const statsEmbed = new EmbedBuilder() + .setColor("#0099ff") + .setTitle(`${data.username}'s Valorant Stats`) + .addFields( + { name: "💻 Platform", value: data.platform }, + { + name: "🏆 Current Rank", + value: data.current_rank, + }, + { + name: "🔝 Peak Rank", + value: data.peak_rank, + }, + { name: "⚔️ Kills", value: `${data.kills}` }, + { name: "🏅 Wins", value: `${data.wins}` }, + { + name: "🎮 Matches Played", + value: `${data.matches_played}`, + }, + { + name: "📈 K/D Ratio", + value: `${data.kd_ratio}`, + }, + { + name: "🎯 Headshot Percentage", + value: `${data.headshot_percentage}%`, + }, + { + name: "📊 Win Percentage", + value: `${data.win_percentage}%`, + } + ) + .setTimestamp() + .setFooter({ + text: "Valorant Stats Bot made by Ayden", + iconURL: interaction.guild.iconURL(), + }); + + return interaction.editReply({ embeds: [statsEmbed] }); + } catch (error) { + console.error("Error fetching player stats:", error); + if (error.response) { + return interaction.editReply( + `Error: ${error.response.data.message || error.response.statusText}` + ); + } else { + return interaction.editReply( + "An error occurred while fetching player stats." + ); + } + } + }, +};