From 37af0ea68fcfb3fd273acabf2def5621452bbc19 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Sat, 18 Nov 2023 17:12:27 +0300 Subject: [PATCH] fixes i would've caught if i tested this for more than 2 seconds --- src/commands/item.ts | 8 +++++--- src/commands/recipe.ts | 35 ++++++++++++++++----------------- src/lib/rpg/items.ts | 44 ++++++++++++++++++++++++++---------------- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/commands/item.ts b/src/commands/item.ts index b19f412..1994962 100644 --- a/src/commands/item.ts +++ b/src/commands/item.ts @@ -1,6 +1,6 @@ import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js'; import { CustomCraftingRecipeItem, CustomItem, db } from '../lib/db'; -import { formatItem, formatItems, getItem, giveItem, itemAutocomplete } from '../lib/rpg/items'; +import { customItemAutocomplete, formatItem, formatItems, getItem, giveItem, itemAutocomplete } from '../lib/rpg/items'; import { behaviors } from '../lib/rpg/behaviors'; import { Command } from '../types/index'; import { formatRecipe, getCustomRecipe } from '../lib/rpg/recipes'; @@ -177,7 +177,7 @@ export default { .setDescription('[ADMIN] Delete a custom item') .addStringOption(opt => opt - .setName('item') + .setName('customitem') .setDescription('The item') .setAutocomplete(true) .setRequired(true) @@ -223,7 +223,7 @@ export default { await interaction.followUp(`${user.toString()} now has ${formatItems(item, inv.quantity)}.`); } else if (subcommand === 'delete') { - const itemID = parseInt(interaction.options.getString('item', true)); + const itemID = parseInt(interaction.options.getString('customitem', true)); const item = await getItem(itemID); if (!item) return interaction.followUp('No such item exists!'); @@ -250,6 +250,8 @@ export default { if (focused.name === 'item') { return itemAutocomplete(interaction); + } else if (focused.name === 'customitem') { + return customItemAutocomplete(interaction); } } } satisfies Command; \ No newline at end of file diff --git a/src/commands/recipe.ts b/src/commands/recipe.ts index a2d1b68..47de3fe 100644 --- a/src/commands/recipe.ts +++ b/src/commands/recipe.ts @@ -98,20 +98,22 @@ export default { parsed = await Promise.all( recipeString .split('|') - .map(items => - Promise.all( - items - .split(';') - .map(itemStack => - itemStack.split(',') - ) - .map(async ([itemID, quantity]) => ( - { - item: (await getItem(parseInt(itemID)))!, - quantity: parseInt(quantity) - } - )) - ) + .map(async items => + items === '' ? + [] : + await Promise.all( + items + .split(';') + .map(itemStack => + itemStack.split(',') + ) + .map(async ([itemID, quantity]) => ( + { + item: (await getItem(parseInt(itemID)))!, + quantity: parseInt(quantity) + } + )) + ) ) ) as Items[][]; } catch (err) { @@ -237,10 +239,7 @@ export default { const focused = interaction.options.getFocused(true); if (focused.name === 'recipe') { - const station = interaction.options.getString('station'); - - const customRecipes = await db('customCraftingRecipes') - .where('station', station); + const customRecipes = await db('customCraftingRecipes'); const resolvedCustomRecipes = await Promise.all(customRecipes.map(resolveCustomRecipe)); diff --git a/src/lib/rpg/items.ts b/src/lib/rpg/items.ts index 4edcab0..3aca51b 100644 --- a/src/lib/rpg/items.ts +++ b/src/lib/rpg/items.ts @@ -251,21 +251,31 @@ export function formatItemsArray(items: Items[], disableBold = false) { return items.map(i => formatItems(i.item, i.quantity, disableBold)).join(' '); } -export async function itemAutocomplete(interaction: AutocompleteInteraction) { - const focused = interaction.options.getFocused(); +function createItemAutocomplete(onlyCustom: boolean) { + return async (interaction: AutocompleteInteraction) => { + const focused = interaction.options.getFocused(); + + const customItems = await db('customItems') + .select('emoji', 'name', 'id') + // @ts-expect-error this LITERALLY works + .whereLike(db.raw('UPPER(name)'), `%${focused.toUpperCase()}%`) + .where('guild', interaction.guildId!) + .limit(25); + + const foundDefaultItems = defaultItems.filter(item => item.name.toUpperCase().includes(focused.toUpperCase())); + + let items; + if (onlyCustom) { + items = customItems; + } else { + items = [...foundDefaultItems, ...customItems]; + } + + await interaction.respond( + items.map(choice => ({ name: `${choice.emoji} ${choice.name}`, value: choice.id.toString() })) + ); + }; +} - const customItems = await db('customItems') - .select('emoji', 'name', 'id') - // @ts-expect-error this LITERALLY works - .whereLike(db.raw('UPPER(name)'), `%${focused.toUpperCase()}%`) - .where('guild', interaction.guildId!) - .limit(25); - - const foundDefaultItems = defaultItems.filter(item => item.name.toUpperCase().includes(focused.toUpperCase())); - - const items = [...foundDefaultItems, ...customItems]; - - await interaction.respond( - items.map(choice => ({ name: `${choice.emoji} ${choice.name}`, value: choice.id.toString() })) - ); -} \ No newline at end of file +export const itemAutocomplete = createItemAutocomplete(false); +export const customItemAutocomplete = createItemAutocomplete(true); \ No newline at end of file