jillo-bot/src/commands/inventory.ts

33 lines
1.2 KiB
TypeScript

import { GuildMember, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { ItemInventory, db } from '../lib/db';
import { formatItems, getItem } from '../lib/rpg/items';
import { initHealth } from '../lib/rpg/pvp';
import { Command } from '../types/index';
export default {
data: new SlashCommandBuilder()
.setName('inventory')
.setDescription('Check your inventory')
.setDMPermission(false),
execute: async (interaction: CommandInteraction) => {
if (!interaction.isChatInputCommand()) return;
const member = interaction.member! as GuildMember;
await initHealth(member.id);
await interaction.deferReply({ephemeral: true});
const itemsList = await db<ItemInventory>('itemInventories')
.select('item', 'quantity')
.where('user', member.user.id);
// kind of stupid kind of awful
const items = (await Promise.all(itemsList.map(async i => ({item: await getItem(i.item), quantity: i.quantity})))).filter(i => i.item && i.quantity !== 0);
await interaction.followUp(
`Your inventory:\n${items.length === 0 ? '_Your inventory is empty!_' : items.map(i => `- ${formatItems(i.item!, i.quantity)}\n_${i.item!.description}_`).join('\n')}`
);
}
} satisfies Command;