forked from syntaxbullet/AuroraBot-discord
feat: Implement userService.getOrCreateUser and integrate it across commands, remove old utility scripts, and fix daily bonus calculation.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { users, transactions, cooldowns } from "@/db/schema";
|
||||
import { eq, sql, and, gt } from "drizzle-orm";
|
||||
import { eq, sql, and } from "drizzle-orm";
|
||||
import { DrizzleClient } from "@/lib/DrizzleClient";
|
||||
import { userService } from "@/modules/user/user.service";
|
||||
|
||||
const DAILY_REWARD_AMOUNT = 100n;
|
||||
const STREAK_BONUS = 10n;
|
||||
@@ -113,7 +112,7 @@ export const economyService = {
|
||||
streak = 1;
|
||||
}
|
||||
|
||||
const bonus = BigInt(streak) * STREAK_BONUS;
|
||||
const bonus = (BigInt(streak) - 1n) * STREAK_BONUS;
|
||||
|
||||
const totalReward = DAILY_REWARD_AMOUNT + bonus;
|
||||
|
||||
|
||||
@@ -14,6 +14,24 @@ export const userService = {
|
||||
const user = await DrizzleClient.query.users.findFirst({ where: eq(users.username, username) });
|
||||
return user;
|
||||
},
|
||||
getOrCreateUser: async (id: string, username: string, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
let user = await txFn.query.users.findFirst({
|
||||
where: eq(users.id, BigInt(id)),
|
||||
with: { class: true }
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
const [newUser] = await txFn.insert(users).values({
|
||||
id: BigInt(id),
|
||||
username,
|
||||
}).returning();
|
||||
user = { ...newUser, class: null };
|
||||
}
|
||||
return user;
|
||||
};
|
||||
return tx ? await execute(tx) : await DrizzleClient.transaction(execute);
|
||||
},
|
||||
createUser: async (id: string | bigint, username: string, classId?: bigint, tx?: any) => {
|
||||
const execute = async (txFn: any) => {
|
||||
const [user] = await txFn.insert(users).values({
|
||||
|
||||
Reference in New Issue
Block a user