feat: Implement userService.getOrCreateUser and integrate it across commands, remove old utility scripts, and fix daily bonus calculation.

This commit is contained in:
syntaxbullet
2025-12-08 10:29:40 +01:00
parent 29c0a4752d
commit 866cfab03e
12 changed files with 42 additions and 94 deletions

View File

@@ -15,12 +15,7 @@ export const balance = createCommand({
await interaction.deferReply();
const targetUser = interaction.options.getUser("user") || interaction.user;
const user = await userService.getUserById(targetUser.id);
if (!user) {
await interaction.editReply({ content: "❌ User not found in database." });
return;
}
const user = await userService.getOrCreateUser(targetUser.id, targetUser.username);
const embed = new EmbedBuilder()
.setAuthor({ name: targetUser.username, iconURL: targetUser.displayAvatarURL() })

View File

@@ -1,6 +1,7 @@
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 daily = createCommand({
data: new SlashCommandBuilder()

View File

@@ -21,7 +21,7 @@ export const pay = createCommand({
execute: async (interaction) => {
await interaction.deferReply();
const targetUser = interaction.options.getUser("user", true);
const targetUser = await userService.getOrCreateUser(interaction.options.getUser("user", true).id, interaction.options.getUser("user", true).username);
const amount = BigInt(interaction.options.getInteger("amount", true));
const senderId = interaction.user.id;
const receiverId = targetUser.id;
@@ -31,12 +31,6 @@ export const pay = createCommand({
return;
}
// Ensure receiver exists
let receiver = await userService.getUserById(receiverId);
if (!receiver) {
receiver = await userService.createUser(receiverId, targetUser.username, undefined);
}
try {
await economyService.transfer(senderId, receiverId, amount);

View File

@@ -1,6 +1,7 @@
import { createCommand } from "@/lib/utils";
import { SlashCommandBuilder, EmbedBuilder } from "discord.js";
import { inventoryService } from "@/modules/inventory/inventory.service";
import { userService } from "@/modules/user/user.service";
export const inventory = createCommand({
data: new SlashCommandBuilder()
@@ -15,11 +16,12 @@ export const inventory = createCommand({
await interaction.deferReply();
const targetUser = interaction.options.getUser("user") || interaction.user;
const items = await inventoryService.getInventory(targetUser.id);
const user = await userService.getOrCreateUser(targetUser.id, targetUser.username);
const items = await inventoryService.getInventory(user.id);
if (!items || items.length === 0) {
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Inventory`)
.setTitle(`${user.username}'s Inventory`)
.setDescription("Inventory is empty.")
.setColor("Blue");
@@ -32,7 +34,7 @@ export const inventory = createCommand({
}).join("\n");
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Inventory`)
.setTitle(`${user.username}'s Inventory`)
.setDescription(description)
.setColor("Blue")
.setTimestamp();

View File

@@ -17,25 +17,7 @@ export const profile = createCommand({
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 user = await userService.getOrCreateUser(targetUser.id, targetUser.username);
const embed = new EmbedBuilder()
.setTitle(`${targetUser.username}'s Profile`)