fixes i would've caught if i tested this for more than 2 seconds

This commit is contained in:
Jill 2023-11-18 17:12:27 +03:00
parent 26903e03a8
commit 37af0ea68f
Signed by: oat
GPG Key ID: 33489AA58A955108
3 changed files with 49 additions and 38 deletions

View File

@ -1,6 +1,6 @@
import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js'; import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { CustomCraftingRecipeItem, CustomItem, db } from '../lib/db'; 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 { behaviors } from '../lib/rpg/behaviors';
import { Command } from '../types/index'; import { Command } from '../types/index';
import { formatRecipe, getCustomRecipe } from '../lib/rpg/recipes'; import { formatRecipe, getCustomRecipe } from '../lib/rpg/recipes';
@ -177,7 +177,7 @@ export default {
.setDescription('[ADMIN] Delete a custom item') .setDescription('[ADMIN] Delete a custom item')
.addStringOption(opt => .addStringOption(opt =>
opt opt
.setName('item') .setName('customitem')
.setDescription('The item') .setDescription('The item')
.setAutocomplete(true) .setAutocomplete(true)
.setRequired(true) .setRequired(true)
@ -223,7 +223,7 @@ export default {
await interaction.followUp(`${user.toString()} now has ${formatItems(item, inv.quantity)}.`); await interaction.followUp(`${user.toString()} now has ${formatItems(item, inv.quantity)}.`);
} else if (subcommand === 'delete') { } 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); const item = await getItem(itemID);
if (!item) return interaction.followUp('No such item exists!'); if (!item) return interaction.followUp('No such item exists!');
@ -250,6 +250,8 @@ export default {
if (focused.name === 'item') { if (focused.name === 'item') {
return itemAutocomplete(interaction); return itemAutocomplete(interaction);
} else if (focused.name === 'customitem') {
return customItemAutocomplete(interaction);
} }
} }
} satisfies Command; } satisfies Command;

View File

@ -98,20 +98,22 @@ export default {
parsed = await Promise.all( parsed = await Promise.all(
recipeString recipeString
.split('|') .split('|')
.map(items => .map(async items =>
Promise.all( items === '' ?
items [] :
.split(';') await Promise.all(
.map(itemStack => items
itemStack.split(',') .split(';')
) .map(itemStack =>
.map(async ([itemID, quantity]) => ( itemStack.split(',')
{ )
item: (await getItem(parseInt(itemID)))!, .map(async ([itemID, quantity]) => (
quantity: parseInt(quantity) {
} item: (await getItem(parseInt(itemID)))!,
)) quantity: parseInt(quantity)
) }
))
)
) )
) as Items[][]; ) as Items[][];
} catch (err) { } catch (err) {
@ -237,10 +239,7 @@ export default {
const focused = interaction.options.getFocused(true); const focused = interaction.options.getFocused(true);
if (focused.name === 'recipe') { if (focused.name === 'recipe') {
const station = interaction.options.getString('station'); const customRecipes = await db<CustomCraftingRecipe>('customCraftingRecipes');
const customRecipes = await db<CustomCraftingRecipe>('customCraftingRecipes')
.where('station', station);
const resolvedCustomRecipes = await Promise.all(customRecipes.map(resolveCustomRecipe)); const resolvedCustomRecipes = await Promise.all(customRecipes.map(resolveCustomRecipe));

View File

@ -251,21 +251,31 @@ export function formatItemsArray(items: Items[], disableBold = false) {
return items.map(i => formatItems(i.item, i.quantity, disableBold)).join(' '); return items.map(i => formatItems(i.item, i.quantity, disableBold)).join(' ');
} }
export async function itemAutocomplete(interaction: AutocompleteInteraction) { function createItemAutocomplete(onlyCustom: boolean) {
const focused = interaction.options.getFocused(); return async (interaction: AutocompleteInteraction) => {
const focused = interaction.options.getFocused();
const customItems = await db<CustomItem>('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<CustomItem>('customItems') export const itemAutocomplete = createItemAutocomplete(false);
.select('emoji', 'name', 'id') export const customItemAutocomplete = createItemAutocomplete(true);
// @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() }))
);
}