Skip to content

Commit

Permalink
Restructure identifyPlayer?
Browse files Browse the repository at this point in the history
  • Loading branch information
horrible-little-slime committed Sep 4, 2024
1 parent ed9a02c commit 9c3710a
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions packages/oaf/src/commands/_player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,28 @@ export async function findPlayer(
export async function identifyPlayer(
input: string,
): Promise<string | [KoLPlayer, FindPlayerResult]> {
// If the input is a Discord mention, we'll try to set a player identifier there. Otherwise this just gets set to
// the same value as input.
let playerIdentifier;

// Whatever happens we'll try to ascertain whether this is a known player. Because we either do so at the start or
// at the end, declare a null variable here.
let knownPlayer: Awaited<ReturnType<typeof findPlayer>> = null;

// Check if this is a mention first of all
// Check if this is a discord mention
if (input.match(/^<@\d+>$/)) {
knownPlayer = await findPlayer({ discordId: input.slice(2, -1) });
const knownPlayer = await findPlayer({ discordId: input.slice(2, -1) });

if (knownPlayer === null) {
return "That user hasn't claimed a KoL account.";
}

playerIdentifier = knownPlayer.playerId;
} else {
playerIdentifier = input;
return [await kolClient.players.fetch(knownPlayer.playerId), knownPlayer];
}

if (
typeof playerIdentifier === "string" &&
!validPlayerIdentifier(playerIdentifier)
) {
// Validate if the string identifies a KoL player, either as a player ID or a user name
if (typeof input === "string" && !validPlayerIdentifier(input)) {
return "Come now, you know that isn't a player. Can't believe you'd try and trick me like this. After all we've been through? 😔";
}

const player = await kolClient.players.fetch(playerIdentifier, true);

if (!knownPlayer) {
knownPlayer = await findPlayer({ playerId: player.id });
}
const player = await kolClient.players.fetch(input, true);

if (!player)
return `According to KoL, player ${typeof playerIdentifier === "number" ? "#" : ""}${playerIdentifier} does not exist.`;
return `According to KoL, player ${typeof input === "number" ? "#" : ""}${input} does not exist.`;

const knownPlayer = await findPlayer({ playerId: player.id });

return [player, knownPlayer] as const;
return [player, knownPlayer];
}

0 comments on commit 9c3710a

Please sign in to comment.