delete recipes

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

View File

@ -1,7 +1,7 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, ComponentType, Events, ModalBuilder, SlashCommandBuilder, StringSelectMenuBuilder, TextInputBuilder, TextInputStyle } from 'discord.js';
import { Command } from '../types/index';
import { Items, getItem } from '../lib/rpg/items';
import { formatRecipe } from '../lib/rpg/recipes';
import { formatRecipe, getCustomRecipe, resolveCustomRecipe } from '../lib/rpg/recipes';
import { craftingStations, getStation } from '../lib/rpg/craftingStations';
import { CustomCraftingRecipe, CustomCraftingRecipeItem, db } from '../lib/db';
@ -14,6 +14,18 @@ export default {
.setName('create')
.setDescription('[ADMIN] Create a custom recipe')
)
.addSubcommand(sub =>
sub
.setName('delete')
.setDescription('[ADMIN] Delete a custom recipe')
.addStringOption(opt =>
opt
.setName('recipe')
.setAutocomplete(true)
.setDescription('Which recipe to remove')
.setRequired(true)
)
)
.setDMPermission(false)
.setDefaultMemberPermissions(0),
@ -33,6 +45,21 @@ export default {
content: `To create a recipe, go here: ${interaction.client.config.siteURL}/create-recipe/?guild=${interaction.guildId}\nOnce done, click the button below and paste the resulting string in.`,
components: [row]
});
} else if (sub === 'delete') {
const recipeID = interaction.options.getString('recipe', true);
const recipe = await getCustomRecipe(parseInt(recipeID));
if (!recipe) return interaction.followUp('Recipe does no exist!');
await db<CustomCraftingRecipe>('customCraftingRecipes')
.where('id', recipe.id)
.delete();
await db<CustomCraftingRecipeItem>('customCraftingRecipeItems')
.where('id', recipe.id)
.delete();
await interaction.followUp(`Deleted recipe ${formatRecipe(recipe)}`);
}
},
@ -204,5 +231,29 @@ export default {
}
}
});
},
autocomplete: async (interaction) => {
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 resolvedCustomRecipes = await Promise.all(customRecipes.map(resolveCustomRecipe));
const foundCustomRecipes = resolvedCustomRecipes
.filter(recipe => recipe.outputs.filter(n => n.item.name.toLowerCase().includes(focused.value.toLowerCase())).length > 0);
return interaction.respond(
foundCustomRecipes
.map(recipe => ({
name: formatRecipe(recipe, true),
value: recipe.id.toString()
}))
);
}
}
} satisfies Command;