Guest User

Untitled

a guest
Aug 21st, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. const { SlashCommandBuilder } = require('discord.js');
  2. const Inventory = require('../../schemas/inventory-schema');
  3. // eslint-disable-next-line no-unused-vars
  4. const Item = require('../../schemas/item-schema');
  5.  
  6. module.exports = {
  7. data: new SlashCommandBuilder()
  8. .setName('useitem')
  9. .setDescription('Use an item from your character\'s inventory.')
  10. .addStringOption(option =>
  11. option.setName('name')
  12. .setDescription('The name of the item')
  13. .setRequired(true)),
  14. async execute(interaction) {
  15. const itemName = interaction.options.getString('name');
  16. const inventory = await Inventory.findOne({ player: interaction.user.id });
  17.  
  18. console.log('Inventory Items:', inventory.items);
  19. console.log('Item name:', itemName);
  20. const inventoryItem = await Inventory.findOne({ player: interaction.user.id, 'items.item.name': itemName });
  21. console.log('Inventory Item:', inventoryItem);
  22.  
  23.  
  24. if (!inventoryItem) {
  25. return interaction.reply(`You don't have ${itemName} in your inventory.`);
  26. }
  27.  
  28. if (inventoryItem.quantity === 0) {
  29. return interaction.reply(`You don't have any ${itemName} left.`);
  30. }
  31.  
  32. inventoryItem.quantity--;
  33.  
  34. if (inventoryItem.quantity === 0) {
  35. inventory.items.pull(inventoryItem._id);
  36. }
  37.  
  38. await inventory.save();
  39.  
  40. return interaction.reply(`You used ${itemName}.`);
  41.  
  42.  
  43. },
  44. };
Advertisement
Add Comment
Please, Sign In to add comment