buy, use, delete, edit item

This commit is contained in:
stjet
2024-08-16 04:38:24 +00:00
parent 38c2700c2f
commit 3145f52df7
9 changed files with 258 additions and 5 deletions

34
commands/use_item.ts Normal file
View File

@@ -0,0 +1,34 @@
import type { ChatInputCommandInteraction } from "discord.js";
import type { CommandData } from "./index";
import type { StoreItem, User } from "../db";
import { get_item, sub_item_to_user } from "../db";
import { BotError } from "./common/error";
import { item_name_autocomplete } from "./common/autocompletes";
async function run(interaction: ChatInputCommandInteraction, user: User) {
const options = interaction.options;
const name: string = (await options.get("name")).value as string;
const quantity: number = (await options.get("quantity")).value as number;
if (quantity <= 0) throw new BotError("Can't use 0 or less of an item");
const item = await get_item(name);
if (!item) throw new BotError("Item does not exist");
if (!item.usable) throw new BotError("That item is not usable");
if (!(await sub_item_to_user(user.user, name, quantity))) throw new BotError("You did not have enough of that item to use");
return await interaction.editReply(`Used ${quantity} of \`${name}\``);
}
const data: CommandData = {
name: "use_item",
description: "Use an item (subtracts from your items)",
registered_only: true,
ephemeral: false,
admin_only: false,
run,
autocomplete: item_name_autocomplete, //autocompletes for the "name" option
};
export default data;