feat: Tái cấu trúc bot sang kiến trúc cog, thêm hỗ trợ đa máy chủ, giới thiệu tính năng đăng ký bóng đá, giao diện web và quản lý cấu hình.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from repositories.config import ConfigRepository
|
||||
from infra.db import postgres
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv()
|
||||
|
||||
from repositories.guild import GuildRepository
|
||||
|
||||
class VirtusBot(commands.Bot):
|
||||
def __init__(self):
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
intents.members = True
|
||||
intents.voice_states = True
|
||||
intents.guilds = True
|
||||
|
||||
super().__init__(
|
||||
command_prefix='!',
|
||||
intents=intents,
|
||||
help_command=None # Disable default help command
|
||||
)
|
||||
|
||||
self.config_repo = ConfigRepository()
|
||||
self.guild_repo = GuildRepository()
|
||||
|
||||
async def setup_hook(self):
|
||||
# Database initialization
|
||||
await postgres.create_tables()
|
||||
|
||||
# Load Cogs
|
||||
await self.load_extension('bot.cogs.home_debt')
|
||||
await self.load_extension('bot.cogs.score')
|
||||
await self.load_extension('bot.cogs.noi_tu')
|
||||
await self.load_extension('bot.cogs.football')
|
||||
|
||||
# Sync Application Commands
|
||||
await self.tree.sync()
|
||||
print(f"Logged in as {self.user} (ID: {self.user.id})")
|
||||
|
||||
async def on_ready(self):
|
||||
print(f'{self.user} has connected to Discord!')
|
||||
# Register existing guilds on startup
|
||||
for guild in self.guilds:
|
||||
await self.guild_repo.create_or_update(guild.id, guild.name)
|
||||
print(f"✅ Registered Guild: {guild.name} ({guild.id})")
|
||||
|
||||
async def on_guild_join(self, guild):
|
||||
await self.guild_repo.create_or_update(guild.id, guild.name)
|
||||
print(f"👋 Joined new Guild: {guild.name} ({guild.id})")
|
||||
|
||||
bot = VirtusBot()
|
||||
@@ -0,0 +1,12 @@
|
||||
import discord
|
||||
from core.bot import bot
|
||||
# from core.tasks import sync_commands
|
||||
|
||||
# Using for sync commands to all guilds
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'{bot.user} đã tham gia vào server!')
|
||||
# await sync_commands.start()
|
||||
TEST_GUILD_ID = discord.Object(id=536422615649091595)
|
||||
await bot.tree.sync(guild=TEST_GUILD_ID)
|
||||
print("Đã đồng bộ lệnh!")
|
||||
@@ -0,0 +1,17 @@
|
||||
from discord.ext import tasks
|
||||
from core.bot import bot
|
||||
|
||||
# @tasks.loop(minutes=1)
|
||||
# async def update_voice_exp():
|
||||
# current_time = datetime.now()
|
||||
# for user_id in list(active_voice_users):
|
||||
# last_join_time = await user_repo.get_voice_time(user_id)
|
||||
# if last_join_time:
|
||||
# join_time = datetime.fromisoformat(last_join_time)
|
||||
# time_spent = current_time - join_time
|
||||
# if time_spent.total_seconds() >= 60:
|
||||
# current_exp = await user_repo.get_exp(user_id)
|
||||
# new_exp = current_exp + VOICE_EXP_POINTS_PER_MINUTE
|
||||
# await user_repo.update_exp(user_id, new_exp)
|
||||
# await user_repo.update_voice_time(user_id, current_time.isoformat())
|
||||
# print(f"Thêm {VOICE_EXP_POINTS_PER_MINUTE} điểm kinh nghiệm cho user {user_id} cho thời gian voice")
|
||||
Reference in New Issue
Block a user