economy: update the logic and add random work with different prices, add daily bonus + discount on shop and make shop per guild id

This commit is contained in:
Ayden Jahola 2024-10-26 19:33:45 +01:00
parent e12591e561
commit 81cfdd4b21
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742
8 changed files with 110 additions and 137 deletions

View file

@ -4,32 +4,38 @@ const UserEconomy = require("../../models/UserEconomy");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("balance") .setName("balance")
.setDescription("Check your balance"), .setDescription("Check your balance and claim a bonus for checking daily!"),
async execute(interaction) { async execute(interaction) {
const { user, guild } = interaction; const { user, guild } = interaction;
let userEconomy = await UserEconomy.findOne({ let userEconomy = await UserEconomy.findOne({
userId: user.id, userId: user.id,
guildId: guild.id, guildId: guild.id,
}); });
const checkInBonus = 10;
if (!userEconomy) { if (!userEconomy) {
userEconomy = await UserEconomy.create({ userEconomy = await UserEconomy.create({
userId: user.id, userId: user.id,
guildId: guild.id, guildId: guild.id,
}); });
} else {
userEconomy.balance += checkInBonus;
await userEconomy.save();
} }
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor("#0099ff") .setColor("#0099ff")
.setTitle(`${user.username}'s Balance`) .setTitle(`${user.username}'s Balance`)
.setDescription(`You have **${userEconomy.balance}** coins.`) .setDescription(
.setTimestamp() `Your balance: **${userEconomy.balance}** coins. (Check-in Bonus: +${checkInBonus} coins)`
)
.setFooter({ .setFooter({
text: `Requested by ${user.username}`, text: `Requested by ${user.username}`,
iconURL: user.displayAvatarURL(), iconURL: user.displayAvatarURL(),
}); })
.setTimestamp();
await interaction.reply({ embeds: [embed] }); await interaction.reply({ embeds: [embed] });
}, },
}; };

View file

