discord-multipurpose-bot/commands/economy/daily.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-10-26 03:40:13 +01:00
const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");
const UserEconomy = require("../../models/UserEconomy");
module.exports = {
data: new SlashCommandBuilder()
.setName("daily")
.setDescription("Claim your daily reward and start a streak!"),
2024-10-26 03:40:13 +01:00
async execute(interaction) {
const { user, guild } = interaction;
const dailyBaseReward = 100;
const streakBonus = 10;
const rareItemChance = 0.1;
2024-10-26 03:40:13 +01:00
const oneDay = 24 * 60 * 60 * 1000;
let userEconomy = await UserEconomy.findOne({
userId: user.id,
guildId: guild.id,
});
if (!userEconomy) {
userEconomy = await UserEconomy.create({
userId: user.id,
guildId: guild.id,
streak: 0, // Initialize streak
2024-10-26 03:40:13 +01:00
});
}
const now = new Date();
2024-10-26 03:40:13 +01:00
if (userEconomy.lastDaily && now - userEconomy.lastDaily < oneDay) {
const remainingTime =
new Date(userEconomy.lastDaily.getTime() + oneDay) - now;
const hours = Math.floor((remainingTime / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((remainingTime / (1000 * 60)) % 60);
const seconds = Math.floor((remainingTime / 1000) % 60);
const errorEmbed = new EmbedBuilder()
.setColor("#ff0000")
.setTitle("Daily Reward Claim")
.setDescription(
`Come back in **${hours}h ${minutes}m ${seconds}s** for your next reward!`
2024-10-26 03:40:13 +01:00
)
.setTimestamp()
2024-10-26 03:40:13 +01:00
.setFooter({
text: `Requested by ${user.username}`,
iconURL: user.displayAvatarURL(),
2024-10-26 03:40:13 +01:00
});
await interaction.reply({ embeds: [errorEmbed] });
return;
}
if (userEconomy.lastDaily && now - userEconomy.lastDaily >= oneDay) {
userEconomy.streak = 0;
}
userEconomy.streak += 1;
let reward = dailyBaseReward + userEconomy.streak * streakBonus;
2024-10-26 03:40:13 +01:00
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!";
}
2024-10-26 03:40:13 +01:00
await userEconomy.save();
const successEmbed = new EmbedBuilder()
.setColor("#00ff00")
.setTitle("Daily Reward Claimed!")
.setDescription(
`You've claimed **${reward}** coins and extended your streak!${rareItemMessage}`
2024-10-26 03:40:13 +01:00
)
.addFields({
name: "Streak Bonus",
value: `Current streak: **${userEconomy.streak}** days`,
2024-10-26 03:40:13 +01:00
inline: true,
})
.setFooter({
text: `Requested by ${user.username}`,
iconURL: user.displayAvatarURL(),
})
.setTimestamp();
2024-10-26 03:40:13 +01:00
await interaction.reply({ embeds: [successEmbed] });
},
};