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 { 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;

View File

@ -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<CustomCraftingRecipe>('customCraftingRecipes')
.where('station', station);
const customRecipes = await db<CustomCraftingRecipe>('customCraftingRecipes');
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(' ');
}
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<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')
.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() }))
);
}
export const itemAutocomplete = createItemAutocomplete(false);
export const customItemAutocomplete = createItemAutocomplete(true);