From 94b42c86387d0294089e9d46720cef2e89848338 Mon Sep 17 00:00:00 2001 From: holzmaster Date: Mon, 9 Sep 2024 20:23:51 +0200 Subject: [PATCH] Post item card --- src/commands/gegenstand.ts | 30 +++++++++++++++++++++++++++++- src/service/loot.ts | 12 ++++++++---- src/storage/loot.ts | 12 +++++++++++- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/commands/gegenstand.ts b/src/commands/gegenstand.ts index f857cf41..c7dd9347 100644 --- a/src/commands/gegenstand.ts +++ b/src/commands/gegenstand.ts @@ -1,6 +1,7 @@ import { type AutocompleteInteraction, type CommandInteraction, + InteractionType, SlashCommandBuilder, SlashCommandStringOption, SlashCommandSubcommandBuilder, @@ -115,7 +116,34 @@ export default class GegenstandCommand implements ApplicationCommand { }); } - async #showItemInfo(interaction: CommandInteraction, context: BotContext) {} + async #showItemInfo(interaction: CommandInteraction, _context: BotContext) { + if (!interaction.isChatInputCommand()) { + throw new Error("Interaction is not a chat input command"); + } + + const itemId = Number(interaction.options.getString("item")); + if (!Number.isSafeInteger(itemId)) { + throw new Error("Invalid item ID"); + } + + const item = await lootService.getUserLootById(interaction.user.id, itemId); + if (!item) { + await interaction.reply({ + content: "Diesen Gegensand hast du nicht.", + }); + return; + } + + await interaction.reply({ + embeds: [ + { + title: item.displayName, + description: item.description, + color: 0x00ff00, + }, + ], + }); + } async autocomplete(interaction: AutocompleteInteraction) { const subCommand = interaction.options.getSubcommand(true); diff --git a/src/service/loot.ts b/src/service/loot.ts index 98bbf976..d0288e84 100644 --- a/src/service/loot.ts +++ b/src/service/loot.ts @@ -616,15 +616,19 @@ export function resolveLootTemplate(lootKindId: number) { } export async function getUserLootsByTypeId(userId: Snowflake, lootTypeId: number) { - return await loot.getUserLootsById(userId, lootTypeId); + return await loot.getUserLootsByTypeId(userId, lootTypeId); +} + +export async function getUserLootById(userId: Snowflake, id: LootId) { + return await loot.getUserLootById(userId, id); } export async function getUserLootCountById(userId: Snowflake, lootTypeId: number): Promise { - return (await loot.getUserLootsById(userId, lootTypeId)).length; + return (await loot.getUserLootsByTypeId(userId, lootTypeId)).length; } -export async function getLootsByKindId(lootTykeId: LootTypeId) { - return await loot.getLootsByKindId(lootTykeId); +export async function getLootsByKindId(lootTypeId: LootTypeId) { + return await loot.getLootsByKindId(lootTypeId); } export function transferLootToUser(lootId: LootId, user: User, trackPredecessor: boolean) { diff --git a/src/storage/loot.ts b/src/storage/loot.ts index c402620c..959e049a 100644 --- a/src/storage/loot.ts +++ b/src/storage/loot.ts @@ -70,7 +70,7 @@ export async function findOfMessage(message: Message, ctx = db()) { .executeTakeFirst(); } -export async function getUserLootsById(userId: User["id"], lootKindId: number, ctx = db()) { +export async function getUserLootsByTypeId(userId: User["id"], lootKindId: number, ctx = db()) { return await ctx .selectFrom("loot") .where("winnerId", "=", userId) @@ -80,6 +80,16 @@ export async function getUserLootsById(userId: User["id"], lootKindId: number, c .execute(); } +export async function getUserLootById(userId: User["id"], lootId: LootId, ctx = db()) { + return await ctx + .selectFrom("loot") + .where("winnerId", "=", userId) + .where("id", "=", lootId) + .where(notDeleted) + .selectAll() + .executeTakeFirst(); +} + export async function getLootsByKindId(lootKindId: number, ctx = db()) { return await ctx .selectFrom("loot")