jillo-bot/src/commands/eat.ts

64 lines
2.3 KiB
TypeScript

import { GuildMember, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { Command } from '../types/index';
import { initHealth, resetInvincible } from '../lib/rpg/pvp';
import { consumableInventoryAutocomplete, formatItem, formatItems, getItem, getItemQuantity, giveItem } from '../lib/rpg/items';
import { getBehavior, getBehaviors } from '../lib/rpg/behaviors';
import { Right } from '../lib/util';
export default {
data: new SlashCommandBuilder()
.setName('eat')
.setDescription('Eat an item from your inventory')
.addStringOption(option =>
option
.setName('item')
.setAutocomplete(true)
.setDescription('The item to eat')
.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 eat ${formatItems(item, 1)}... but failed!\n${res.getValue()}`);
return;
} else {
messages.push(res.getValue());
}
}
await resetInvincible(member.id);
const newInv = await giveItem(member.id, item, -1);
return await interaction.followUp(`You ate ${formatItems(item, 1)}!\n${messages.map(m => `_${m}_`).join('\n')}\nYou now have ${formatItems(item, newInv.quantity)}`);
},
autocomplete: consumableInventoryAutocomplete,
} satisfies Command;