Get started with Peachy now 🍑
PEACHYPEACHY
Inventory

Give Item Command

The giveitem command allows users to transfer items from their inventory to another user. This command is available to all users with sufficient items.

Command Details

  • Name: giveitem
  • Aliases: gi
  • Category: Inventory
  • Description: Give your item to another user
  • Usage: giveitem <user> <item> <amount> or gi <user> <item> <amount>
  • Examples:
    • giveitem @user gem 1
    • gi @user gem 5
    • giveitem @user gem all
  • Cooldown: 5 seconds
  • Arguments: Yes (user, item ID, amount)
  • Slash Command: Enabled
  • Permissions:
    • Bot: SendMessages, ViewChannel, EmbedLinks
    • User: None
  • Player Requirements: Must have the specified item in inventory

Functionality

  • Retrieves the user's inventory from the Users schema to verify they have the item.
  • Validates the target user (not a bot, not the same as the author) and the item ID against ShopItems and ImportantItems.
  • Checks if the item is giftable and if the user has enough of it.
  • Supports flexible amounts (e.g., all, half, or a specific number).
  • Sends a confirmation embed with Accept/Cancel buttons for the transaction.
  • Updates the database by removing or reducing the item from the giver's inventory and adding it to the receiver's inventory upon acceptance.
  • Handles errors such as empty inventory, invalid users, non-giftable items, or invalid amounts with localized error messages.
  • Disables buttons after the transaction or timeout (120 seconds).

Code Overview

The command:

  • Extends the Command class from ../../structures/index.js.
  • Imports Users schema, ShopItems, and ImportantItems for item data.
  • Uses the run method to handle both message and slash command inputs.
  • Validates the target user, item existence, giftability, and amount.
  • Creates a confirmation embed with Discord buttons (ActionRowBuilder, ButtonBuilder) for Accept/Cancel.
  • Uses a MessageComponentCollector to handle button interactions, updating the database (Users.updateOne) on acceptance.
  • Removes buttons after completion or timeout and sends localized success or cancellation messages.
  • Catches and logs errors, sending a generic error message to the user.