forked from syntaxbullet/AuroraBot-discord
feat: Implement custom error classes, a Drizzle transaction utility, and update Discord.js ephemeral message flags.
This commit is contained in:
@@ -1,30 +1,32 @@
|
||||
import { users, transactions, userTimers } from "@/db/schema";
|
||||
import { eq, sql, and } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { config } from "@/lib/config";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
import { UserError } from "@/lib/errors";
|
||||
|
||||
export const economyService = {
|
||||
transfer: async (fromUserId: string, toUserId: string, amount: bigint, tx?: any) => {
|
||||
transfer: async (fromUserId: string, toUserId: string, amount: bigint, tx?: Transaction) => {
|
||||
if (amount <= 0n) {
|
||||
throw new Error("Amount must be positive");
|
||||
throw new UserError("Amount must be positive");
|
||||
}
|
||||
|
||||
if (fromUserId === toUserId) {
|
||||
throw new Error("Cannot transfer to self");
|
||||
throw new UserError("Cannot transfer to self");
|
||||
}
|
||||
|
||||
const execute = async (txFn: any) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
// Check sender balance
|
||||
const sender = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(fromUserId)),
|
||||
});
|
||||
|
||||
if (!sender) {
|
||||
throw new Error("Sender not found");
|
||||
throw new UserError("Sender not found");
|
||||
}
|
||||
|
||||
if ((sender.balance ?? 0n) < amount) {
|
||||
throw new Error("Insufficient funds");
|
||||
throw new UserError("Insufficient funds");
|
||||
}
|
||||
|
||||
// Deduct from sender
|
||||
@@ -59,19 +61,11 @@ export const economyService = {
|
||||
});
|
||||
|
||||
return { success: true, amount };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
}, tx);
|
||||
},
|
||||
|
||||
claimDaily: async (userId: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
claimDaily: async (userId: string, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const now = new Date();
|
||||
const startOfDay = new Date(now);
|
||||
startOfDay.setHours(0, 0, 0, 0);
|
||||
@@ -86,7 +80,7 @@ export const economyService = {
|
||||
});
|
||||
|
||||
if (cooldown && cooldown.expiresAt > now) {
|
||||
throw new Error(`Daily already claimed. Ready at ${cooldown.expiresAt}`);
|
||||
throw new UserError(`Daily already claimed. Ready at ${cooldown.expiresAt}`);
|
||||
}
|
||||
|
||||
// Get user for streak logic
|
||||
@@ -95,7 +89,7 @@ export const economyService = {
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error("User not found");
|
||||
throw new Error("User not found"); // This might be system error because user should exist if authenticated, but keeping simple for now
|
||||
}
|
||||
|
||||
let streak = (user.dailyStreak || 0) + 1;
|
||||
@@ -145,26 +139,18 @@ export const economyService = {
|
||||
});
|
||||
|
||||
return { claimed: true, amount: totalReward, streak, nextReadyAt };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
}, tx);
|
||||
},
|
||||
|
||||
modifyUserBalance: async (id: string, amount: bigint, type: string, description: string, relatedUserId?: string | null, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
modifyUserBalance: async (id: string, amount: bigint, type: string, description: string, relatedUserId?: string | null, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
if (amount < 0n) {
|
||||
// Check sufficient funds if removing
|
||||
const user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id))
|
||||
});
|
||||
if (!user || (user.balance ?? 0n) < -amount) {
|
||||
throw new Error("Insufficient funds");
|
||||
throw new UserError("Insufficient funds");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,14 +170,6 @@ export const economyService = {
|
||||
});
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
});
|
||||
}
|
||||
}, tx);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
|
||||
import { inventory, items, users } from "@/db/schema";
|
||||
import { eq, and, sql, count } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
import { config } from "@/lib/config";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export const inventoryService = {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
addItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
// Check if item exists in inventory
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
@@ -39,7 +40,7 @@ export const inventoryService = {
|
||||
.from(inventory)
|
||||
.where(eq(inventory.userId, BigInt(userId)));
|
||||
|
||||
if (inventoryCount.count >= config.inventory.maxSlots) {
|
||||
if (inventoryCount && inventoryCount.count >= config.inventory.maxSlots) {
|
||||
throw new Error(`Inventory full (Max ${config.inventory.maxSlots} slots)`);
|
||||
}
|
||||
|
||||
@@ -56,12 +57,11 @@ export const inventoryService = {
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
removeItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
removeItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const existing = await txFn.query.inventory.findFirst({
|
||||
where: and(
|
||||
eq(inventory.userId, BigInt(userId)),
|
||||
@@ -93,8 +93,7 @@ export const inventoryService = {
|
||||
.returning();
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
getInventory: async (userId: string) => {
|
||||
@@ -106,8 +105,8 @@ export const inventoryService = {
|
||||
});
|
||||
},
|
||||
|
||||
buyItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
buyItem: async (userId: string, itemId: number, quantity: bigint = 1n, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const item = await txFn.query.items.findFirst({
|
||||
where: eq(items.id, itemId),
|
||||
});
|
||||
@@ -123,9 +122,7 @@ export const inventoryService = {
|
||||
await inventoryService.addItem(userId, itemId, quantity, txFn);
|
||||
|
||||
return { success: true, item, totalPrice };
|
||||
};
|
||||
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
getItem: async (itemId: number) => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { users, userTimers } from "@/db/schema";
|
||||
import { eq, sql, and } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import { config } from "@/lib/config";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export const levelingService = {
|
||||
// Calculate XP required for a specific level
|
||||
@@ -10,8 +11,8 @@ export const levelingService = {
|
||||
},
|
||||
|
||||
// Pure XP addition - No cooldown checks
|
||||
addXp: async (id: string, amount: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
addXp: async (id: string, amount: bigint, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
// Get current state
|
||||
const user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id)),
|
||||
@@ -43,20 +44,12 @@ export const levelingService = {
|
||||
.returning();
|
||||
|
||||
return { user: updatedUser, levelUp, currentLevel };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
})
|
||||
}
|
||||
}, tx);
|
||||
},
|
||||
|
||||
// Handle chat XP with cooldowns
|
||||
processChatXp: async (id: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
processChatXp: async (id: string, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
// check if an xp cooldown is in place
|
||||
const cooldown = await txFn.query.userTimers.findFirst({
|
||||
where: and(
|
||||
@@ -93,14 +86,6 @@ export const levelingService = {
|
||||
});
|
||||
|
||||
return { awarded: true, amount, ...result };
|
||||
};
|
||||
|
||||
if (tx) {
|
||||
return await execute(tx);
|
||||
} else {
|
||||
return await DrizzleClient.transaction(async (t) => {
|
||||
return await execute(t);
|
||||
})
|
||||
}
|
||||
}, tx);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,10 +4,12 @@ import { eq, and, sql } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
import { levelingService } from "@/modules/leveling/leveling.service";
|
||||
import { withTransaction } from "@/lib/db";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export const questService = {
|
||||
assignQuest: async (userId: string, questId: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
assignQuest: async (userId: string, questId: number, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
return await txFn.insert(userQuests)
|
||||
.values({
|
||||
userId: BigInt(userId),
|
||||
@@ -16,12 +18,11 @@ export const questService = {
|
||||
})
|
||||
.onConflictDoNothing() // Ignore if already assigned
|
||||
.returning();
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
updateProgress: async (userId: string, questId: number, progress: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
updateProgress: async (userId: string, questId: number, progress: number, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
return await txFn.update(userQuests)
|
||||
.set({ progress: progress })
|
||||
.where(and(
|
||||
@@ -29,12 +30,11 @@ export const questService = {
|
||||
eq(userQuests.questId, questId)
|
||||
))
|
||||
.returning();
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
completeQuest: async (userId: string, questId: number, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
completeQuest: async (userId: string, questId: number, tx?: Transaction) => {
|
||||
return await withTransaction(async (txFn) => {
|
||||
const userQuest = await txFn.query.userQuests.findFirst({
|
||||
where: and(
|
||||
eq(userQuests.userId, BigInt(userId)),
|
||||
@@ -73,9 +73,7 @@ export const questService = {
|
||||
}
|
||||
|
||||
return { success: true, rewards: results };
|
||||
};
|
||||
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
}, tx);
|
||||
},
|
||||
|
||||
getUserQuests: async (userId: string) => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { economyService } from "@/modules/economy/economy.service";
|
||||
import { inventoryService } from "@/modules/inventory/inventory.service";
|
||||
import { itemTransactions } from "@/db/schema";
|
||||
import type { Transaction } from "@/lib/types";
|
||||
|
||||
export class TradeService {
|
||||
private static sessions = new Map<string, TradeSession>();
|
||||
@@ -136,7 +137,7 @@ export class TradeService {
|
||||
this.endSession(threadId);
|
||||
}
|
||||
|
||||
private static async processTransfer(tx: any, from: TradeParticipant, to: TradeParticipant, threadId: string) {
|
||||
private static async processTransfer(tx: Transaction, from: TradeParticipant, to: TradeParticipant, threadId: string) {
|
||||
// 1. Money
|
||||
if (from.offer.money > 0n) {
|
||||
await economyService.modifyUserBalance(
|
||||
|
||||
Reference in New Issue
Block a user