@ -4,11 +4,13 @@ const UserEconomy = require("../../models/UserEconomy");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("daily") .setName("daily")
.setDescription("Claim your daily reward"), .setDescription("Claim your daily reward and start a streak!"),
async execute(interaction) { async execute(interaction) {
const { user, guild } = interaction; const { user, guild } = interaction;
const dailyReward = 100; const dailyBaseReward = 100;
const streakBonus = 10;
const rareItemChance = 0.1;
const oneDay = 24 * 60 * 60 * 1000; const oneDay = 24 * 60 * 60 * 1000;
let userEconomy = await UserEconomy.findOne({ let userEconomy = await UserEconomy.findOne({
@ -35,37 +37,48 @@ module.exports = {
.setColor("#ff0000") .setColor("#ff0000")
.setTitle("Daily Reward Claim") .setTitle("Daily Reward Claim")
.setDescription( .setDescription(
`You have already claimed your daily reward today. Come back in **${hours}h ${minutes}m ${seconds}s**!` `Come back in **${hours}h ${minutes}m ${seconds}s** for your next reward!`
) )
.setTimestamp()
.setFooter({ .setFooter({
text: `Requested in ${guild.name}`, text: `Requested by ${user.username}`,
iconURL: guild.iconURL() || null, iconURL: user.displayAvatarURL(),
}); });
await interaction.reply({ embeds: [errorEmbed] }); await interaction.reply({ embeds: [errorEmbed] });
return; return;
} }
userEconomy.balance += dailyReward; let reward = dailyBaseReward + userEconomy.streak * streakBonus;
userEconomy.streak += 1;
userEconomy.lastDaily = now; userEconomy.lastDaily = now;
userEconomy.balance += reward;
const rareItemEarned = Math.random() < rareItemChance;
let rareItemMessage = "";
if (rareItemEarned) {
rareItemMessage = "\nYou also found a **rare item** in your reward!";
}
await userEconomy.save(); await userEconomy.save();
const successEmbed = new EmbedBuilder() const successEmbed = new EmbedBuilder()
.setColor("#00ff00") .setColor("#00ff00")
.setTitle("Daily Reward Claimed!") .setTitle("Daily Reward Claimed!")
.setDescription( .setDescription(
`You claimed your daily reward of **${dailyReward}** coins!` `You've claimed **${reward}** coins and extended your streak!${rareItemMessage}`
) )
.addFields({ .addFields({
name: "Total Balance", name: "Streak Bonus",
value: `You now have **${userEconomy.balance}** coins.`, value: `Current streak: **${userEconomy.streak}** days`,
inline: true, inline: true,
}) })
.setTimestamp()
.setFooter({ .setFooter({
text: `Requested by ${user.username}`, text: `Requested by ${user.username}`,
iconURL: user.displayAvatarURL(), iconURL: user.displayAvatarURL(),
}); })
.setTimestamp();
await interaction.reply({ embeds: [successEmbed] }); await interaction.reply({ embeds: [successEmbed] });
}, },

View file

@ -5,11 +5,10 @@ const ShopItem = require("../../models/ShopItem");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("inventory") .setName("inventory")
.setDescription("View your inventory"), .setDescription("View your inventory with item rarity"),
async execute(interaction) { async execute(interaction) {
const { user, guild } = interaction; const { user, guild } = interaction;
const inventory = await UserInventory.find({ const inventory = await UserInventory.find({
userId: user.id, userId: user.id,
guildId: guild.id, guildId: guild.id,
@ -20,9 +19,10 @@ module.exports = {
.setColor("#ff0000") .setColor("#ff0000")
.setTitle("Inventory") .setTitle("Inventory")
.setDescription("Your inventory is empty.") .setDescription("Your inventory is empty.")
.setTimestamp()
.setFooter({ .setFooter({
text: `Requested in ${guild.name}`, text: `Requested by ${user.username}`,
iconURL: guild.iconURL() || null, iconURL: user.displayAvatarURL(),
}); });
await interaction.reply({ embeds: [emptyEmbed] }); await interaction.reply({ embeds: [emptyEmbed] });
@ -33,7 +33,7 @@ module.exports = {
inventory.map(async (item) => { inventory.map(async (item) => {
const shopItem = await ShopItem.findOne({ itemId: item.itemId }); const shopItem = await ShopItem.findOne({ itemId: item.itemId });
if (item.quantity > 0) { if (item.quantity > 0) {
return `${shopItem.name} (x${item.quantity})`; return `${shopItem.name} (x${item.quantity}) - **Rarity**: ${shopItem.rarity}`;
} }
return null; return null;
}) })
@ -41,20 +41,6 @@ module.exports = {
const filteredItemDetails = itemDetails.filter((detail) => detail !== null); const filteredItemDetails = itemDetails.filter((detail) => detail !== null);
if (filteredItemDetails.length === 0) {
const noItemsEmbed = new EmbedBuilder()
.setColor("#ff0000")
.setTitle("Inventory")
.setDescription("Your inventory is empty.")
.setFooter({
text: `Requested in ${guild.name}`,
iconURL: guild.iconURL() || null,
});
await interaction.reply({ embeds: [noItemsEmbed] });
return;
}
const inventoryEmbed = new EmbedBuilder() const inventoryEmbed = new EmbedBuilder()
.setColor("#00ff00") .setColor("#00ff00")
.setTitle(`${user.username}'s Inventory`) .setTitle(`${user.username}'s Inventory`)

View file

@ -1,108 +1,49 @@
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js"); const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const ShopItem = require("../../models/ShopItem"); const ShopItem = require("../../models/ShopItem");
const UserEconomy = require("../../models/UserEconomy");
const UserInventory = require("../../models/UserInventory");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("shop") .setName("shop")
.setDescription("View the shop and buy items") .setDescription("Browse the shop for items with rarity and discounts!"),
.addStringOption((option) =>
option.setName("item").setDescription("The ID of the item to buy")
),
async execute(interaction) { async execute(interaction) {
const { user, guild } = interaction; const { user, guild } = interaction;
const itemId = interaction.options.getString("item"); const discountChance = 0.3;
const items = await ShopItem.find({ guildId: guild.id });
if (!itemId) { if (items.length === 0) {
const items = await ShopItem.find(); const emptyEmbed = new EmbedBuilder()
const itemDescriptions = items.map(
(item) => `**${item.itemId}**: ${item.name} - **${item.price}** coins`
);
const shopEmbed = new EmbedBuilder()
.setColor("#00bfff")
.setTitle("🛒 Shop Items")
.setDescription(
itemDescriptions.length > 0
? itemDescriptions.join("\n")
: "No items available at the moment."
)
.setFooter({
text: `Requested in ${guild.name}`,
iconURL: guild.iconURL() || null,
});
await interaction.reply({ embeds: [shopEmbed] });
return;
}
const shopItem = await ShopItem.findOne({ itemId });
if (!shopItem) {
const notFoundEmbed = new EmbedBuilder()
.setColor("#ff0000") .setColor("#ff0000")
.setTitle("❌ Item Not Found") .setTitle("Shop")
.setDescription("The specified item could not be found in the shop.") .setDescription("No items in the shop currently.")
.setTimestamp()
.setFooter({ .setFooter({
text: `Requested in ${guild.name}`, text: `Requested by ${user.username}`,
iconURL: guild.iconURL() || null, iconURL: user.displayAvatarURL(),
}); });
await interaction.reply({ embeds: [notFoundEmbed] }); await interaction.reply({ embeds: [emptyEmbed] });
return; return;
} }
const userEconomy = await UserEconomy.findOne({ const shopItemsDetails = items.map((item) => {
userId: user.id, const discount = Math.random() < discountChance ? 0.8 : 1;
guildId: guild.id, const price = Math.floor(item.price * discount);
const discountText = discount < 1 ? " (Discounted!)" : "";
return `${item.name} - **${price}** coins${discountText} - Rarity: ${item.rarity}`;
}); });
if (!userEconomy || userEconomy.balance < shopItem.price) {
const insufficientFundsEmbed = new EmbedBuilder()
.setColor("#ff0000")
.setTitle("💸 Insufficient Funds")
.setDescription("You don't have enough coins to purchase this item.")
.setFooter({
text: `Requested in ${guild.name}`,
iconURL: guild.iconURL() || null,
});
await interaction.reply({ embeds: [insufficientFundsEmbed] }); const shopEmbed = new EmbedBuilder()
return;
}
userEconomy.balance -= shopItem.price;
await userEconomy.save();
let userInventory = await UserInventory.findOne({
userId: user.id,
guildId: guild.id,
itemId,
});
if (userInventory) {
userInventory.quantity += 1;
} else {
userInventory = new UserInventory({
userId: user.id,
guildId: guild.id,
itemId,
quantity: 1,
});
}
await userInventory.save();
const successEmbed = new EmbedBuilder()
.setColor("#00ff00") .setColor("#00ff00")
.setTitle("🎉 Purchase Successful") .setTitle("Shop")
.setDescription( .setDescription(shopItemsDetails.join("\n"))
`You've successfully purchased **${shopItem.name}** for **${shopItem.price}** coins!`
)
.setTimestamp() .setTimestamp()
.setFooter({ .setFooter({
text: `Requested by ${user.username}`, text: `Requested by ${user.username}`,
iconURL: user.displayAvatarURL(), iconURL: user.displayAvatarURL(),
}); });
await interaction.reply({ embeds: [successEmbed] }); await interaction.reply({ embeds: [shopEmbed] });
}, },
}; };

View file

@ -4,29 +4,35 @@ const UserEconomy = require("../../models/UserEconomy");
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName("work") .setName("work")
.setDescription("Work to earn coins!"), .setDescription("Work to earn coins and experience random events!"),
async execute(interaction) { async execute(interaction) {
const { user, guild } = interaction; const { user, guild } = interaction;
const workReward = 100; const jobs = [
const cooldownTime = 60 * 60 * 1000; { name: "Farmer", reward: 100 },
{ name: "Miner", reward: 150 },
{ name: "Chef", reward: 120 },
{ name: "Artist", reward: 130 },
];
const randomJob = jobs[Math.floor(Math.random() * jobs.length)];
const randomBonus =
Math.random() < 0.1 ? Math.floor(Math.random() * 200) : 0;
const workReward = randomJob.reward + randomBonus;
let userEconomy = await UserEconomy.findOne({ let userEconomy = await UserEconomy.findOne({
userId: user.id, userId: user.id,
guildId: guild.id, guildId: guild.id,
}); });
const cooldownTime = 60 * 60 * 1000;
if (!userEconomy) { if (!userEconomy) {
userEconomy = await UserEconomy.create({ userEconomy = await UserEconomy.create({
userId: user.id, userId: user.id,
guildId: guild.id, guildId: guild.id,
lastWork: null,
balance: 0,
}); });
} }
const now = new Date(); const now = new Date();
if (userEconomy.lastWork && now - userEconomy.lastWork < cooldownTime) { if (userEconomy.lastWork && now - userEconomy.lastWork < cooldownTime) {
const remainingTime = cooldownTime - (now - userEconomy.lastWork); const remainingTime = cooldownTime - (now - userEconomy.lastWork);
const remainingMinutes = Math.ceil(remainingTime / (60 * 1000)); const remainingMinutes = Math.ceil(remainingTime / (60 * 1000));
@ -54,12 +60,9 @@ module.exports = {
const successEmbed = new EmbedBuilder() const successEmbed = new EmbedBuilder()
.setColor("#00ff00") .setColor("#00ff00")
.setTitle("Work Success") .setTitle("Work Success")
.setDescription(`You worked hard and earned **${workReward}** coins!`) .setDescription(
.addFields({ `You worked as a **${randomJob.name}** and earned **${workReward}** coins!`
name: "Total Balance", )
value: `${userEconomy.balance} coins`,
inline: true,
})
.setTimestamp() .setTimestamp()
.setFooter({ .setFooter({
text: `Requested by ${user.username}`, text: `Requested by ${user.username}`,

View file

@ -62,11 +62,14 @@ client.once("ready", async () => {
console.log(`🤖 Logged in as ${client.user.tag}`); console.log(`🤖 Logged in as ${client.user.tag}`);
console.log(`==============================`); console.log(`==============================`);
// Seed the shop items
await seedShopItems();
// Register commands for all existing guilds // Register commands for all existing guilds
const guilds = client.guilds.cache.map((guild) => guild.id); const guilds = client.guilds.cache.map((guild) => guild.id);
// Seed the shop items
for (const guildId of guilds) {
await seedShopItems(guildId); // Pass guildId to seedShopItems
}
for (const guildId of guilds) { for (const guildId of guilds) {
await registerCommands(guildId); await registerCommands(guildId);
} }
@ -87,6 +90,9 @@ client.on("guildCreate", async (guild) => {
await ServerSettings.create({ guildId: guild.id }); await ServerSettings.create({ guildId: guild.id });
console.log(`✅ Registered new server: ${guild.name} (ID: ${guild.id})`); console.log(`✅ Registered new server: ${guild.name} (ID: ${guild.id})`);
// seed items for new guild with guildId
await seedShopItems(guild.id);
// Register slash commands for the new guild // Register slash commands for the new guild
await registerCommands(guild.id); await registerCommands(guild.id);
} catch (error) { } catch (error) {

View file

@ -1,10 +1,16 @@
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const shopItemSchema = new mongoose.Schema({ const shopItemSchema = new mongoose.Schema({
itemId: { type: String, required: true, unique: true }, itemId: { type: String, required: true },
guildId: { type: String, required: true },
name: { type: String, required: true }, name: { type: String, required: true },
price: { type: Number, required: true }, price: { type: Number, required: true },
description: { type: String, required: true }, description: { type: String, required: true },
rarity: {
type: String,
enum: ["Common", "Rare", "Epic", "Legendary"],
default: "Common",
},
}); });
module.exports = mongoose.model("ShopItem", shopItemSchema); module.exports = mongoose.model("ShopItem", shopItemSchema);

View file

@ -1,14 +1,14 @@
const ShopItem = require("../models/ShopItem"); const ShopItem = require("../models/ShopItem");
async function seedShopItems() { async function seedShopItems(guildId) {
const items = [ const items = [
// Valorant Skins
{ {
itemId: "prime_vandal", itemId: "prime_vandal",
name: "Prime Vandal", name: "Prime Vandal",
price: 1200, price: 1200,
description: description:
"A futuristic skin for the Vandal with a sleek design and special effects.", "A futuristic skin for the Vandal with a sleek design and special effects.",
rarity: "Rare",
}, },
{ {
itemId: "reaver_vandal", itemId: "reaver_vandal",
@ -16,6 +16,7 @@ async function seedShopItems() {
price: 1500, price: 1500,
description: description:
"One of the most popular Vandal skins with a haunting aesthetic and special animations.", "One of the most popular Vandal skins with a haunting aesthetic and special animations.",
rarity: "Epic",
}, },
{ {
itemId: "sovereign_ghost", itemId: "sovereign_ghost",
@ -23,6 +24,7 @@ async function seedShopItems() {
price: 800, price: 800,
description: description:
"Golden elegance for the Ghost pistol with unique sound effects.", "Golden elegance for the Ghost pistol with unique sound effects.",
rarity: "Common",
}, },
{ {
itemId: "araxys_operator", itemId: "araxys_operator",
@ -30,6 +32,7 @@ async function seedShopItems() {
price: 2000, price: 2000,
description: description:
"A top-tier sniper skin with alien-like animations and sound effects.", "A top-tier sniper skin with alien-like animations and sound effects.",
rarity: "Legendary",
}, },
{ {
itemId: "glitchpop_bulldog", itemId: "glitchpop_bulldog",
@ -37,15 +40,15 @@ async function seedShopItems() {
price: 900, price: 900,
description: description:
"A flashy skin for the Bulldog with vibrant colors and cyberpunk vibe.", "A flashy skin for the Bulldog with vibrant colors and cyberpunk vibe.",
rarity: "Rare",
}, },
// CS2 Skins
{ {
itemId: "dragon_lore_awp", itemId: "dragon_lore_awp",
name: "AWP Dragon Lore", name: "AWP Dragon Lore",
price: 2500, price: 2500,
description: description:
"A legendary skin for the AWP with dragon designs, a rare and coveted item.", "A legendary skin for the AWP with dragon designs, a rare and coveted item.",
rarity: "Legendary",
}, },
{ {
itemId: "ak47_redline", itemId: "ak47_redline",
@ -53,6 +56,7 @@ async function seedShopItems() {
price: 1000, price: 1000,
description: description:
"A simple yet iconic AK-47 skin with red and black color scheme.", "A simple yet iconic AK-47 skin with red and black color scheme.",
rarity: "Common",
}, },
{ {
itemId: "m4a4_howl", itemId: "m4a4_howl",
@ -60,6 +64,7 @@ async function seedShopItems() {
price: 2200, price: 2200,
description: description:
"A rare and valuable skin for the M4A4 with a striking wolf design.", "A rare and valuable skin for the M4A4 with a striking wolf design.",
rarity: "Epic",
}, },
{ {
itemId: "desert_eagle_kumicho_dragon", itemId: "desert_eagle_kumicho_dragon",
@ -67,6 +72,7 @@ async function seedShopItems() {
price: 800, price: 800,
description: description:
"A Desert Eagle skin with an intricate dragon design and a metallic finish.", "A Desert Eagle skin with an intricate dragon design and a metallic finish.",
rarity: "Rare",
}, },
{ {
itemId: "usp_kill_confirmed", itemId: "usp_kill_confirmed",
@ -74,14 +80,20 @@ async function seedShopItems() {
price: 1100, price: 1100,
description: description:
"A detailed skin for the USP-S with a unique comic-style design.", "A detailed skin for the USP-S with a unique comic-style design.",
rarity: "Epic",
}, },
]; ];
// Upsert each item with guildId included, so changes to price, rarity, etc., get updated
for (const item of items) { for (const item of items) {
await ShopItem.updateOne({ itemId: item.itemId }, item, { upsert: true }); await ShopItem.updateOne(
{ itemId: item.itemId, guildId },
{ ...item, guildId },
{ upsert: true }
);
} }
console.log("✅ Shop items seeded!"); console.log(`✅ Shop items seeded with rarity for guild: ${guildId}`);
} }
module.exports = seedShopItems; module.exports = seedShopItems;