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

41
commands/buy.ts Normal file
View File

@@ -0,0 +1,41 @@
import type { ChatInputCommandInteraction } from "discord.js";
import type { CommandData } from "./index";
import type { StoreItem, User } from "../db";
import { get_item, add_item_to_user, sub_balance } from "../db";
import { BotError } from "./common/error";
import { item_name_autocomplete } from "./common/autocompletes";
import { has_role } from "../util";
import config from "../config.json";
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 buy 0 or less of an item. Nice try");
const item = await get_item(name);
if (!item) throw new BotError("Item does not exist");
if (item.roles_required.length > 0) {
for (const role_id of item.roles_required) {
if (!has_role(interaction, role_id)) throw new BotError("Missing one of the required roles to buy this item");
}
}
const total_cost = item.price * quantity;
if (!(await sub_balance(user.user, total_cost))) throw new BotError("Not enough balance to buy this item");
await add_item_to_user(user.user, item.name, quantity);
return await interaction.editReply(`Bought ${quantity} of \`${name}\` for ${total_cost} ${ total_cost === 1 ? config.currency : config.currency_plural }`);
}
const data: CommandData = {
name: "buy",
description: "Buy an item from the store",
registered_only: true,
ephemeral: false,
admin_only: false,
run,
autocomplete: item_name_autocomplete, //autocompletes for the "name" option
};
export default data;