adding debt app
This commit is contained in:
+2
-2
@@ -16,12 +16,12 @@ intents.guilds = True
|
||||
bot = commands.Bot(command_prefix='!', intents=intents)
|
||||
|
||||
# Initialize repositories
|
||||
from repositories import ServerRepository, ChannelRepository, ChannelAppRepository, UserRepository
|
||||
from repositories import ServerRepository, ChannelRepository, ChannelAppRepository, HomeDebtRepository
|
||||
|
||||
server_repo = ServerRepository()
|
||||
channel_repo = ChannelRepository()
|
||||
channel_app_repo = ChannelAppRepository()
|
||||
user_repo = UserRepository()
|
||||
home_debt_repo = HomeDebtRepository()
|
||||
|
||||
# Store user cooldowns
|
||||
from typing import Dict, Set
|
||||
|
||||
+5
-55
@@ -1,58 +1,8 @@
|
||||
from datetime import datetime
|
||||
from .bot import bot, user_repo, user_cooldowns, active_voice_users, CHAT_EXP_POINTS, CHAT_COOLDOWN, VOICE_EXP_POINTS_PER_MINUTE
|
||||
from core.bot import bot
|
||||
from core.tasks import sync_commands
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print(f'{bot.user} has connected to Discord!')
|
||||
from .tasks import update_voice_exp
|
||||
update_voice_exp.start()
|
||||
try:
|
||||
synced = await bot.tree.sync()
|
||||
print(f"Synced {len(synced)} command(s)")
|
||||
except Exception as e:
|
||||
print(f"Failed to sync commands: {e}")
|
||||
|
||||
@bot.event
|
||||
async def on_message(message):
|
||||
if message.author.bot:
|
||||
return
|
||||
|
||||
# Handle chat experience points
|
||||
user_id = message.author.id
|
||||
current_time = datetime.now()
|
||||
|
||||
if user_id not in user_cooldowns or (current_time - user_cooldowns[user_id]).total_seconds() >= CHAT_COOLDOWN:
|
||||
current_exp = await user_repo.get_exp(user_id)
|
||||
new_exp = current_exp + CHAT_EXP_POINTS
|
||||
await user_repo.update_exp(user_id, new_exp)
|
||||
user_cooldowns[user_id] = current_time
|
||||
print(f"Added {CHAT_EXP_POINTS} exp points to {message.author.name}. Total: {new_exp}")
|
||||
|
||||
await bot.process_commands(message)
|
||||
|
||||
@bot.event
|
||||
async def on_voice_state_update(member, before, after):
|
||||
if member.bot:
|
||||
return
|
||||
|
||||
# User joined a voice channel
|
||||
if before.channel is None and after.channel is not None:
|
||||
current_time = datetime.now().isoformat()
|
||||
await user_repo.update_voice_time(member.id, current_time)
|
||||
active_voice_users.add(member.id)
|
||||
print(f"{member.name} joined voice channel {after.channel.name}")
|
||||
|
||||
# User left a voice channel
|
||||
elif before.channel is not None and after.channel is None:
|
||||
if member.id in active_voice_users:
|
||||
active_voice_users.remove(member.id)
|
||||
last_join_time = await user_repo.get_voice_time(member.id)
|
||||
if last_join_time:
|
||||
join_time = datetime.fromisoformat(last_join_time)
|
||||
time_spent = datetime.now() - join_time
|
||||
minutes = int(time_spent.total_seconds() / 60)
|
||||
exp_points = minutes * VOICE_EXP_POINTS_PER_MINUTE
|
||||
current_exp = await user_repo.get_exp(member.id)
|
||||
new_exp = current_exp + exp_points
|
||||
await user_repo.update_exp(member.id, new_exp)
|
||||
print(f"{member.name} spent {minutes} minutes in voice. Added {exp_points} exp points. Total: {new_exp}")
|
||||
print(f'{bot.user} đã tham gia vào server!')
|
||||
await sync_commands.start()
|
||||
print("Đã đồng bộ lệnh!")
|
||||
+19
-15
@@ -1,18 +1,22 @@
|
||||
from discord.ext import tasks
|
||||
from datetime import datetime
|
||||
from .bot import bot, user_repo, active_voice_users, VOICE_EXP_POINTS_PER_MINUTE
|
||||
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")
|
||||
|
||||
@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"Added {VOICE_EXP_POINTS_PER_MINUTE} exp points to user {user_id} for voice time")
|
||||
async def sync_commands():
|
||||
await bot.tree.sync()
|
||||
print("Đã đồng bộ lệnh!")
|
||||
Reference in New Issue
Block a user