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")
|
2024-10-26 19:33:45 +01:00
|
|
|
.setDescription("Claim your daily reward and start a streak!"),
|
2024-10-26 03:40:13 +01:00
|
|
|
|
|
|
|
async execute(interaction) {
|
|
|
|
const { user, guild } = interaction;
|
2024-10-26 19:33:45 +01:00
|
|
|
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,
|
2024-10-26 20:06:20 +01:00
|
|
|
streak: 0,
|
|
|
|
balance: 0,
|
2024-10-26 03:40:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const now = new Date();
|
2024-10-26 19:42:52 +01:00
|
|
|
|
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(
|
2024-10-26 19:33:45 +01:00
|
|
|
`Come back in **${hours}h ${minutes}m ${seconds}s** for your next reward!`
|
2024-10-26 03:40:13 +01:00
|
|
|
)
|
2024-10-26 19:33:45 +01:00
|
|
|
.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
|
|
|
});
|
|
|
|
|
|
|
|
await interaction.reply({ embeds: [errorEmbed] });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-26 19:42:52 +01:00
|
|
|
if (userEconomy.lastDaily && now - userEconomy.lastDaily >= oneDay) {
|
|
|
|
userEconomy.streak = 0;
|
|
|
|
}
|
|
|
|
|
2024-10-26 19:33:45 +01:00
|
|
|
userEconomy.streak += 1;
|
2024-10-26 20:06:20 +01:00
|
|
|
const reward = dailyBaseReward + userEconomy.streak * streakBonus;
|
2024-10-26 03:40:13 +01:00
|
|
|
userEconomy.lastDaily = now;
|
2024-10-26 19:33:45 +01:00
|
|
|
userEconomy.balance += reward;
|
|
|
|
|
|
|
|
const rareItemEarned = Math.random() < rareItemChance;
|
2024-10-26 20:06:20 +01:00
|
|
|
const rareItemMessage = rareItemEarned
|
|
|
|
? "\nYou also found a **rare item** in your reward!"
|
|
|
|
: "";
|
2024-10-26 19:33:45 +01:00
|
|
|
|
2024-10-26 03:40:13 +01:00
|
|
|
await userEconomy.save();
|
|
|
|
|
|
|
|
const successEmbed = new EmbedBuilder()
|
|
|
|
.setColor("#00ff00")
|
|
|
|
.setTitle("Daily Reward Claimed!")
|
|
|
|
.setDescription(
|
2024-10-26 19:33:45 +01:00
|
|
|
`You've claimed **${reward}** coins and extended your streak!${rareItemMessage}`
|
2024-10-26 03:40:13 +01:00
|
|
|
)
|
|
|
|
.addFields({
|
2024-10-26 19:33:45 +01:00
|
|
|
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(),
|
2024-10-26 19:33:45 +01:00
|
|
|
})
|
|
|
|
.setTimestamp();
|
2024-10-26 03:40:13 +01:00
|
|
|
|
|
|
|
await interaction.reply({ embeds: [successEmbed] });
|
|
|
|
},
|
|
|
|
};
|