Get started with Peachy now 🍑
PEACHYPEACHY
Inventory

Sell Command

The sell command allows users to sell items or entire categories from their inventory for coins, with an interactive interface for quantity selection. This command is available to all users with sellable items.

Command Details

  • Name: sell
  • Aliases: None
  • Category: Inventory
  • Description: Sell an item or category from your inventory
  • Usage: sell <item_id | category> [amount]
  • Examples:
    • sell coal
    • sell coal 5
    • sell resources
    • sell all
  • Cooldown: 5 seconds
  • Arguments: Yes (item ID or category, optional amount)
  • Slash Command: Enabled
  • Permissions:
    • Bot: SendMessages, ViewChannel, EmbedLinks
    • User: None
  • Player Requirements: Must have sellable items in inventory

Functionality

  • Retrieves the user's inventory from the Users schema and checks for sellable items from ShopItems, ImportantItems, Woods, Minerals, SlimeCategory, and Tools.
  • Supports selling individual items by ID, entire categories (e.g., resources, card), or all sellable items (sell all).
  • For individual items, provides an interactive interface with buttons to adjust quantity (+, -, 1, Half, All), confirm, or cancel the sale.
  • For categories or all, displays a confirmation embed listing items to be sold and their total value, with Confirm/Cancel buttons.
  • Validates inputs, ensuring items are sellable, owned, and the quantity is valid (supports all, half, or numeric amounts).
  • Updates the user's balance and inventory in the database, removing sold items and unequipping them if necessary.
  • Sends localized success, cancellation, or error messages, with a 1-minute timeout for interactions.
  • Handles errors such as empty inventory, invalid items, or non-sellable items with appropriate messages.

Code Overview

The command:

  • Extends the Command class from ../../structures/index.js.
  • Imports Users schema and inventory data (ShopItems, ImportantItems, Woods, Minerals, SlimeCategory, Tools).
  • Filters sellable items (sell price > 0) into AllItems for processing.
  • Uses the run method to handle both message and slash command inputs, routing to sellAllItems, sellSpecificCategory, or showSellInterface based on input.
  • Implements showSellInterface for individual item sales with interactive buttons for quantity adjustment and confirmation.
  • Implements sellAllItems and sellSpecificCategory for bulk sales, with confirmation embeds and buttons.
  • Uses parseQuantity to handle flexible quantity inputs (all, half, numbers).
  • Updates the database with Users.updateOne or Users.findOneAndUpdate to adjust coins and inventory.
  • Uses a MessageComponentCollector for button interactions, disabling components on timeout (1 minute).
  • Logs errors and sends localized error messages for issues like database failures or invalid inputs.