Skip to content

Commit

Permalink
refactor: switched to seraph cache for username conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kk committed Nov 25, 2023
1 parent 601a561 commit 18765d5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
13 changes: 6 additions & 7 deletions src/commands/skyblock/SkyblockNetworthCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { MinecraftBot } from "../../index";
import { getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { EmbedBuilder } from "discord.js";

class SkyblockNetworthCommand extends CommandBase {
Expand All @@ -11,12 +11,11 @@ class SkyblockNetworthCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
let playerUuid;
if (this.getBotInstance().getPlayerCache().has(player.toLowerCase()) && params.length == 0) {
const cachedPlayer = this.getBotInstance().getPlayerCache().get(player.toLowerCase());
if (cachedPlayer) playerUuid = cachedPlayer.uuid;
} else {
playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}

const selectedPlayerName = params.length == 0 ? cleanPlayerName : params[0].trim();
Expand Down
13 changes: 6 additions & 7 deletions src/commands/stats/BedwarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { formatNumber, formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { formatNumber, formatRatio, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { MinecraftBot } from "../../index";

class BedwarsStatisticsCommand extends CommandBase {
Expand All @@ -10,14 +10,13 @@ class BedwarsStatisticsCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
let playerUuid;
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

if (this.getBotInstance().getPlayerCache().has(player.toLowerCase()) && params.length == 0) {
const cachedPlayer = this.getBotInstance().getPlayerCache().get(player.toLowerCase());
if (cachedPlayer) playerUuid = cachedPlayer.uuid;
} else {
playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}

const playerStats = await hypixelClient.getPlayer(playerUuid);

if (playerStats) {
Expand Down
13 changes: 6 additions & 7 deletions src/commands/stats/DailyBedwarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { formatNumber, formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { formatNumber, formatRatio, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { MinecraftBot } from "../../index";

class BedwarsStatisticsCommand extends CommandBase {
Expand All @@ -10,14 +10,13 @@ class BedwarsStatisticsCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
let playerUuid;
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

if (this.getBotInstance().getPlayerCache().has(player.toLowerCase()) && params.length == 0) {
const cachedPlayer = this.getBotInstance().getPlayerCache().get(player.toLowerCase());
if (cachedPlayer) playerUuid = cachedPlayer.uuid;
} else {
playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}

const playerStats = await hypixelClient.getPlayer(playerUuid);
const cachedStats = await hypixelClient.getCachedPlayer(playerUuid, "daily");

Expand Down
8 changes: 6 additions & 2 deletions src/commands/stats/DuelsStatisticsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { formatNumber, formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { formatNumber, formatRatio, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { MinecraftBot } from "../../index";

class DuelsStatisticsCommand extends CommandBase {
Expand All @@ -10,8 +10,12 @@ class DuelsStatisticsCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

const playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}
const playerStats = await hypixelClient.getPlayer(playerUuid);
const duels_mode = params.length >= 2 ? `${params[1]}_duel_` : "";

Expand Down
13 changes: 6 additions & 7 deletions src/commands/stats/MonthlyBedwarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { formatNumber, formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { formatNumber, formatRatio, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { MinecraftBot } from "../../index";

class BedwarsStatisticsCommand extends CommandBase {
Expand All @@ -10,14 +10,13 @@ class BedwarsStatisticsCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
let playerUuid;
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

if (this.getBotInstance().getPlayerCache().has(player.toLowerCase()) && params.length == 0) {
const cachedPlayer = this.getBotInstance().getPlayerCache().get(player.toLowerCase());
if (cachedPlayer) playerUuid = cachedPlayer.uuid;
} else {
playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}

const playerStats = await hypixelClient.getPlayer(playerUuid);
const cachedStats = await hypixelClient.getCachedPlayer(playerUuid, "monthly");

Expand Down
9 changes: 7 additions & 2 deletions src/commands/stats/SkywarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { formatNumber, formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { formatNumber, formatRatio, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";
import { MinecraftBot } from "../../index";
import { getSkyWarsLevelInfo, getSkyWarsPrestigeForLevel } from "@zikeji/hypixel";

Expand All @@ -11,8 +11,13 @@ class DuelsStatisticsCommand extends CommandBase {
public execute = async ({ player, params }: CommandExecute) =>
useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
const cleanPlayerName = sanatiseMessage(player).trim();
const playerUuid = await this.getSeraphCache().getPlayerByName(params.length == 0 ? cleanPlayerName : params[0].trim());

if (!playerUuid) {
this.getBotInstance().getMineflayerInstance().chat(`Couldn't find a player by this name.`);
return;
}

const playerUuid = await getPlayerUuid(params.length == 0 ? cleanPlayerName : params[0].trim());
const playerStats = await hypixelClient.getPlayer(playerUuid);

if (playerStats) {
Expand Down

0 comments on commit 18765d5

Please sign in to comment.