diff --git a/src/commands/profile.ts b/src/commands/profile.ts index a5b3875..c3d8e3d 100644 --- a/src/commands/profile.ts +++ b/src/commands/profile.ts @@ -1,5 +1,5 @@ import { Command, CommandBuilder, CommandClient, CommandInteraction, Constants, SlashCommand } from "athena-prime"; -import { getUser } from "../utils"; +import { getPetPosition, getUser } from "../utils"; // ---------- @@ -16,6 +16,7 @@ class ProfileCommand extends Command { await interaction.defer(); const _user = await getUser(interaction.user.id); + const position = await getPetPosition(interaction.user.id); await interaction.createMessage({ embeds: [{ @@ -26,12 +27,23 @@ class ProfileCommand extends Command { color: 0xe77ed1, - // TODO: Add Rankings. title: `Your Profile | Pet the Pond`, fields: [ - { name: 'Pets', value: `${_user.statistics.pets}`, inline: true }, - { name: 'Rank', value: 'N/A', inline: true }, - { name: 'Participating', value: _user.settings.participating ? 'Yes' : 'No', inline: false } + { + name: 'Pets', + value: `${_user.statistics.pets}`, + inline: true + }, + { + name: 'Rank', + value: position ? `#${position}` : 'N/A', + inline: true + }, + { + name: 'Participating', + value: _user.settings.participating ? 'Yes' : 'No', + inline: false + } ], footer: { diff --git a/src/utils/database.ts b/src/utils/database.ts index 96dff4f..45504c4 100644 --- a/src/utils/database.ts +++ b/src/utils/database.ts @@ -93,4 +93,27 @@ export async function getHighestPets(limit = 10): Promise<{ userId: string, pets })); } +export async function getPetPosition(userId: string): Promise { + const result = await client<{ position: number }[]>` + SELECT position + FROM ( + SELECT + user_id, + RANK() OVER (ORDER BY pets DESC, user_id ASC) AS position + FROM ( + SELECT + user_id, + (data->'statistics'->>'pets')::integer AS pets + FROM users + WHERE + COALESCE((data->'flags'->>'blacklisted')::boolean, false) = false + AND COALESCE((data->'settings'->>'participating')::boolean, true) = true + ) ranked + ) ranked_users + WHERE user_id = ${userId} + `; + + return result[0]?.position ?? null; +} + //#endregion