Guest User

Untitled

a guest
Apr 5th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { SlashCommandBuilder, AttachmentBuilder } = require("discord.js");
  2. const axios = require("axios");
  3. const Canvas = require("canvas");
  4. const path = require("path");
  5. const MOJANG_API_URL = "https://api.mojang.com/users/profiles/minecraft";
  6.  
  7. module.exports = {
  8.   data: new SlashCommandBuilder()
  9.     .setName("stats")
  10.     .setDescription("Fetches BedWars stats from JartexNetwork")
  11.     .addStringOption((option) =>
  12.       option
  13.         .setName("username")
  14.         .setDescription("Enter Minecraft username")
  15.         .setRequired(true)
  16.     )
  17.     .addStringOption((option) =>
  18.       option
  19.         .setName("interval")
  20.         .setDescription("Stat interval: weekly, monthly, yearly, overall")
  21.         .setRequired(true)
  22.         .addChoices(
  23.           { name: "Weekly", value: "weekly" },
  24.           { name: "Monthly", value: "monthly" },
  25.           { name: "Yearly", value: "yearly" },
  26.           { name: "Overall", value: "total" }
  27.         )
  28.     )
  29.     .addStringOption((option) =>
  30.       option
  31.         .setName("mode")
  32.         .setDescription("Game mode")
  33.         .setRequired(true)
  34.         .addChoices(
  35.           { name: "Solos", value: "solo" },
  36.           { name: "Doubles", value: "doubles" },
  37.           { name: "Triples", value: "triples" },
  38.           { name: "Quads", value: "quad" }
  39.         )
  40.     ),
  41.  
  42.   async execute(interaction) {
  43.     async function getCorrectCasedUsername(ign) {
  44.       try {
  45.         const response = await axios.get(
  46.           `https://stats.jartexnetwork.com/api/profile/${ign}`
  47.         );
  48.         return response.data.username;
  49.       } catch (err) {
  50.         return null;
  51.       }
  52.     }
  53.  
  54.     const interval = interaction.options.getString("interval");
  55.     const mode = interaction.options.getString("mode");
  56.     const rawIGN = interaction.options.getString("username");
  57.     const properIGN = await getCorrectCasedUsername(rawIGN);
  58.  
  59.     if (!properIGN) {
  60.       return interaction.reply(
  61.         "❌ Could not find that username on Jartex. Please check the IGN."
  62.       );
  63.     }
  64.  
  65.     await interaction.deferReply();
  66.     try {
  67.       const mojangRes = await axios.get(`${MOJANG_API_URL}/${properIGN}`);
  68.       const uuid = mojangRes.data.id;
  69.       const skinURL = `https://crafatar.com/renders/body/${uuid}?overlay`;
  70.  
  71.       const apiURL = `https://stats.jartexnetwork.com/api/profile/${properIGN}/leaderboard?type=bedwars&interval=${interval}&mode=${mode}`;
  72.       const response = await axios.get(apiURL);
  73.       const stats = response.data;
  74.  
  75.       if (!stats) return;
  76.  
  77.       const getStat = (statName) => {
  78.         const entry = stats?.[statName]?.entries;
  79.         return entry && entry.length > 0 ? entry[0].value : "0";
  80.       };
  81.  
  82.       const wins = getStat("Wins");
  83.       const losses = getStat("Losses");
  84.       const finalKills = getStat("Final kills");
  85.       const finalDeaths = getStat("Final deaths");
  86.       const bedsBroken = getStat("Beds destroyed");
  87.       const bedsLost = getStat("Losses");
  88.       const gamesPlayed = getStat("Games played");
  89.       const winstreak = getStat("Highest winstreak reached");
  90.  
  91.       const winLossRatio = (wins / losses).toFixed(2);
  92.       const finalKillDeathRatio = (finalKills / finalDeaths).toFixed(2);
  93.       const bedsRatio = (bedsBroken / bedsLost).toFixed(2);
  94.  
  95.       const canvas = Canvas.createCanvas(800, 450);
  96.       const ctx = canvas.getContext("2d");
  97.  
  98.       const backgroundPath = path.join(__dirname, "background1.png");
  99.       const background = await Canvas.loadImage(backgroundPath);
  100.       ctx.drawImage(background, 0, 0, canvas.width, canvas.height);
  101.  
  102.       // Title
  103.       ctx.font = "bold 36px cursive";
  104.       ctx.fillStyle = "#FFC0CB";
  105.       ctx.fillText(properIGN, 50, 60);
  106.  
  107.       // Column 1
  108.       ctx.font = "24px cursive";
  109.       ctx.fillStyle = "#FFFFFF";
  110.       ctx.fillText(`Wins: ${wins}`, 50, 130);
  111.       ctx.fillText(`Losses: ${losses}`, 50, 170);
  112.       ctx.fillStyle = winLossRatio >= 1 ? "#00FF00" : "#FF0000";
  113.       ctx.fillText(`W/L Ratio: ${winLossRatio}`, 50, 210);
  114.  
  115.       // Column 2
  116.       ctx.fillStyle = "#FFFFFF";
  117.       ctx.fillText(`Final Kills: ${finalKills}`, 290, 130);
  118.       ctx.fillText(`Final Deaths: ${finalDeaths}`, 290, 170);
  119.       ctx.fillStyle = finalKillDeathRatio >= 1 ? "#00FF00" : "#FF0000";
  120.       ctx.fillText(`FKDR: ${finalKillDeathRatio}`, 290, 210);
  121.  
  122.       // Column 3
  123.       ctx.fillStyle = "#FFFFFF";
  124.       ctx.fillText(`Beds Broken: ${bedsBroken}`, 530, 130);
  125.       ctx.fillText(`Beds Lost: ${bedsLost}`, 530, 170);
  126.       ctx.fillStyle = bedsRatio >= 1 ? "#00FF00" : "#FF0000";
  127.       ctx.fillText(`Bed Ratio: ${bedsRatio}`, 530, 210);
  128.  
  129.       // Bottom row
  130.       ctx.fillStyle = "#FFFFFF";
  131.       ctx.fillText(`Winstreak: ${winstreak}`, 50, 300);
  132.       ctx.fillText(`Games Played: ${gamesPlayed}`, 290, 300);
  133.  
  134.       // Skin
  135.       const skinImage = await Canvas.loadImage(skinURL);
  136.       ctx.drawImage(skinImage, 610, 100, 150, 300);
  137.  
  138.       const attachment = new AttachmentBuilder(canvas.toBuffer(), {
  139.         name: "bedwars-stats.png",
  140.       });
  141.       await interaction.editReply({
  142.         content: `Here are **${properIGN}**'s BedWars stats:`,
  143.        files: [attachment],
  144.      });
  145.    } catch (error) {
  146.      console.error(error);
  147.      await interaction.editReply({
  148.        content: "An error occurred while fetching your stats.",
  149.      });
  150.    }
  151.  },
  152. };
  153.  
Advertisement
Add Comment
Please, Sign In to add comment