migrate from supabase to postgres

This commit is contained in:
2025-09-09 16:01:58 +07:00
parent a836ebb2a6
commit aa62667d89
21 changed files with 327 additions and 902 deletions
+4 -5
View File
@@ -3,7 +3,7 @@ import asyncio
from core.bot import bot, CHANNEL_NOI_TU_ID
from typing import Set
from datetime import datetime
from apps.currency import incr
from apps.score import incr
# Lazy load repository để tránh lỗi database connection
noi_tu_repo = None
@@ -199,7 +199,7 @@ async def end_game(ctx):
game.start_time = None
@bot.command(name='add')
async def add_word(ctx, *, word: str, meaning: str = None):
async def add_word(ctx, *, word: str):
"""Thêm từ mới vào cơ sở dữ liệu (chỉ admin)"""
if not is_correct_channel(ctx):
return
@@ -219,12 +219,11 @@ async def add_word(ctx, *, word: str, meaning: str = None):
return
# Thêm từ
success = await get_noi_tu_repo().add(word, meaning)
success = await get_noi_tu_repo().add(word)
if success:
embed = discord.Embed(
title="✅ Thêm từ thành công!",
description=f"Từ: **{word}**\n"
f"Nghĩa: {meaning if meaning else 'Không có'}",
description=f"Từ: **{word}**",
color=discord.Color.green()
)
await ctx.send(embed=embed)
+14 -14
View File
@@ -1,38 +1,38 @@
import discord
from discord.ext import commands
from core.bot import bot, currency_repo
from core.bot import bot, score_repo
@bot.command(name="ncheck", description="Kiểm tra thông tin tài khoản của bạn")
async def currency_check(ctx: commands.Context):
async def score_check(ctx: commands.Context):
"""Kiểm tra thông tin tài khoản của bạn"""
# Check if user is registered in database. If not, create a new one
currency = await currency_repo.get(ctx.author.id)
if not currency:
await ctx.send("Bạn chưa có tài khoản currency")
await currency_repo.create(ctx.author.id, 0)
currency = await currency_repo.get(ctx.author.id)
score = await score_repo.get(ctx.author.id)
if not score:
await ctx.send("Bạn chưa có tài khoản score")
await score_repo.create(ctx.author.id, 0)
score = await score_repo.get(ctx.author.id)
# Kiểm tra lại sau khi tạo
if not currency:
await ctx.send("Có lỗi xảy ra khi tạo tài khoản currency")
if not score:
await ctx.send("Có lỗi xảy ra khi tạo tài khoản score")
return
await ctx.send(f"Thông tin tài khoản của bạn: {currency.balance}")
await ctx.send(f"Thông tin tài khoản của bạn: {score.point}")
@bot.command(name="rank", description="list rank")
async def list_rank(ctx: commands.Context):
"""Kiểm tra thông tin tài khoản của bạn"""
# Check if user is registered in database. If not, create a new one
balances = await currency_repo.get_all()
points = await score_repo.get_all()
msg = "```\n"
msg += f"{'No.':<4} {'Name':<20} {'Win':>8}\n"
msg += "-" * 34 + "\n"
for i, b in enumerate(balances, start=1):
msg += f"{i:<4} {b.user_name:<20} {b.balance:>8}\n"
for i, b in enumerate(points, start=1):
msg += f"{i:<4} {b.user_name:<20} {b.point:>8}\n"
msg += "```"
await ctx.send(msg)
async def incr(user_id, user_name, amount):
await currency_repo.upsert_or_increment_balance(user_id, user_name, amount)
await score_repo.upsert_or_increment_point(user_id, user_name, amount)