Skip to content

Commit

Permalink
feat: add guild requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
kk committed Nov 3, 2023
1 parent 28657a6 commit 76fdd05
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
47 changes: 47 additions & 0 deletions src/commands/misc/GuildRequirementCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { CommandBase, CommandExecute } from "../../util/CommandHandler";
import { MinecraftBot } from "../../index";
import axios from "axios";
import { PlayerDB } from "../../util/CustomTypes";
import { formatRatio, getPlayerUuid, sanatiseMessage, useHypixelApi } from "../../util/CommonUtils";

class GuildRequirementCheck extends CommandBase {
constructor(minecraftBot: MinecraftBot) {
super({ name: "reqcheck", description: "Checks if a player meets the requirements..", minecraftBot });
}

public execute = async ({ player, message, params }: CommandExecute) => {
await useHypixelApi(this.getBotInstance(), async (hypixelClient) => {
if (params.length != 1) {
this.getBotInstance().getMineflayerInstance().chat("Please enter a name.");
return;
}
const cleanPlayerName = sanatiseMessage(params[0]).trim();
const playerStats = await hypixelClient.getPlayer(cleanPlayerName);

if (playerStats) {
let bedwarsRequirement = false, duelsRequirement = false;

const bedwars = playerStats?.stats?.Bedwars;
if (bedwars) {
const wins = bedwars?.wins_bedwars ?? 0;
const fkdr = formatRatio(bedwars?.final_kills_bedwars ?? 0, bedwars?.final_deaths_bedwars ?? 0);
bedwarsRequirement = wins > 4000 && Math.floor(Number(fkdr)) > 4;
}

const duels = playerStats?.stats?.Duels;
if (duels) {
const wins = duels['wins'] ? (duels['wins'] as number) : 0;
const losses = duels['losses'] ? (duels['losses'] as number) : 0;
const wlr = formatRatio(wins, losses);

duelsRequirement = wins > 6000 && Math.round(Number(wlr)) > 2.5;
}

const formattedString = `Bedwars: ${bedwarsRequirement ? '\u2713' : '\u2573'} | Duels: ${duelsRequirement ? '\u2713' : '\u2573'}`;
this.send("NONE", formattedString, playerStats);
} else {
this.getBotInstance().getMineflayerInstance().chat(`The player ${cleanPlayerName} is invalid!`);
}
});
};
}
2 changes: 1 addition & 1 deletion src/commands/stats/BedwarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BedwarsStatisticsCommand extends CommandBase {

const formattedString = `${star}\u272B FKDR: ${fkdr} | BBLR: ${bblr} | WLR: ${wlr} | FK: ${formatNumber(finals)} | Wins: ${formatNumber(wins)}`;

this.send("Bedwars", formattedString, playerStats);
this.send("bedwars", formattedString, playerStats);
} else {
this.getBotInstance().getMineflayerInstance().chat(`The player ${cleanPlayerName} is invalid!`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/stats/DuelsStatisticsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DuelsStatisticsCommand extends CommandBase {

const formattedString = `Kills: ${formatNumber(kills)} | Deaths: ${formatNumber(deaths)} | KDR: ${kdr} | Wins: ${formatNumber(wins)} | Losses: ${formatNumber(losses)} | WLR: ${wlr}`;

this.send("Duels", formattedString, playerStats);
this.send("duels", formattedString, playerStats);
} else {
this.getBotInstance().getMineflayerInstance().chat(`This player has no Duels stats!`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/stats/SkywarsStatisticsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DuelsStatisticsCommand extends CommandBase {

const formattedString = `${levelFormatted} Kills: ${formatNumber(kills)} | Deaths: ${formatNumber(deaths)} | KDR: ${kdr} | Wins: ${formatNumber(wins)} | Losses: ${formatNumber(losses)} | WLR: ${wlr}`;

this.send("Skywars", formattedString, playerStats);
this.send("skywars", formattedString, playerStats);
} else {
this.getBotInstance().getMineflayerInstance().chat(`This player has no Skywars stats!`);
}
Expand Down
11 changes: 8 additions & 3 deletions src/util/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ export abstract class CommandBase {

abstract execute({ player, message, params }: CommandExecute): void;

protected send = (gamemode: string, formattedString: string, playerStats: Player) => {
protected send = (gamemode: "bedwars" | "skywars" | "duels" | "NONE", formattedString: string, playerStats: Player) => {
const playerRank = getPlayerRank(playerStats);
const formattedRank = `${playerRank.cleanPrefix} ${playerStats.displayname.replaceAll("_", "\\_")}`;
const playerRankColour: HexColorString = `#${playerRank.colorHex}`;

this.getBotInstance().getMineflayerInstance().chat(formattedString);
const embed = new EmbedBuilder().setTitle(formattedRank).setDescription(`${gamemode} statistics for: ${formattedRank} | ${formattedString}`).setColor(playerRankColour).setThumbnail(`https://crafthead.net/avatar/${playerStats.uuid}`);
this.getBotInstance().sendToDiscord(embed);
if (gamemode != "NONE") {
const embed = new EmbedBuilder().setTitle(formattedRank).setDescription(`${gamemode.charAt(0).toUpperCase()+gamemode.slice(1)} statistics for: ${formattedRank} | ${formattedString}`).setColor(playerRankColour).setThumbnail(`https://crafthead.net/avatar/${playerStats.uuid}`);
this.getBotInstance().sendToDiscord(embed);
} else {
const embed = new EmbedBuilder().setTitle(formattedRank).setDescription(formattedString).setColor(playerRankColour).setThumbnail(`https://crafthead.net/avatar/${playerStats.uuid}`);
this.getBotInstance().sendToDiscord(embed);
}
};
}
22 changes: 18 additions & 4 deletions src/util/SeraphCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ export class SeraphCache {
"User-Agent": "hypixel-bridge-bot",
};

public getPlayer = async (uuid: string) => {
const { data, status } = await axios.get(`https://cache.seraph.si/player/${uuid}`, { headers: { ...this.headers } });
if (data.success && status == 200) {
return data.player as Components.Schemas.Player;
public getPlayerByName = async (userName: string)=>{
const { data, status } = await axios.get<{ id: string; name: string }>(`https://cache.seraph.si/seraph/username/${userName}`, { headers: { ...this.headers } });
if (status == 200) {
return data.id;
} else {
return null;
}
}

public getPlayer = async (uuid: string) => {
let checkedUuid;
if (uuid.length == 32 || uuid.length == 36) {
checkedUuid = uuid;
} else {
const res = await this.getPlayerByName(uuid);
if (res == null) return null;
checkedUuid = res;
}

const { data, status } = await axios.get(`https://cache.seraph.si/player/${checkedUuid}`, { headers: { ...this.headers } });
return data.success && status == 200 ? (data.player as Components.Schemas.Player) : null;
};

public getGuildByPlayer = async (uuid: string) => {
Expand Down

0 comments on commit 76fdd05

Please sign in to comment.