remove unuse file and modify home debt app

This commit is contained in:
2025-06-26 16:51:54 +07:00
parent c31cff8749
commit 464bb18b3c
10 changed files with 84 additions and 362 deletions
+42 -104
View File
@@ -1,146 +1,84 @@
import discord
from core.bot import bot, server_repo, channel_repo, home_debt_repo
from core.bot import bot, home_debt_repo, CHANNEL_HOME_DEBT_ID
from utils.common import format_vnd
@bot.tree.command(name='set-home-debt', description='Set channel to home debt')
async def set_home_debt(interaction: discord.Interaction, channel: discord.TextChannel):
try:
# Kiểm tra xem người dùng có quyền admin không
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("Bạn cần có quyền Administrator để sử dụng lệnh này!", ephemeral=True)
return
await interaction.response.defer(ephemeral=False)
# Ensure server is in database
server = await server_repo.get(str(interaction.guild_id))
if not server:
await interaction.followup.send("Server chưa được khởi tạo trong database! Vui lòng sử dụng lệnh /init_server trước.", ephemeral=True)
return
def is_correct_channel(ctx):
"""Kiểm tra xem command có được thực hiện trong đúng channel không"""
return ctx.channel.id == CHANNEL_HOME_DEBT_ID
# Ensure channel was registered in database
discord_channel = await channel_repo.get_channel(channel.id)
if not discord_channel:
await channel_repo.create_channel(interaction.guild_id, channel.id, 'home_debt')
else:
await channel_repo.update_channel(channel.id, 'home_debt')
await interaction.followup.send(f"home_debt app đã được cấu hình cho {channel.mention}")
except Exception as e:
if interaction.response.is_done():
await interaction.followup.send(f"Lỗi khi cấu hình home_debt app: {str(e)}", ephemeral=True)
else:
await interaction.response.send_message(f"Lỗi khi cấu hình home_debt app: {str(e)}", ephemeral=True)
# Check if the channel is registered and configured for home_debt app
async def check_channel_home_debt(channel_id: int) -> bool:
# Check if channel is registered in database
channel = await channel_repo.get_channel(channel_id)
if not channel or channel.app != 'home_debt':
return False
return True
@bot.tree.command(name="home-debt-add", description="Thêm khoản chi tiêu mới")
async def home_debt_add(interaction: discord.Interaction, amount: int, description: str = "Không có lý do"):
@bot.command(name="hdadd", description="Thêm khoản chi tiêu mới")
async def add(ctx, amount: int):
"""Vì home chỉ có 2 người nên sẽ tự động thêm khoản chi tiêu cho người còn lại"""
try:
check = await check_channel_home_debt(interaction.channel_id)
if not check:
await interaction.response.send_message("Channel chưa được đăng ký!", ephemeral=True)
if not is_correct_channel(ctx):
return
await interaction.response.defer(ephemeral=False)
# Get info other user from home_debt table
other_user = await home_debt_repo.get_other(interaction.user.id)
other_user = await home_debt_repo.get_other(ctx.author.id)
other_user.value += round(amount / 2)
resp = await home_debt_repo.update_home_debt(other_user)
if not resp:
await interaction.followup.send("Có lỗi xảy ra khi cập nhật dữ liệu", ephemeral=True)
await ctx.send("Có lỗi xảy ra khi cập nhật dữ liệu")
return
await interaction.followup.send(f"Đã thêm {format_vnd(round(amount * 1000 / 2))} cho {interaction.user.name}. Số dư hiện tại là {format_vnd(other_user.value * 1000)}", ephemeral=False)
await ctx.send(f"Đã thêm {format_vnd(round(amount * 1000 / 2))} cho {ctx.author.name}. Số dư hiện tại là {format_vnd(other_user.value * 1000)}")
except Exception as e:
if interaction.response.is_done():
await interaction.followup.send(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
else:
await interaction.response.send_message(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
await ctx.send(f"Có lỗi xảy ra: {str(e)}")
@bot.tree.command(name="home-debt-check", description="Kiểm tra số dư của bạn")
async def home_debt_check(interaction: discord.Interaction):
@bot.command(name="hdcheck", description="Kiểm tra số dư của bạn")
async def home_debt_check(ctx):
"""Kiểm tra số dư của mọi người"""
try:
check = await check_channel_home_debt(interaction.channel_id)
if not check:
await interaction.response.send_message("Channel chưa được đăng ký!", ephemeral=True)
if not is_correct_channel(ctx):
return
await interaction.response.defer(ephemeral=False)
# Lấy số dư của mọi người
home_debts = await home_debt_repo.get_all()
embed = discord.Embed(title="Số dư của mọi người", color=discord.Color.blue())
for home_debt in home_debts:
user = await bot.fetch_user(home_debt.user_id)
value = format_vnd(home_debt.value * 1000)
embed.add_field(name=f"{user.name}", value=f"{value}", inline=False)
await interaction.followup.send(embed=embed, ephemeral=False)
except Exception as e:
if interaction.response.is_done():
await interaction.followup.send(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
else:
await interaction.response.send_message(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
@bot.tree.command(name="vay-debt", description="Vay nợ")
async def vay_debt(interaction: discord.Interaction, amount: int, description: str = "Không có lý do"):
"""Vay nợ"""
try:
check = await check_channel_home_debt(interaction.channel_id)
if not check:
await interaction.response.send_message("Channel chưa được đăng ký!", ephemeral=True)
return
await interaction.response.defer(ephemeral=False)
# Get info user from home_debt table
user = await home_debt_repo.get(interaction.user.id)
users = await home_debt_repo.get_all()
embed = discord.Embed(title="Số dư của mọi người", color=discord.Color.blue())
for user in users:
value = format_vnd(user.value * 1000)
embed.add_field(name=f"{ctx.guild.get_member(user.user_id).display_name}", value=f"{value}", inline=False)
await ctx.send(embed=embed)
except Exception as e:
await ctx.send(f"Có lỗi xảy ra: {str(e)}")
@bot.command(name="hdvay", description="Vay nợ")
async def vay(ctx, amount: int):
"""Vay nợ"""
try:
if not is_correct_channel(ctx):
return
# Get info user from home_debt table
user = await home_debt_repo.get(ctx.author.id)
user.value += amount
resp = await home_debt_repo.update_home_debt(user)
if not resp:
await interaction.followup.send("Có lỗi xảy ra khi cập nhật dữ liệu", ephemeral=True)
await ctx.send("Có lỗi xảy ra khi cập nhật dữ liệu")
return
# Send message to user
await interaction.followup.send(f"Đã vay {format_vnd(amount * 1000)} bởi {interaction.user.name}", ephemeral=False)
await ctx.send(f"Đã vay {format_vnd(amount * 1000)} bởi {ctx.author.name}")
except Exception as e:
if interaction.response.is_done():
await interaction.followup.send(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
else:
await interaction.response.send_message(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
await ctx.send(f"Có lỗi xảy ra: {str(e)}")
@bot.tree.command(name="tra-debt", description="Trả nợ")
async def tra_debt(interaction: discord.Interaction, amount: int):
@bot.command(name="hdtra", description="Trả nợ")
async def tra(ctx, amount: int):
"""Trả nợ"""
try:
check = await check_channel_home_debt(interaction.channel_id)
if not check:
await interaction.response.send_message("Channel chưa được đăng ký!", ephemeral=True)
if not is_correct_channel(ctx):
return
await interaction.response.defer(ephemeral=False)
# Get info user from home_debt table
user = await home_debt_repo.get(interaction.user.id)
user = await home_debt_repo.get(ctx.author.id)
user.value -= amount
resp = await home_debt_repo.update_home_debt(user)
if not resp:
await interaction.followup.send("Có lỗi xảy ra khi cập nhật dữ liệu", ephemeral=True)
await ctx.send("Có lỗi xảy ra khi cập nhật dữ liệu")
return
# Send message to user
await interaction.followup.send(f"Đã trả {format_vnd(amount * 1000)} từ {interaction.user.name}", ephemeral=False)
await ctx.send(f"Đã trả {format_vnd(amount * 1000)} từ {ctx.author.name}")
except Exception as e:
if interaction.response.is_done():
await interaction.followup.send(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
else:
await interaction.response.send_message(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
await ctx.send(f"Có lỗi xảy ra: {str(e)}")
+3 -8
View File
@@ -1,10 +1,8 @@
import discord
import os
import asyncio
from core.bot import bot
from discord.ext import commands
from typing import Dict, Set, Optional
from datetime import datetime, timedelta
from core.bot import bot, CHANNEL_NOI_TU_ID
from typing import Set
from datetime import datetime
from apps.currency import incr
# Lazy load repository để tránh lỗi database connection
@@ -18,9 +16,6 @@ def get_noi_tu_repo():
noi_tu_repo = NoiTuRepository()
return noi_tu_repo
# Lấy channel ID từ environment
CHANNEL_NOI_TU_ID = int(os.getenv('CHANNEL_NOI_TU_ID', 0))
# Game state
class NoiTuGame:
def __init__(self):
-48
View File
@@ -1,48 +0,0 @@
import discord
from core.bot import bot, server_repo
@bot.tree.command(name='help', description='Show help')
async def help(interaction: discord.Interaction):
"""Command để hiển thị danh sách các lệnh"""
# Get all commands
commands = bot.tree.get_commands()
command_list = "\n".join([f"{c.name}: {c.description}" for c in commands])
# Create embed with command list
embed = discord.Embed(title="Help", description="Đây là danh sách các lệnh bạn có thể sử dụng:")
embed.add_field(name="Danh sách lệnh", value=command_list, inline=False)
await interaction.response.send_message(embed=embed, ephemeral=False)
print(f"Đã gửi lệnh help cho {interaction.user.name}")
@bot.tree.command(name="init_server", description="Khởi tạo thông tin server trong database")
async def init_server(interaction: discord.Interaction):
"""Command để khởi tạo thông tin server trong database"""
try:
# Kiểm tra quyền admin
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message("Bạn cần có quyền Administrator để sử dụng lệnh này!", ephemeral=True)
return
# Gửi response ngay lập tức
await interaction.response.defer(ephemeral=True)
# Kiểm tra xem server đã tồn tại trong database chưa
existing_server = await server_repo.get_server(interaction.guild_id)
if not existing_server:
# Tạo record mới cho server
await server_repo.create_server(interaction.guild_id, interaction.guild.name)
await interaction.followup.send(f"Đã thêm server {interaction.guild.name} vào database!", ephemeral=True)
else:
# Cập nhật tên server nếu có thay đổi
if existing_server.name != interaction.guild.name:
await server_repo.update_server(interaction.guild_id, interaction.guild.name)
await interaction.followup.send(f"Đã cập nhật tên server trong database!", ephemeral=True)
else:
await interaction.followup.send("Server đã tồn tại trong database!", ephemeral=True)
except Exception as e:
# Nếu chưa gửi response, gửi response lỗi
if not interaction.response.is_done():
await interaction.response.send_message(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)
else:
# Nếu đã gửi response, gửi followup
await interaction.followup.send(f"Có lỗi xảy ra: {str(e)}", ephemeral=True)