Merge pull request 'Added Balance, Pay and Daily Commands' (#3) from HotPlate/discord-rpg-concept:main into main
Reviewed-on: syntaxbullet/discord-rpg-concept#3
This commit was merged in pull request #3.
This commit is contained in:
@@ -1,13 +1,29 @@
|
|||||||
import { createCommand } from "@lib/utils";
|
import { createCommand } from "@lib/utils";
|
||||||
import { getUserBalance } from "@/modules/economy/economy.service";
|
import { getUserBalance } from "@/modules/economy/economy.service";
|
||||||
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
|
import { createUser, getUserById } from "@/modules/users/users.service";
|
||||||
|
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js";
|
||||||
|
|
||||||
export const balance = createCommand({
|
export const balance = createCommand({
|
||||||
data: new SlashCommandBuilder().setName("balance").setDescription("Check your balance"),
|
data: new SlashCommandBuilder()
|
||||||
execute: async (interaction) => {
|
.setName("balance")
|
||||||
const balance = await getUserBalance(interaction.user.id) || 0;
|
.setDescription("Check your balance")
|
||||||
const embed = new EmbedBuilder().setDescription(`Your balance is ${balance}`);
|
|
||||||
|
|
||||||
|
, execute: async (interaction) => {
|
||||||
|
const user = interaction.user;
|
||||||
|
// Ensure user exists in DB
|
||||||
|
let dbUser = await getUserById(user.id);
|
||||||
|
if (!dbUser) {
|
||||||
|
await createUser(user.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const balance = await getUserBalance(user.id);
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(`${user.username}'s Balance`)
|
||||||
|
.setDescription(`💰 **${balance} coins**`)
|
||||||
|
.setColor("Green");
|
||||||
|
|
||||||
await interaction.reply({ embeds: [embed] });
|
await interaction.reply({ embeds: [embed] });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
68
src/commands/economy/daily.ts
Normal file
68
src/commands/economy/daily.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import { createCommand } from "@lib/utils";
|
||||||
|
import { addUserBalance } from "@/modules/economy/economy.service";
|
||||||
|
import { createUser, getUserById, updateUserDaily } from "@/modules/users/users.service";
|
||||||
|
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
|
||||||
|
|
||||||
|
export const daily = createCommand({
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("daily")
|
||||||
|
.setDescription("Get rewarded with daily coins"),
|
||||||
|
execute: async (interaction) => {
|
||||||
|
const user = interaction.user;
|
||||||
|
// Ensure user exists in DB
|
||||||
|
let dbUser = await getUserById(user.id);
|
||||||
|
if (!dbUser) {
|
||||||
|
dbUser = await createUser(user.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = new Date();
|
||||||
|
const lastDaily = dbUser.lastDaily;
|
||||||
|
|
||||||
|
if (lastDaily) {
|
||||||
|
const diff = now.getTime() - lastDaily.getTime();
|
||||||
|
const oneDay = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
if (diff < oneDay) {
|
||||||
|
const remaining = oneDay - diff;
|
||||||
|
const hours = Math.floor(remaining / (1000 * 60 * 60));
|
||||||
|
const minutes = Math.floor((remaining % (1000 * 60 * 60)) / (1000 * 60));
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle("Daily Reward")
|
||||||
|
.setDescription(`You have already claimed your daily reward.\nCome back in **${hours}h ${minutes}m**.`)
|
||||||
|
.setColor("Red");
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [embed], ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate streak
|
||||||
|
let streak = dbUser.dailyStreak;
|
||||||
|
if (lastDaily) {
|
||||||
|
const diff = now.getTime() - lastDaily.getTime();
|
||||||
|
const twoDays = 48 * 60 * 60 * 1000;
|
||||||
|
if (diff < twoDays) {
|
||||||
|
streak += 1;
|
||||||
|
} else {
|
||||||
|
streak = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
streak = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseReward = 100;
|
||||||
|
const streakBonus = (streak - 1) * 10;
|
||||||
|
const totalReward = baseReward + streakBonus;
|
||||||
|
|
||||||
|
await updateUserDaily(user.id, now, streak);
|
||||||
|
await addUserBalance(user.id, totalReward);
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle("Daily Reward Claimed!")
|
||||||
|
.setDescription(`You received **${totalReward} coins**! 💰\n\n**Streak:** ${streak} days 🔥`)
|
||||||
|
.setColor("Green");
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [embed] });
|
||||||
|
}
|
||||||
|
});
|
||||||
61
src/commands/economy/pay.ts
Normal file
61
src/commands/economy/pay.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { createCommand } from "@lib/utils";
|
||||||
|
import { getUserBalance, setUserBalance } from "@/modules/economy/economy.service";
|
||||||
|
import { createUser, getUserById } from "@/modules/users/users.service";
|
||||||
|
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js";
|
||||||
|
|
||||||
|
export const pay = createCommand({
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName("pay")
|
||||||
|
.setDescription("Send balance to another user")
|
||||||
|
.addUserOption(option =>
|
||||||
|
option.setName('recipient')
|
||||||
|
.setDescription('The user to send balance to')
|
||||||
|
.setRequired(true))
|
||||||
|
.addIntegerOption(option =>
|
||||||
|
option.setName('amount')
|
||||||
|
.setDescription('The amount of balance to send')
|
||||||
|
.setRequired(true))
|
||||||
|
|
||||||
|
|
||||||
|
, execute: async (interaction) => {
|
||||||
|
const user = interaction.user;
|
||||||
|
// Ensure if your user exists in DB
|
||||||
|
let dbUser = await getUserById(user.id);
|
||||||
|
if (!dbUser) {
|
||||||
|
await createUser(user.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const balance = await getUserBalance(user.id);
|
||||||
|
const recipient = interaction.options.getUser('recipient');
|
||||||
|
const amount = interaction.options.getInteger('amount');
|
||||||
|
|
||||||
|
if (amount! <= 0) {
|
||||||
|
await interaction.reply({ content: "❌ Amount must be greater than zero.", ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (amount! > balance) {
|
||||||
|
await interaction.reply({ content: "❌ You do not have enough coins to complete this transaction.", ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recipient!.id === user.id) {
|
||||||
|
await interaction.reply({ content: "❌ You cannot send coins to yourself.", ephemeral: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure recipient exists in DB
|
||||||
|
let dbRecipient = await getUserById(recipient!.id);
|
||||||
|
if (!dbRecipient) {
|
||||||
|
dbRecipient = await createUser(recipient!.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
await setUserBalance(user.id, balance - amount!); // Deduct from sender
|
||||||
|
await setUserBalance(recipient!.id, (await getUserBalance(recipient!.id)) + amount!); // Add to recipient
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setDescription(`sent **${amount} coins** to ${recipient!.username}`)
|
||||||
|
.setColor("Green");
|
||||||
|
|
||||||
|
await interaction.reply({ embeds: [embed] });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -4,6 +4,7 @@ import { env } from "@lib/env";
|
|||||||
|
|
||||||
// Load commands
|
// Load commands
|
||||||
await KyokoClient.loadCommands();
|
await KyokoClient.loadCommands();
|
||||||
|
await KyokoClient.deployCommands();
|
||||||
|
|
||||||
KyokoClient.once(Events.ClientReady, async c => {
|
KyokoClient.once(Events.ClientReady, async c => {
|
||||||
console.log(`Ready! Logged in as ${c.user.tag}`);
|
console.log(`Ready! Logged in as ${c.user.tag}`);
|
||||||
|
|||||||
@@ -9,4 +9,10 @@ export async function getUserBalance(userId: string) {
|
|||||||
|
|
||||||
export async function setUserBalance(userId: string, balance: number) {
|
export async function setUserBalance(userId: string, balance: number) {
|
||||||
await DrizzleClient.update(users).set({ balance }).where(eq(users.userId, userId));
|
await DrizzleClient.update(users).set({ balance }).where(eq(users.userId, userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function addUserBalance(userId: string, amount: number) {
|
||||||
|
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.userId, userId) });
|
||||||
|
if (!user) return;
|
||||||
|
await DrizzleClient.update(users).set({ balance: user.balance + amount }).where(eq(users.userId, userId));
|
||||||
}
|
}
|
||||||
@@ -8,4 +8,8 @@ export async function getUserById(userId: string) {
|
|||||||
|
|
||||||
export async function createUser(userId: string) {
|
export async function createUser(userId: string) {
|
||||||
return (await DrizzleClient.insert(users).values({ userId }).returning())[0]!;
|
return (await DrizzleClient.insert(users).values({ userId }).returning())[0]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateUserDaily(userId: string, lastDaily: Date, dailyStreak: number) {
|
||||||
|
await DrizzleClient.update(users).set({ lastDaily, dailyStreak }).where(eq(users.userId, userId));
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user