feat: Introduce new modules for class, inventory, leveling, and quests with expanded schema, refactor user service, and add verification scripts.

This commit is contained in:
syntaxbullet
2025-12-07 23:03:33 +01:00
parent be471f348d
commit 29c0a4752d
21 changed files with 1228 additions and 163 deletions

View File

@@ -1,29 +1,32 @@
import { createCommand } from "@lib/utils";
import { getUserBalance } from "@/modules/economy/economy.service";
import { createUser, getUserById } from "@/modules/users/users.service";
import { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits } from "discord.js";
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { userService } from "@/modules/user/user.service";
export const balance = createCommand({
data: new SlashCommandBuilder()
.setName("balance")
.setDescription("Check your balance")
.setDescription("Check your or another user's balance")
.addUserOption(option =>
option.setName("user")
.setDescription("The user to check")
.setRequired(false)
),
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user") || interaction.user;
const user = await userService.getUserById(targetUser.id);
, execute: async (interaction) => {
const user = interaction.user;
// Ensure user exists in DB
let dbUser = await getUserById(user.id);
if (!dbUser) {
await createUser(user.id);
if (!user) {
await interaction.editReply({ content: "❌ User not found in database." });
return;
}
const balance = await getUserBalance(user.id);
const embed = new EmbedBuilder()
.setTitle(`${user.username}'s Balance`)
.setDescription(`💰 **${balance} coins**`)
.setColor("Green");
.setAuthor({ name: targetUser.username, iconURL: targetUser.displayAvatarURL() })
.setDescription(`**Balance**: ${user.balance || 0n} 🪙`)
.setColor("Yellow");
await interaction.reply({ embeds: [embed] });
await interaction.editReply({ embeds: [embed] });
}
});
});

View File

@@ -1,68 +1,42 @@
import { createCommand } from "@lib/utils";
import { addUserBalance } from "@/modules/economy/economy.service";
import { createUser, getUserById, updateUserDaily } from "@/modules/users/users.service";
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { economyService } from "@/modules/economy/economy.service";
export const daily = createCommand({
data: new SlashCommandBuilder()
.setName("daily")
.setDescription("Get rewarded with daily coins"),
.setDescription("Claim your daily reward"),
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);
}
await interaction.deferReply();
const now = new Date();
const lastDaily = dbUser.lastDaily;
try {
const result = await economyService.claimDaily(interaction.user.id);
if (lastDaily) {
const diff = now.getTime() - lastDaily.getTime();
const oneDay = 24 * 60 * 60 * 1000;
const embed = new EmbedBuilder()
.setTitle("💰 Daily Reward Claimed!")
.setDescription(`You claimed **${result.amount}** coins!`)
.addFields(
{ name: "Current Streak", value: `🔥 ${result.streak} days`, inline: true },
{ name: "Next Reward", value: `<t:${Math.floor(result.nextReadyAt.getTime() / 1000)}:R>`, inline: true }
)
.setColor("Gold")
.setTimestamp();
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));
await interaction.editReply({ embeds: [embed] });
} catch (error: any) {
if (error.message.includes("Daily already claimed")) {
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 });
.setTitle("⏳ Cooldown")
.setDescription(error.message)
.setColor("Orange");
await interaction.editReply({ embeds: [embed] });
return;
}
console.error(error);
await interaction.editReply({ content: "❌ An error occurred while claiming your daily reward." });
}
// 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] });
}
});
});

View File

@@ -1,61 +1,60 @@
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";
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { economyService } from "@/modules/economy/economy.service";
import { userService } from "@/modules/user/user.service";
export const pay = createCommand({
data: new SlashCommandBuilder()
.setName("pay")
.setDescription("Send balance to another user")
.setDescription("Transfer coins to another user")
.addUserOption(option =>
option.setName('recipient')
.setDescription('The user to send balance to')
.setRequired(true))
option.setName("user")
.setDescription("The user to pay")
.setRequired(true)
)
.addIntegerOption(option =>
option.setName('amount')
.setDescription('The amount of balance to send')
.setRequired(true))
option.setName("amount")
.setDescription("Amount to transfer")
.setMinValue(1)
.setRequired(true)
),
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user", true);
const amount = BigInt(interaction.options.getInteger("amount", true));
const senderId = interaction.user.id;
const receiverId = targetUser.id;
, 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 });
if (senderId === receiverId) {
await interaction.editReply({ content: "❌ You cannot pay yourself." });
return;
}
if (recipient!.id === user.id) {
await interaction.reply({ content: "❌ You cannot send coins to yourself.", ephemeral: true });
return;
// Ensure receiver exists
let receiver = await userService.getUserById(receiverId);
if (!receiver) {
receiver = await userService.createUser(receiverId, targetUser.username, undefined);
}
// Ensure recipient exists in DB
let dbRecipient = await getUserById(recipient!.id);
if (!dbRecipient) {
dbRecipient = await createUser(recipient!.id);
try {
await economyService.transfer(senderId, receiverId, amount);
const embed = new EmbedBuilder()
.setTitle("💸 Transfer Successful")
.setDescription(`Successfully sent **${amount}** coins to ${targetUser}.`)
.setColor("Green")
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
} catch (error: any) {
if (error.message.includes("Insufficient funds")) {
await interaction.editReply({ content: "❌ Insufficient funds." });
return;
}
console.error(error);
await interaction.editReply({ content: "❌ Transfer failed." });
}
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] });
}
});
});

View File

