From adcf0eae9482cc7567faa384704b3348653b4028 Mon Sep 17 00:00:00 2001 From: DuckySoLucky Date: Fri, 21 Apr 2023 19:24:53 +0200 Subject: [PATCH] refactor: `!ww` command --- data/skyblockNotifer.json | 4 +- src/contracts/minecraftCommand.js | 13 ++++--- src/minecraft/commands/skyblockCommand.js | 4 +- .../commands/skyblockLevelCommand.js | 4 +- src/minecraft/commands/woolwarsCommand.js | 38 +++++++------------ 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/data/skyblockNotifer.json b/data/skyblockNotifer.json index 709c91da..f9fac413 100644 --- a/data/skyblockNotifer.json +++ b/data/skyblockNotifer.json @@ -163,6 +163,8 @@ "Blitz Duo 3rd Iteration Tournament Results! | https://hypixel.net/threads/blitz-duo-3rd-iteration-tournament-results.5318905/", "SkyBlock Update 0.18.2 - Garden and Stranded Update | https://hypixel.net/threads/skyblock-update-0-18-2-garden-and-stranded-update.5317809/", "[March 24] Various Grammatical Fixes | https://hypixel.net/threads/march-24-various-grammatical-fixes.5314082/", - "10 Year Anniversary Contests - Results + Surprise Gift! | https://hypixel.net/threads/10-year-anniversary-contests-results-surprise-gift.5333726/" + "10 Year Anniversary Contests - Results + Surprise Gift! | https://hypixel.net/threads/10-year-anniversary-contests-results-surprise-gift.5333726/", + "SkyBlock Patch 0.18.3 - Garden Crop Growth & Presets, Bazaar & Sack Additions, Bug Fixes, & More | https://hypixel.net/threads/skyblock-patch-0-18-3-garden-crop-growth-presets-bazaar-sack-additions-bug-fixes-more.5324136/", + "UHC Modifiers, Achievements & Bug fixes | https://hypixel.net/threads/uhc-modifiers-achievements-bug-fixes.4035874/" ] } diff --git a/src/contracts/minecraftCommand.js b/src/contracts/minecraftCommand.js index 9735f118..a2c3edc7 100644 --- a/src/contracts/minecraftCommand.js +++ b/src/contracts/minecraftCommand.js @@ -21,20 +21,21 @@ class minecraftCommand { const listener = async (msg) => { if (msg.toString().includes('You are sending commands too fast! Please slow down.') && !msg.toString().includes(':')) { bot.removeListener("message", listener); + n++; - if (n === 5) { - n++; + if (n >= 5) { return this.send("/gc Command failed to send message after 5 attempts. Please try again later."); } - await delay(1000); + await delay(69.420); return this.send(message); } else if (msg.toString().includes("You cannot say the same message twice!") === true && msg.toString().includes(":") === false && message.startsWith("/gc") === true) { bot.removeListener("message", listener); + n++; - if (n === 5) { + if (n >= 5) { return this.send("/gc Command failed to send message after 5 attempts. Please try again later."); } @@ -47,7 +48,9 @@ class minecraftCommand { bot.chat(message); setTimeout(() => { - bot.removeListener("message", listener); + if (bot.listenerCount("message") > 0) { + bot.removeListener("message", listener); + } }, 500); } diff --git a/src/minecraft/commands/skyblockCommand.js b/src/minecraft/commands/skyblockCommand.js index fd1c1cb3..60b8624b 100644 --- a/src/minecraft/commands/skyblockCommand.js +++ b/src/minecraft/commands/skyblockCommand.js @@ -98,7 +98,9 @@ class SkyblockCommand extends minecraftCommand { this.send( `/gc ${username}'s Level: ${ - data.profile.leveling.experience / 100 + data.profile.leveling?.experience + ? data.profile.leveling.experience / 100 + : 0 } | Senither Weight: ${senitherWeight} | Lily Weight: ${lilyWeight} | Skill Average: ${skillAverage} | Slayer: ${slayerXp} | Catacombs: ${catacombsLevel} | Class Average: ${classAverage} | Networth: ${networthValue} | Accessories: ${talismanCount} | Recombobulated: ${recombobulatedCount} | Enriched: ${enrichmentCount}` ); } catch (error) { diff --git a/src/minecraft/commands/skyblockLevelCommand.js b/src/minecraft/commands/skyblockLevelCommand.js index 804b812f..63b0a1d6 100644 --- a/src/minecraft/commands/skyblockLevelCommand.js +++ b/src/minecraft/commands/skyblockLevelCommand.js @@ -30,7 +30,9 @@ class CatacombsCommand extends minecraftCommand { this.send( `/gc ${username}'s Skyblock Level: ${ - data.profile.leveling.experience / 100 + data.profile.leveling?.experience + ? data.profile.leveling.experience / 100 + : 0 }` ); } catch (error) { diff --git a/src/minecraft/commands/woolwarsCommand.js b/src/minecraft/commands/woolwarsCommand.js index e91aa8be..ca8367d4 100644 --- a/src/minecraft/commands/woolwarsCommand.js +++ b/src/minecraft/commands/woolwarsCommand.js @@ -1,8 +1,5 @@ const minecraftCommand = require("../../contracts/minecraftCommand.js"); -const config = require("../../../config.json"); -const axios = require("axios"); -const { toFixed } = require("../../contracts/helperFunctions.js"); -const { getUUID } = require("../../contracts/API/PlayerDBAPI.js"); +const hypixel = require("../../contracts/API/HypixelRebornAPI.js"); class WoolwarsCommand extends minecraftCommand { constructor(minecraft) { @@ -24,42 +21,35 @@ class WoolwarsCommand extends minecraftCommand { try { username = this.getArgs(message)[0] || username; - const uuid = await getUUID(username); - const response = ( - await axios.get( - `https://api.hypixel.net/player?key=${config.minecraft.API.hypixelAPIkey}&uuid=${uuid}` - ) - ).data; + const response = await hypixel.getPlayer(username, { raw: true }); if (response.player === null) { // eslint-disable-next-line no-throw-literal throw "This player has never joined Hypixel."; } - const woolWars = response?.player?.stats?.WoolGames?.wool_wars; + const woolWars = response?.player?.stats?.WoolGames?.wool_wars?.stats; if (woolWars == undefined) { // eslint-disable-next-line no-throw-literal throw "This player has never played WoolWars."; } - const level = - getWoolWarsStar( - response?.player?.stats?.WoolGames?.progression?.experience - ) || 0; + const experience = + response.player?.stats?.WoolGames?.progression?.experience ?? 0; + const level = getWoolWarsStar(experience); this.send( - `/gc [${toFixed(level, 0)}✫] ${username}: W: ${ - woolWars.stats.wins - } | WLR: ${toFixed( - woolWars.stats.wins / woolWars.stats.games_played, + `/gc [${Math.floor(level)}✫] ${username}: W: ${woolWars.wins} | WLR: ${( + woolWars.wins / woolWars.games_played + ).toFixed(2)} | KDR: ${(woolWars.kills / woolWars.deaths).toFixed( 2 - )} | KDR: ${toFixed( - woolWars.stats.kills / woolWars.stats.deaths, + )} | BB: ${woolWars.blocks_broken} | WP: ${ + woolWars.wool_placed + } | WPP: ${(woolWars.wool_placed / woolWars.games_played).toFixed( 2 - )} | BB: ${woolWars.stats.blocks_broken} | WP: ${ - woolWars.stats.wool_placed - }` + )} | WPG: ${(woolWars.wool_placed / woolWars.blocks_broken).toFixed(2)} + ` ); } catch (error) { this.send(`/gc Error: ${error}`);