Moved to hypixel api for check command

Added seperate file for functions
This commit is contained in:
2023-07-02 15:49:28 +02:00
parent f0b73a8502
commit 3c6aaa95f9
2 changed files with 317 additions and 185 deletions

View File

@@ -1,188 +1,237 @@
const {SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits} = const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } =
require("discord.js"); require("discord.js");
const {bwfdkr, bwstars, bwwins, duelswins, swstars} = const { bwfdkr, bwstars, bwwins, duelswins, swstars } =
require("../config/reqs.json"); require("../config/reqs.json");
const env = require("dotenv").config(); const env = require("dotenv").config();
const hypixelApiKey = process.env.HYPIXELAPI; const hypixelApiKey = process.env.HYPIXELAPIKEY;
const {color} = require("../config/options.json"); const { color } = require("../config/options.json");
const fetch = require("axios"); const fetch = require("axios");
const { getExactLevel, skywarsLevel, getLevelForExp } = require("../utils/functions.js");
module.exports = { module.exports = {
name : "check", name: "check",
description : "Check a player's stats.", description: "Check a player's stats.",
type : "slash", type: "slash",
data : new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("check") .setName("check")
.setDescription("Check a player's stats.") .setDescription("Check a player's stats.")
.addStringOption((option) => option.setName("ign") .addStringOption((option) => option.setName("ign")
.setDescription("The player's IGN.") .setDescription("The player's IGN.")
.setRequired(true)) .setRequired(true))
.setDMPermission(false), .setDMPermission(false),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply({}); await interaction.deferReply({});
const ign = interaction.options.getString("ign"); const ign = interaction.options.getString("ign");
const mojang = "https://api.mojang.com/users/profiles/minecraft/"; const mojang = "https://api.mojang.com/users/profiles/minecraft/";
const slothPixel = "https://api.slothpixel.me/api/players/"; // const slothPixel = "https://api.slothpixel.me/api/players/";
const guildAPI = "https://api.slothpixel.me/api/guilds/"; // const guildAPI = "https://api.slothpixel.me/api/guilds/";
const minotar = "https://minotar.net/helm/"; const hypixel = "https://api.hypixel.net/player"
const embedColor = Number(color.replace("#", "0x")); const guildAPI = "https://api.hypixel.net/guild"
const minotar = "https://minotar.net/helm/";
const embedColor = Number(color.replace("#", "0x"));
const head = minotar + ign;
if (!ign) { if (!ign) {
await interaction.editReply("Please provide a player's IGN."); await interaction.editReply("Please provide a player's IGN.");
return; return;
} }
try { try {
await fetch(mojang + ign); await fetch(mojang + ign);
} catch (error) { } catch (error) {
interaction.editReply({ interaction.editReply({
embeds : [ embeds: [
{description : "That player doesn't exist.", color : embedColor} { description: "That player doesn't exist.", color: embedColor }
] ]
}); });
return; return;
} }
const userCheck = await fetch(mojang + ign); const userCheck = await fetch(mojang + ign);
const uuid = userCheck.data.id; const uuid = userCheck.data.id;
try { /* try {
await fetch(slothPixel + uuid); await fetch(slothPixel + uuid);
} catch (error) { } catch (error) {
interaction.editReply({ interaction.editReply({
embeds : [ { embeds: [{
description : "That player hasn't played Hypixel before.", description: "That player hasn't played Hypixel before.",
color : embedColor color: embedColor
} ] }]
}); });
return; return;
} } */
const stats = await fetch(slothPixel + uuid); const player = hypixel + "?key=" + hypixelApiKey + "&uuid=" + uuid
const head = minotar + ign; const stats = await fetch(player);
if (!stats.data.uuid) { if (!stats.data.player) {
interaction.editReply({ interaction.editReply({
embeds : [ { embeds: [{
description : "That player hasn't played Hypixel before.", description: "That player hasn't played Hypixel before.",
color : embedColor color: embedColor
} ] }]
}); });
return; return;
} }
const rank_formatted = stats.data.rank_formatted; const rank2 = stats.data.player.newPackageRank;
const rank2 = rank_formatted.replace(/&[0-9a-fk-or]/g, ""); const monthlyRank = stats.data.player.monthlyPackageRank;
if (rank2 === "") { if (rank2 === 'VIP') {
var rank = ""; var rank = "[VIP] "
} else { } else if (rank2 === 'VIP_PLUS') {
var rank = rank2 + " "; var rank = "[VIP+] "
} } else if (rank2 === 'MVP') {
var rank = "[MVP] "
} else if (rank2 === 'MVP_PLUS') {
var rank = "[MVP+] "
} else if (rank2 === 'MVP_PLUS' && monthlyRank === 'SUPERSTAR') {
var rank = "[MVP++] "
}
try { /* try {
const guildCheck = await fetch(guildAPI + uuid); const guildCheck = await fetch(guildAPI + uuid);
var guildName = guildCheck.data.name; var guildName = guildCheck.data.name;
} catch (error) { } catch (error) {
var guildName = "None"; var guildName = "None";
} } */
const hsbwstars = stats.data.stats.BedWars.level; const guild = guildAPI + "?key=" + hypixelApiKey + "&player=" + uuid
const hsbwfkdr = stats.data.stats.BedWars.final_k_d; const guildCheck = await fetch(guild);
const hsbwwins = stats.data.stats.BedWars.wins;
const hsswstars = stats.data.stats.SkyWars.level;
const hsduelswins = stats.data.stats.Duels.general.wins;
if (hsbwstars < bwstars || hsbwfkdr < bwfdkr || hsbwwins < bwwins) { if (!guildCheck.data.guild) {
var bwtitle = var guildName = "None";
"<a:cross_a:1087808606897983539> This player does not meet the BedWars requirements."; } else {
} else { var guildName = guildCheck.data.guild.name;
var bwtitle = }
"<a:check_a:1087808632172847134> This player meets the BedWars requirements.";
}
if (hsswstars < swstars) { if (!guildCheck.data.guild) {
var swtitle = var guildTag = ""
"<a:cross_a:1087808606897983539> This player does not meet the SkyWars requirements."; } else if (!guildCheck.data.guild.tag) {
} else { var guildTag = ""
var swtitle = } else {
"<a:check_a:1087808632172847134> This player meets the SkyWars requirements."; var guildTag = " [" + guildCheck.data.guild.tag + "]"
} }
if (hsduelswins < duelswins) { //bedwars level
var duelstitle = const hsbwexp = stats.data.player.stats.Bedwars.Experience;
"<a:cross_a:1087808606897983539> This player does not meet the Duels requirements."; const hsbwstars = getLevelForExp(hsbwexp);
} else { // bedwars fkdr
var duelstitle = const hsbwfk = stats.data.player.stats.Bedwars.final_kills_bedwars;
"<a:check_a:1087808632172847134> This player meets the Duels requirements."; const hsbwfd = stats.data.player.stats.Bedwars.final_deaths_bedwars;
} const hsbwfkdr = hsbwfk / hsbwfd;
// bedwars wins
const hsbwwins = stats.data.player.stats.Bedwars.wins_bedwars;
// skywars level
const hsswexp = stats.data.player.stats.SkyWars.skywars_experience;
const hsswstars = skywarsLevel(hsswexp);
// skywars kdr
const hsswkills = stats.data.player.stats.SkyWars.kills;
const hsswdeaths = stats.data.player.stats.SkyWars.deaths;
const hsswkd = hsswkills / hsswdeaths;
//skywars wins
const hsswwins = stats.data.player.stats.SkyWars.wins;
// dueks kdr
const hsduelskills = stats.data.player.stats.Duels.kills
const hsduelsdeaths = stats.data.player.stats.Duels.deaths
const hsduelskd = hsduelskills / hsduelsdeaths
// duels wins
const hsduelswins = stats.data.player.stats.Duels.wins;
// duels wlr
const hsduelslosses = stats.data.player.stats.Duels.losses;
const hsduelswlr = hsduelswins / hsduelslosses;
// network level
const hypixelExp = stats.data.player.networkExp;
const level = getExactLevel(hypixelExp);
if (hsbwstars < bwstars || hsbwfkdr < bwfdkr || hsbwwins < bwwins) {
var bwtitle =
"<a:cross_a:1087808606897983539> This player does not meet the BedWars requirements.";
} else {
var bwtitle =
"<a:check_a:1087808632172847134> This player meets the BedWars requirements.";
}
try { if (hsswstars < swstars) {
const guildCheck = await fetch(guildAPI + uuid); var swtitle =
const tag_formatted = guildCheck.data.tag_formatted; "<a:cross_a:1087808606897983539> This player does not meet the SkyWars requirements.";
const guildTag2 = tag_formatted.replace(/&[0-9a-fk-or]/g, ""); } else {
var guildTag = " " + guildTag2; var swtitle =
} catch (error) { "<a:check_a:1087808632172847134> This player meets the SkyWars requirements.";
var guildTag = ""; }
}
await interaction.editReply({ if (hsduelswins < duelswins) {
embeds : [ { var duelstitle =
title : rank + stats.data.username + guildTag, "<a:cross_a:1087808606897983539> This player does not meet the Duels requirements.";
description : "**Network Level:** `" + } else {
stats.data.level.toString() + "`\n" + var duelstitle =
"**Current Guild:** `" + guildName + "`", "<a:check_a:1087808632172847134> This player meets the Duels requirements.";
color : embedColor, }
thumbnail : {url : head},
footer : { /* try {
text : interaction.guild.name + " | Developed by @Taken#0002", const guildCheck = await fetch(guildAPI + uuid);
icon_url : interaction.guild.iconURL() const tag_formatted = guildCheck.data.tag_formatted;
}, const guildTag2 = tag_formatted.replace(/&[0-9a-fk-or]/g, "");
fields : [ var guildTag = " " + guildTag2;
{ } catch (error) {
name : bwtitle, var guildTag = "";
value : "**➺ Stars:** `" + } */
stats.data.stats.BedWars.level.toString() + " / " +
bwstars.toString() + "`\n" + await interaction.editReply({
"**➺ FKDR:** `" + embeds: [{
stats.data.stats.BedWars.final_k_d.toString() + title: rank + stats.data.player.displayname + guildTag,
" / " + bwfdkr.toString() + "`\n" + description: "**Network Level:** `" +
"**➺ Wins:** `" + level.toFixed(2).toString() + "`\n" +
stats.data.stats.BedWars.wins.toString() + " / " + "**Current Guild:** `" + guildName + "`",
bwwins.toString() + "`" color: embedColor,
}, thumbnail: { url: head },
{ footer: {
name : swtitle, text: interaction.guild.name + " | Developed by @Taken#0002",
value : icon_url: interaction.guild.iconURL()
"**➺ Stars:** `" + },
stats.data.stats.SkyWars.level.toFixed(2).toString() + fields: [
" / " + swstars.toString() + "`\n" + {
"**➺ KDR:** `" + name: bwtitle,
stats.data.stats.SkyWars.kill_death_ratio.toString() + value: "**➺ Stars:** `" +
"`\n" + hsbwstars.toFixed(2).toString() + " / " +
"**➺ Wins:** `" + bwstars.toString() + "`\n" +
stats.data.stats.SkyWars.wins.toString() + "`" "**➺ FKDR:** `" +
}, hsbwfkdr.toFixed(2).toString() +
{ " / " + bwfdkr.toString() + "`\n" +
name : duelstitle, "**➺ Wins:** `" +
value : "**➺ Wins:** `" + hsbwwins.toString() + " / " +
stats.data.stats.Duels.general.wins.toString() + bwwins.toString() + "`"
" / " + duelswins.toString() + "`\n" + },
"**➺ KDR:** `" + {
stats.data.stats.Duels.general.kd_ratio.toFixed(2) name: swtitle,
.toString() + value:
"`\n" + "**➺ Stars:** `" +
"**➺ WLR:** `" + hsswstars.toFixed(2).toString() +
stats.data.stats.Duels.general.win_loss_ratio " / " + swstars.toString() + "`\n" +
.toFixed(2) "**➺ KDR:** `" +
.toString() + hsswkd.toFixed(2).toString() +
"`" "`\n" +
} "**➺ Wins:** `" +
] hsswwins.toString() + "`"
} ] },
}); {
} name: duelstitle,
}; value: "**➺ Wins:** `" +
hsduelswins.toString() +
" / " + duelswins.toString() + "`\n" +
"**➺ KDR:** `" +
hsduelskd.toFixed(2).toString() +
"`\n" +
"**➺ WLR:** `" +
hsduelswlr.toFixed(2).toString() +
"`"
}
]
}]
});
}
};

