const { SlashCommandBuilder } = require('@discordjs/builders'); const { MessageEmbed } = require('discord.js'); const mongoose = require("mongoose"); const UserSave = require('../models/UserSchema'); const GuildSave = require('../models/GuildSchema'); const rs = require('randomstring'); module.exports = { data: new SlashCommandBuilder() .setName('buy') .setDescription('Buy items from the shop') .addStringOption(i => i.setName("id").setDescription("The ID of the item").setRequired(true)), async execute(interaction) { let ID = interaction.options.getString("id"); let UserProfile = await UserSave.findOne({ userID: interaction.user.id, guildID: interaction.guild.id }); if(!UserProfile) { UserProfile = await new UserSave({ _id: mongoose.Types.ObjectId(), userID: interaction.user.id, guildID: interaction.guild.id, bank: 0, wallet: 0, }).save(); }; let GuildProfile = await GuildSave.findOne({ guildID: interaction.guild.id }); if(!GuildProfile) { GuildProfile = await new GuildSave({ _id: mongoose.Types.ObjectId(), guildID: interaction.guild.id, bank: 50000, shop: [], }).save(); }; function FindItem(item) { return item.id === ID; } let Item = await GuildProfile.shop.find(FindItem) let embed = new MessageEmbed() .setTitle("Something went wrong!") .addField("Possible reasons:", `- You don't have enough money\n- You're trying to buy an item that doesn't exist\n__**The money must be in your wallet**__`) .setColor("RED") .setFooter("Disbank", interaction.client.user.displayAvatarURL({ dynamic: true })) .setTimestamp(); if(!Item || UserProfile.wallet < Item.price) { await interaction.reply({ embeds: [embed], ephemeral: true }); return; } let embed2 = new MessageEmbed() .setTitle("Item bought!") .addField("Item:", `${Item.item.charAt(0).toUpperCase() + Item.item.slice(1)}`) .addField("Price:", `\`❂ ${Item.price}\``) .setColor('#EFD08C') .setFooter("Disbank", interaction.client.user.displayAvatarURL({ dynamic: true })) .setTimestamp(); let itemID = await rs.generate({ length: 4, charset: 'numeric' }); let object = { item: Item.item, id: itemID, count: 1, } let Inventory = UserProfile.inventory; let ItemCount = await Inventory.find(x => x.item === Item.item) if(ItemCount) { await UserSave.findOneAndUpdate({ userID: interaction.user.id, guildID: interaction.guild.id }, { wallet: UserProfile.wallet - Item.price }); await GuildSave.findOneAndUpdate({ guildID: interaction.guild.id }, { bank: GuildProfile.bank - Item.price }); await UserSave.findOneAndUpdate({ userID: interaction.user.id, guildID: interaction.guild.id }, { $inc: { 'inventory.$[findId].count': 1 } }, { arrayFilters: [{ 'findId.id': ItemCount.id }] }); await interaction.reply({ embeds: [embed2] }); return; } else { await UserSave.findOneAndUpdate({ userID: interaction.user.id, guildID: interaction.guild.id }, { wallet: UserProfile.wallet - Item.price }); await GuildSave.findOneAndUpdate({ guildID: interaction.guild.id }, { bank: GuildProfile.bank - Item.price }); await UserSave.findOneAndUpdate({ userID: interaction.user.id, guildID: interaction.guild.id }, { $push: { inventory: object } }); await interaction.reply({ embeds: [embed2] }); return; } }, };