Skip to content

Commit

Permalink
Merge pull request #35 from clienterrverse/GrishMahat-patch-1
Browse files Browse the repository at this point in the history
Update economy.js
  • Loading branch information
GrishMahat authored Jun 2, 2024
2 parents 019c835 + 3045e2a commit 852521a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/commands/economy/daliy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { SlashCommandBuilder, EmbedBuilder } from 'discord.js';
import { Balance } from '../../schemas/economy.js';

export default {
data: new SlashCommandBuilder()
.setName("daily")
.setDescription("Claim your daily reward.")
.toJSON(),
userPermissions: [],
botPermissions: [],
cooldown: 5,
nwfwMode: false,
testMode: false,
devOnly: false,

run: async (client, interaction) => {
try {
const userId = interaction.user.id;
const emoji = '🎁'; // Using a gift emoji for the daily reward
const minAmount = 1; // Minimum amount to be received
const maxAmount = 30; // Maximum amount to be received
const dailyCooldown = 24 * 60 * 60 * 1000; // 24 hours in milliseconds

// Fetch the user's balance from the database
let userBalance = await Balance.findOne({ userId });

// If the user does not exist in the database, create a new entry
if (!userBalance) {
userBalance = new Balance({ userId });
}

// Check if the user has already claimed their daily reward
const now = Date.now();
if (userBalance.lastDaily && (now - new Date(userBalance.lastDaily).getTime()) < dailyCooldown) {
const timeLeft = dailyCooldown - (now - new Date(userBalance.lastDaily).getTime());
const hours = Math.floor(timeLeft / (1000 * 60 * 60));
const minutes = Math.floor((timeLeft % (1000 * 60 * 60)) / (1000 * 60));

const embed = new EmbedBuilder()
.setColor(0xFF0000)
.setTitle('Daily Reward')
.setDescription(`You have already claimed your daily reward. Please try again in ${hours} hours and ${minutes} minutes.`);

return interaction.reply({ embeds: [embed], ephemeral: true });
}

// Generate a random amount for the daily reward
const amount = Math.floor(Math.random() * (maxAmount - minAmount + 1)) + minAmount;

// Update the user's balance and last daily claim time
userBalance.balance += amount;
userBalance.lastDaily = new Date();
await userBalance.save();

// Create an embed for the response
const embed = new EmbedBuilder()
.setColor(0x00FF00)
.setTitle('Daily Reward')
.setDescription(`${emoji} You have claimed your daily reward of ${amount} coins!`)
.addFields(
{ name: 'New Balance', value: `${userBalance.balance} coins`, inline: true },
);

// Reply with the embed
await interaction.reply({ embeds: [embed] });
} catch (error) {
console.error('Error processing daily command:', error);
const embed = new EmbedBuilder()
.setColor(0xFF0000)
.setTitle('Error')
.setDescription('There was an error trying to process your daily reward.');

await interaction.reply({ embeds: [embed], ephemeral: true });
}
},
};
5 changes: 1 addition & 4 deletions src/schemas/economy.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/** @format */

import mongoose from 'mongoose';

// Schema for user balances
const balanceSchema = new mongoose.Schema({
userId: { type: String, required: true, unique: true, index: true },
balance: { type: Number, default: 0 },
bank: { type: Number, default: 0 },
lastDaily: { type: Date, default: null },
lastWeekly: { type: Date, default: null },
lastHourly: { type: Date, default: null },
lastDaily: { type: Date, default: null },
lastBeg: { type: Date, default: null },
lastcoin: { type: Date, default: null },
lastWork: { type: Date, default: null },
Expand Down

0 comments on commit 852521a

Please sign in to comment.