delete items

This commit is contained in:
Jill 2023-11-18 16:03:52 +03:00
parent fd3dbfa65b
commit 7d3bf20eaa
Signed by: oat
GPG Key ID: 33489AA58A955108
1 changed files with 34 additions and 2 deletions

View File

@ -1,8 +1,9 @@
import { AutocompleteInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js';
import { CustomItem, db } from '../lib/db';
import { formatItems, getItem, giveItem, itemAutocomplete } from '../lib/rpg/items';
import { CustomCraftingRecipeItem, CustomItem, db } from '../lib/db';
import { 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';
//function extendOption(t: string) {
// return {name: t, value: t};
@ -170,6 +171,18 @@ export default {
.setDescription('Amount of items to give')
)
)
.addSubcommand(cmd =>
cmd
.setName('delete')
.setDescription('[ADMIN] Delete a custom item')
.addStringOption(opt =>
opt
.setName('item')
.setDescription('The item')
.setAutocomplete(true)
.setRequired(true)
)
)
.setDefaultMemberPermissions('0')
.setDMPermission(false),
@ -209,6 +222,25 @@ export default {
const inv = await giveItem(user.id, item, quantity);
await interaction.followUp(`${user.toString()} now has ${formatItems(item, inv.quantity)}.`);
} else if (subcommand === 'delete') {
const itemID = parseInt(interaction.options.getString('item', true));
const item = await getItem(itemID);
if (!item) return interaction.followUp('No such item exists!');
const usedIn = await db<CustomCraftingRecipeItem>('customCraftingRecipeItems')
.where('item', item.id);
if (usedIn.length > 0) {
const recipes = (await Promise.all(usedIn.map(i => getCustomRecipe(i.id)))).filter(r => r !== undefined);
return interaction.followUp(`⚠️ This item is used in the following recipes:\n${recipes.map(r => `- ${formatRecipe(r!)}`).join('\n')}`);
}
await db<CustomItem>('customItems')
.where('id', item.id)
.delete();
interaction.followUp(`${formatItem(item)} has been deleted.`);
}
}
},