@@ -0,0 +1,42 @@
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { inventoryService } from "@/modules/inventory/inventory.service";
export const inventory = createCommand({
data: new SlashCommandBuilder()
.setName("inventory")
.setDescription("View your or another user's inventory")
.addUserOption(option =>
option.setName("user")
.setDescription("User to view")
.setRequired(false)
),
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user") || interaction.user;
const items = await inventoryService.getInventory(targetUser.id);
if (!items || items.length === 0) {
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Inventory`)
.setDescription("Inventory is empty.")
.setColor("Blue");
await interaction.editReply({ embeds: [embed] });
return;
}
const description = items.map(entry => {
return `**${entry.item.name}** x${entry.quantity}`;
}).join("\n");
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Inventory`)
.setDescription(description)
.setColor("Blue")
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
}
});

View File

@@ -0,0 +1,50 @@
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { DrizzleClient } from "@/lib/DrizzleClient";
import { users } from "@/db/schema";
import { desc } from "drizzle-orm";
export const leaderboard = createCommand({
data: new SlashCommandBuilder()
.setName("leaderboard")
.setDescription("View the top players")
.addStringOption(option =>
option.setName("type")
.setDescription("Sort by XP or Balance")
.setRequired(true)
.addChoices(
{ name: "Level / XP", value: "xp" },
{ name: "Balance", value: "balance" }
)
),
execute: async (interaction) => {
await interaction.deferReply();
const type = interaction.options.getString("type", true);
const isXp = type === "xp";
const leaders = await DrizzleClient.query.users.findMany({
orderBy: isXp ? desc(users.xp) : desc(users.balance),
limit: 10
});
if (leaders.length === 0) {
await interaction.editReply({ content: "❌ No users found." });
return;
}
const description = leaders.map((user, index) => {
const medal = index === 0 ? "🥇" : index === 1 ? "🥈" : index === 2 ? "🥉" : `${index + 1}.`;
const value = isXp ? `Lvl ${user.level} (${user.xp} XP)` : `${user.balance} 🪙`;
return `${medal} **${user.username}** — ${value}`;
}).join("\n");
const embed = new EmbedBuilder()
.setTitle(isXp ? "🏆 XP Leaderboard" : "💰 Richest Players")
.setDescription(description)
.setColor("Gold")
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
}
});

View File

@@ -0,0 +1,45 @@
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { questService } from "@/modules/quest/quest.service";
export const quests = createCommand({
data: new SlashCommandBuilder()
.setName("quests")
.setDescription("View your active quests"),
execute: async (interaction) => {
await interaction.deferReply({ ephemeral: true });
const userQuests = await questService.getUserQuests(interaction.user.id);
if (!userQuests || userQuests.length === 0) {
const embed = new EmbedBuilder()
.setTitle("📜 Quest Log")
.setDescription("You have no active quests.")
.setColor("Grey");
await interaction.editReply({ embeds: [embed] });
return;
}
const embed = new EmbedBuilder()
.setTitle("📜 Quest Log")
.setColor("Blue")
.setTimestamp();
userQuests.forEach(entry => {
const status = entry.completedAt ? "✅ Completed" : "In Progress";
const rewards = entry.quest.rewards as { xp?: number, balance?: number };
const rewardStr = [];
if (rewards?.xp) rewardStr.push(`${rewards.xp} XP`);
if (rewards?.balance) rewardStr.push(`${rewards.balance} 🪙`);
embed.addFields({
name: `${entry.quest.name} (${status})`,
value: `${entry.quest.description}\n**Rewards:** ${rewardStr.join(", ")}\n**Progress:** ${entry.progress}%`,
inline: false
});
});
await interaction.editReply({ embeds: [embed] });
}
});

View File

@@ -0,0 +1,57 @@
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { userService } from "@/modules/user/user.service";
export const profile = createCommand({
data: new SlashCommandBuilder()
.setName("profile")
.setDescription("View your or another user's profile")
.addUserOption(option =>
option.setName("user")
.setDescription("The user to view")
.setRequired(false)
),
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user") || interaction.user;
const targetMember = await interaction.guild?.members.fetch(targetUser.id).catch(() => null);
let user = await userService.getUserById(targetUser.id);
if (!user) {
// Auto-create user if they don't exist
// Assuming no class assigned initially (null)
user = await userService.createUser(targetUser.id, targetUser.username, undefined);
}
// Refetch to get class relation if needed (though createUser returns user, it might not have relations loaded if we add them later)
// For now, let's assume we might need to join class manually or update userService to return it.
// Actually, let's just use what we have. If we need class name, we might need a separate query or update userService to include relation.
// Let's check if 'class' is in the returned user object from userService.getUserById.
// Looking at userService.ts, it uses findFirst. If we want relations, we need to add `with`.
// Let's quickly re-fetch with relations to be safe and get Class Name
// Or we can update userService given we are in "Implement Commands" but changing service might be out of scope?
// No, I should make sure the command works.
// Let's rely on standard Drizzle query here for the "view" part or update service.
// Updating service is cleaner.
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Profile`)
.setThumbnail(targetUser.displayAvatarURL({ size: 512 }))
.setColor(targetMember?.displayHexColor || "Random")
.addFields(
{ name: "Level", value: `${user!.level || 1}`, inline: true },
{ name: "XP", value: `${user!.xp || 0}`, inline: true },
{ name: "Balance", value: `${user!.balance || 0}`, inline: true },
{ name: "Class", value: user!.class?.name || "None", inline: true },
{ name: "Joined Discord", value: `<t:${Math.floor(targetUser.createdTimestamp / 1000)}:R>`, inline: true },
{ name: "Joined Server", value: targetMember ? `<t:${Math.floor(targetMember.joinedTimestamp! / 1000)}:R>` : "Unknown", inline: true }
)
.setFooter({ text: `ID: ${targetUser.id}` })
.setTimestamp();
await interaction.editReply({ embeds: [embed] });
}
});