2024-10-26 03:40:13 +01:00
|
|
|
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
|
|
|
|
const ShopItem = require("../../models/ShopItem");
|
2024-10-26 20:06:20 +01:00
|
|
|
const UserEconomy = require("../../models/UserEconomy");
|
|
|
|
const UserInventory = require("../../models/UserInventory");
|
2024-10-26 03:40:13 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName("shop")
|
2024-10-26 20:06:20 +01:00
|
|
|
.setDescription("Browse the shop for items with rarity and discounts!")
|
|
|
|
.addStringOption((option) =>
|
|
|
|
option
|
|
|
|
.setName("item")
|
|
|
|
.setDescription("The item you want to buy (use item name)")
|
|
|
|
.setRequired(false)
|
|
|
|
),
|
2024-10-26 03:40:13 +01:00
|
|
|
|
|
|
|
async execute(interaction) {
|
|
|
|
const { user, guild } = interaction;
|
2024-10-26 19:33:45 +01:00
|
|
|
const discountChance = 0.3;
|
2024-10-26 20:06:20 +01:00
|
|
|
const itemName = interaction.options.getString("item");
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
const items = await ShopItem.find({ guildId: guild.id });
|
2024-10-26 03:40:13 +01:00
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
if (items.length === 0) {
|
|
|
|
const emptyEmbed = new EmbedBuilder()
|
2024-10-26 03:40:13 +01:00
|
|
|
.setColor("#ff0000")
|
2024-10-26 20:06:20 +01:00
|
|
|
.setTitle("🛒 Shop Items")
|
2024-10-26 19:33:45 +01:00
|
|
|
.setDescription("No items in the shop currently.")
|
|
|
|
.setTimestamp()
|
2024-10-26 03:40:13 +01:00
|
|
|
.setFooter({
|
2024-10-26 19:33:45 +01:00
|
|
|
text: `Requested by ${user.username}`,
|
|
|
|
iconURL: user.displayAvatarURL(),
|
2024-10-26 03:40:13 +01:00
|
|
|
});
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
await interaction.reply({ embeds: [emptyEmbed] });
|
2024-10-26 03:40:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-26 20:06:20 +01:00
|
|
|
if (itemName) {
|
|
|
|
const item = items.find(
|
|
|
|
(item) => item.name.toLowerCase() === itemName.toLowerCase()
|
|
|
|
);
|
|
|
|
if (!item) {
|
|
|
|
await interaction.reply({
|
|
|
|
content: "Item not found in the shop!",
|
|
|
|
ephemeral: false,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let userEconomy = await UserEconomy.findOne({
|
|
|
|
userId: user.id,
|
|
|
|
guildId: guild.id,
|
|
|
|
});
|
|
|
|
if (!userEconomy) {
|
|
|
|
userEconomy = await UserEconomy.create({
|
|
|
|
userId: user.id,
|
|
|
|
guildId: guild.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const discount = Math.random() < discountChance ? 0.8 : 1;
|
|
|
|
const price = Math.floor(item.price * discount);
|
|
|
|
|
|
|
|
if (userEconomy.balance < price) {
|
|
|
|
const insufficientFundsEmbed = new EmbedBuilder()
|
|
|
|
.setColor("#ff0000")
|
|
|
|
.setTitle("💸 Insufficient Funds")
|
|
|
|
.setDescription(
|
|
|
|
`You don't have enough coins to buy **${item.name}**!`
|
|
|
|
)
|
|
|
|
.addFields(
|
|
|
|
{
|
|
|
|
name: "Your Balance",
|
|
|
|
value: `${userEconomy.balance} coins`,
|
|
|
|
inline: true,
|
|
|
|
},
|
|
|
|
{ name: "Item Price", value: `${price} coins`, inline: true }
|
|
|
|
)
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter({
|
|
|
|
text: `Requested by ${user.username}`,
|
|
|
|
iconURL: user.displayAvatarURL(),
|
|
|
|
});
|
|
|
|
|
|
|
|
await interaction.reply({
|
|
|
|
embeds: [insufficientFundsEmbed],
|
|
|
|
ephemeral: false,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
userEconomy.balance -= price;
|
|
|
|
await userEconomy.save();
|
|
|
|
|
|
|
|
const userInventory = await UserInventory.findOne({
|
|
|
|
userId: user.id,
|
|
|
|
guildId: guild.id,
|
|
|
|
itemId: item.itemId,
|
|
|
|
});
|
|
|
|
if (userInventory) {
|
|
|
|
userInventory.quantity += 1;
|
|
|
|
await userInventory.save();
|
|
|
|
} else {
|
|
|
|
await UserInventory.create({
|
|
|
|
userId: user.id,
|
|
|
|
guildId: guild.id,
|
|
|
|
itemId: item.itemId,
|
|
|
|
quantity: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const successEmbed = new EmbedBuilder()
|
|
|
|
.setColor("#00ff00")
|
|
|
|
.setTitle("🎉 Purchase Successful")
|
|
|
|
.setDescription(`You bought **${item.name}** for **${price}** coins!`)
|
|
|
|
.setTimestamp()
|
|
|
|
.setFooter({
|
|
|
|
text: `Requested by ${user.username}`,
|
|
|
|
iconURL: user.displayAvatarURL(),
|
|
|
|
});
|
|
|
|
|
|
|
|
await interaction.reply({ embeds: [successEmbed], ephemeral: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
const shopItemsDetails = items.map((item) => {
|
|
|
|
const discount = Math.random() < discountChance ? 0.8 : 1;
|
|
|
|
const price = Math.floor(item.price * discount);
|
|
|
|
const discountText = discount < 1 ? " (Discounted!)" : "";
|
2024-10-26 03:40:13 +01:00
|
|
|
|
2024-10-26 19:42:52 +01:00
|
|
|
return `${item.name} - **${price}** coins${discountText} - Rarity: ${
|
|
|
|
item.rarity
|
|
|
|
} - Type: ${item.type} - Category: ${item.category || "N/A"}`;
|
2024-10-26 03:40:13 +01:00
|
|
|
});
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
const shopEmbed = new EmbedBuilder()
|
2024-10-26 03:40:13 +01:00
|
|
|
.setColor("#00ff00")
|
2024-10-26 20:06:20 +01:00
|
|
|
.setTitle("🛒 Shop Items")
|
2024-10-26 19:33:45 +01:00
|
|
|
.setDescription(shopItemsDetails.join("\n"))
|
2024-10-26 03:40:13 +01:00
|
|
|
.setTimestamp()
|
|
|
|
.setFooter({
|
|
|
|
text: `Requested by ${user.username}`,
|
|
|
|
iconURL: user.displayAvatarURL(),
|
|
|
|
});
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
await interaction.reply({ embeds: [shopEmbed] });
|
2024-10-26 03:40:13 +01:00
|
|
|
},
|
|
|
|
};
|