Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { SlashCommandBuilder, AttachmentBuilder } = require("discord.js");
- const axios = require("axios");
- const Canvas = require("canvas");
- const path = require("path");
- const MOJANG_API_URL = "https://api.mojang.com/users/profiles/minecraft";
- module.exports = {
- data: new SlashCommandBuilder()
- .setName("stats")
- .setDescription("Fetches BedWars stats from JartexNetwork")
- .addStringOption((option) =>
- option
- .setName("username")
- .setDescription("Enter Minecraft username")
- .setRequired(true)
- )
- .addStringOption((option) =>
- option
- .setName("interval")
- .setDescription("Stat interval: weekly, monthly, yearly, overall")
- .setRequired(true)
- .addChoices(
- { name: "Weekly", value: "weekly" },
- { name: "Monthly", value: "monthly" },
- { name: "Yearly", value: "yearly" },
- { name: "Overall", value: "total" }
- )
- )
- .addStringOption((option) =>
- option
- .setName("mode")
- .setDescription("Game mode")
- .setRequired(true)
- .addChoices(
- { name: "Solos", value: "solo" },
- { name: "Doubles", value: "doubles" },
- { name: "Triples", value: "triples" },
- { name: "Quads", value: "quad" }
- )
- ),
- async execute(interaction) {
- async function getCorrectCasedUsername(ign) {
- try {
- const response = await axios.get(
- `https://stats.jartexnetwork.com/api/profile/${ign}`
- );
- return response.data.username;
- } catch (err) {
- return null;
- }
- }
- const interval = interaction.options.getString("interval");
- const mode = interaction.options.getString("mode");
- const rawIGN = interaction.options.getString("username");
- const properIGN = await getCorrectCasedUsername(rawIGN);
- if (!properIGN) {
- return interaction.reply(
- "❌ Could not find that username on Jartex. Please check the IGN."
- );
- }
- await interaction.deferReply();
- try {
- const mojangRes = await axios.get(`${MOJANG_API_URL}/${properIGN}`);
- const uuid = mojangRes.data.id;
- const skinURL = `https://crafatar.com/renders/body/${uuid}?overlay`;
- const apiURL = `https://stats.jartexnetwork.com/api/profile/${properIGN}/leaderboard?type=bedwars&interval=${interval}&mode=${mode}`;
- const response = await axios.get(apiURL);
- const stats = response.data;
- if (!stats) return;
- const getStat = (statName) => {
- const entry = stats?.[statName]?.entries;
- return entry && entry.length > 0 ? entry[0].value : "0";
- };
- const wins = getStat("Wins");
- const losses = getStat("Losses");
- const finalKills = getStat("Final kills");
- const finalDeaths = getStat("Final deaths");
- const bedsBroken = getStat("Beds destroyed");
- const bedsLost = getStat("Losses");
- const gamesPlayed = getStat("Games played");
- const winstreak = getStat("Highest winstreak reached");
- const winLossRatio = (wins / losses).toFixed(2);
- const finalKillDeathRatio = (finalKills / finalDeaths).toFixed(2);
- const bedsRatio = (bedsBroken / bedsLost).toFixed(2);
- const canvas = Canvas.createCanvas(800, 450);
- const ctx = canvas.getContext("2d");
- const backgroundPath = path.join(__dirname, "background1.png");
- const background = await Canvas.loadImage(backgroundPath);
- ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
- // Title
- ctx.font = "bold 36px cursive";
- ctx.fillStyle = "#FFC0CB";
- ctx.fillText(properIGN, 50, 60);
- // Column 1
- ctx.font = "24px cursive";
- ctx.fillStyle = "#FFFFFF";
- ctx.fillText(`Wins: ${wins}`, 50, 130);
- ctx.fillText(`Losses: ${losses}`, 50, 170);
- ctx.fillStyle = winLossRatio >= 1 ? "#00FF00" : "#FF0000";
- ctx.fillText(`W/L Ratio: ${winLossRatio}`, 50, 210);
- // Column 2
- ctx.fillStyle = "#FFFFFF";
- ctx.fillText(`Final Kills: ${finalKills}`, 290, 130);
- ctx.fillText(`Final Deaths: ${finalDeaths}`, 290, 170);
- ctx.fillStyle = finalKillDeathRatio >= 1 ? "#00FF00" : "#FF0000";
- ctx.fillText(`FKDR: ${finalKillDeathRatio}`, 290, 210);
- // Column 3
- ctx.fillStyle = "#FFFFFF";
- ctx.fillText(`Beds Broken: ${bedsBroken}`, 530, 130);
- ctx.fillText(`Beds Lost: ${bedsLost}`, 530, 170);
- ctx.fillStyle = bedsRatio >= 1 ? "#00FF00" : "#FF0000";
- ctx.fillText(`Bed Ratio: ${bedsRatio}`, 530, 210);
- // Bottom row
- ctx.fillStyle = "#FFFFFF";
- ctx.fillText(`Winstreak: ${winstreak}`, 50, 300);
- ctx.fillText(`Games Played: ${gamesPlayed}`, 290, 300);
- // Skin
- const skinImage = await Canvas.loadImage(skinURL);
- ctx.drawImage(skinImage, 610, 100, 150, 300);
- const attachment = new AttachmentBuilder(canvas.toBuffer(), {
- name: "bedwars-stats.png",
- });
- await interaction.editReply({
- content: `Here are **${properIGN}**'s BedWars stats:`,
- files: [attachment],
- });
- } catch (error) {
- console.error(error);
- await interaction.editReply({
- content: "An error occurred while fetching your stats.",
- });
- }
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment