import { GuildMember, CommandInteraction, SlashCommandBuilder } from 'discord.js'; import { Command } from '../types/index'; import { initHealth, resetInvincible } from '../lib/rpg/pvp'; import { formatItem, getItem, getItemQuantity, plainInventoryAutocomplete } from '../lib/rpg/items'; import { getBehavior, getBehaviors } from '../lib/rpg/behaviors'; import { Right } from '../lib/util'; export default { data: new SlashCommandBuilder() .setName('use') .setDescription('Use an item from your inventory') .addStringOption(option => option .setName('item') .setAutocomplete(true) .setDescription('The item to use') .setRequired(true) ) .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 itemID = parseInt(interaction.options.getString('item', true)); const item = await getItem(itemID); if (!item) return await interaction.followUp('Item does not exist!'); const itemInv = await getItemQuantity(member.id, item.id); if (itemInv.quantity <= 0) return await interaction.followUp(`You do not have ${formatItem(item)}!`); const behaviors = await getBehaviors(item); const messages = []; for (const itemBehavior of behaviors) { const behavior = getBehavior(itemBehavior.behavior); if (!behavior) continue; if (!behavior.onUse) continue; const res = await behavior.onUse({ value: itemBehavior.value, item, user: member.id }); if (res instanceof Right) { await interaction.followUp(`You tried to use ${formatItem(item)}... but failed!\n${res.getValue()}`); return; } else { messages.push(res.getValue()); } } await resetInvincible(member.id); return await interaction.followUp(`You used ${formatItem(item)}!\n${messages.map(m => `_${m}_`).join('\n')}`); }, autocomplete: plainInventoryAutocomplete, } satisfies Command;