View File

@@ -1,26 +1,109 @@
const fetch = require("axios"); /* ---------- bedwars level ---------- */
function getExpForLevel(level){
if(level == 0) return 0;
async function getuuid(ign) { var respectedLevel = getLevelRespectingPrestige(level);
const mojangAPI = "https://api.mojang.com/users/profiles/minecraft/" if(respectedLevel > EASY_LEVELS){
return 5000;
}
try { switch(respectedLevel){
const user = await fetch(mojangAPI + ign) case 1:
return user.data.id; return 500;
} catch (error) { case 2:
return null; return 1000;
} case 3:
return 2000;
case 4:
return 3500;
}
return 5000;
} }
async function getplayer(uuid) { function getLevelRespectingPrestige(level){
const slothPixel = "https://api.slothpixel.me/api/players/"; if(level > HIGHEST_PRESTIGE * LEVELS_PER_PRESTIGE){
return level - HIGHEST_PRESTIGE * LEVELS_PER_PRESTIGE;
}
else {
return level % LEVELS_PER_PRESTIGE;
}
}
const EASY_LEVELS = 4;
const EASY_LEVELS_XP = 7000;
const XP_PER_PRESTIGE = 96 * 5000 + EASY_LEVELS_XP;
const LEVELS_PER_PRESTIGE = 100;
const HIGHEST_PRESTIGE = 50;
function getLevelForExp(exp){
var prestiges = Math.floor(exp / XP_PER_PRESTIGE);
var level = prestiges * LEVELS_PER_PRESTIGE;
var expWithoutPrestiges = exp - (prestiges * XP_PER_PRESTIGE);
try { for(let i = 1; i <= EASY_LEVELS; ++i){
const player = await fetch(slothPixel + uuid) var expForEasyLevel = getExpForLevel(i);
return player if(expWithoutPrestiges < expForEasyLevel){
} catch (error) { break;
return null; }
} level++;
expWithoutPrestiges -= expForEasyLevel;
}
return level + expWithoutPrestiges / 5000
}
// hypixel
const BASE = 10000;
const GROWTH = 2500;
const HALF_GROWTH = 0.5 * GROWTH;
const REVERSE_PQ_PREFIX = -(BASE - 0.5 * GROWTH) / GROWTH;
const REVERSE_CONST = REVERSE_PQ_PREFIX * REVERSE_PQ_PREFIX;
const GROWTH_DIVIDES_2 = 2 / GROWTH;
function getLevel(exp) {
return exp <= 1 ? 1 : Math.floor(1 + REVERSE_PQ_PREFIX + Math.sqrt(REVERSE_CONST + GROWTH_DIVIDES_2 * exp));
} }
module.exports = getuuid function getExactLevel(exp) {
module.exports = getplayer return getLevel(exp) + getPercentageToNextLevel(exp);
}
function getExpFromLevelToNext(level) {
return level < 1 ? BASE : GROWTH * (level - 1) + BASE;
}
function getTotalExpToLevel(level) {
const lv = Math.floor(level); const
x0 = getTotalExpToFullLevel(lv);
if (level === lv) return x0;
return (getTotalExpToFullLevel(lv + 1) - x0) * (level % 1) + x0;
}
function getTotalExpToFullLevel(level) {
return (HALF_GROWTH * (level - 2) + BASE) * (level - 1);
}
function getPercentageToNextLevel(exp) {
const lv = getLevel(exp);
const x0 = getTotalExpToLevel(lv);
return (exp - x0) / (getTotalExpToLevel(lv + 1) - x0);
}
/* ---------- skywars level ---------- */
function skywarsLevel(xp) {
var xps = [0, 20, 70, 150, 250, 500, 1000, 2000, 3500, 6000, 10000, 15000];
let exactLevel = 0
if (xp >= 15000) {
exactLevel = (xp - 15000) / 10000 + 12;
return exactLevel;
} else {
for (i = 0; i < xps.length; i++) {
if (xp < xps[i]) {
exactLevel = i + (xp - xps[i-1]) / (xps[i] - xps[i-1]);
return exactLevel;
}
}
}
}
module.exports = {
getExactLevel,
skywarsLevel,
getLevelForExp
}