Get started with Peachy now 🍑
PEACHYPEACHY
Inventory

Buy Command

The buy command allows users to purchase items from the shop using their in-game coins. This command is available to all users.

Command Details

  • Name: buy
  • Aliases: None
  • Category: Inventory
  • Description: Buy an item from the shop
  • Usage: buy <itemId> [amount]
  • Examples:
    • buy f01
    • buy f01 5
    • buy f01 all
  • Cooldown: 5 seconds
  • Arguments: Yes (item ID, optional amount)
  • Slash Command: Disabled
  • Permissions:
    • Bot: SendMessages, ViewChannel, EmbedLinks
    • User: None
  • Player Requirements: Must have sufficient coins

Functionality

  • Retrieves the user's balance and inventory from the Users schema.
  • Validates the provided item ID against the shop's inventory (ShopItems).
  • Checks if the user has enough coins to purchase the item.
  • Supports buying multiple quantities of an item, with options like all or half for the amount.
  • Prevents purchasing items with a buy price of 0 or items already owned (e.g., unique rings).
  • Updates the user's inventory and deducts the total cost from their coin balance.
  • Sends a confirmation embed with purchase details, including item name, quantity, and total price.
  • Handles errors such as invalid item IDs, insufficient coins, or invalid amounts with localized error messages.

Code Overview

The command:

  • Extends the Command class from ../../structures/index.js.
  • Imports the Users schema and ShopItems inventory data.
  • Filters and sorts items by buy price to create a purchasable item list.
  • Uses the run method to handle command execution, supporting both message and interaction inputs.
  • Validates user balance, item existence, and purchase eligibility (e.g., not owning a unique ring).
  • Calculates the maximum affordable quantity and processes the purchase amount (e.g., all, half, or a number).
  • Updates the database using Users.updateOne to adjust coins and inventory.
  • Sends a success embed with localized messages and purchase details.