добавлена проверка на существование добавляемого игрока

This commit is contained in:
vrubelroman 2025-11-07 22:54:49 +03:00
parent a5d6481495
commit ecd252cdad
2 changed files with 45 additions and 18 deletions

View file

@ -166,29 +166,37 @@ class LichessBot:
username = update.message.text.strip() username = update.message.text.strip()
user_id = update.effective_user.id user_id = update.effective_user.id
if username: if not username:
# Add gamer to database (without token)
gamer_id = self.db.add_gamer(username)
# Link user to gamer (without token)
self.db.add_user_gamer(user_id, gamer_id, None)
# Set default period to 1 hour (60 minutes) for new gamer
self.db.set_user_gamer_period(user_id, gamer_id, 60)
# If this is the first gamer for this user, make it active
user_gamers = self.db.get_user_gamers(user_id)
if len(user_gamers) == 1:
self.db.set_user_active_gamer(user_id, gamer_id)
await update.message.reply_text(
f"✅ Игрок {username} успешно добавлен!"
)
else:
await update.message.reply_text( await update.message.reply_text(
"❌ Username не может быть пустым. Попробуйте еще раз." "❌ Username не может быть пустым. Попробуйте еще раз."
) )
return WAITING_FOR_USERNAME return WAITING_FOR_USERNAME
# Check if user exists on Lichess
user_exists = await self.lichess_api.check_user_exists(username)
if not user_exists:
await update.message.reply_text(
f"❌ Игрок {username} не найден на Lichess. Проверьте правильность написания имени."
)
return WAITING_FOR_USERNAME
# Add gamer to database (without token)
gamer_id = self.db.add_gamer(username)
# Link user to gamer (without token)
self.db.add_user_gamer(user_id, gamer_id, None)
# Set default period to 1 hour (60 minutes) for new gamer
self.db.set_user_gamer_period(user_id, gamer_id, 60)
# If this is the first gamer for this user, make it active
user_gamers = self.db.get_user_gamers(user_id)
if len(user_gamers) == 1:
self.db.set_user_active_gamer(user_id, gamer_id)
await update.message.reply_text(
f"✅ Игрок {username} успешно добавлен!"
)
return ConversationHandler.END return ConversationHandler.END
async def getgamers(self, update: Update, context: ContextTypes.DEFAULT_TYPE): async def getgamers(self, update: Update, context: ContextTypes.DEFAULT_TYPE):

View file

@ -118,6 +118,25 @@ class LichessAPI:
logger.error(f"Error getting puzzles period: {e}") logger.error(f"Error getting puzzles period: {e}")
return None return None
async def check_user_exists(self, username: str) -> bool:
"""Check if user exists on Lichess"""
try:
async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.lichess_base_url}/user/{username}"
) as response:
if response.status == 200:
return True
elif response.status == 404:
logger.warning(f"User {username} not found on Lichess (404)")
return False
else:
logger.error(f"Failed to check user existence: {response.status}")
return False
except Exception as e:
logger.error(f"Error checking user existence: {e}")
return False
async def get_user_ratings(self, username: str) -> Optional[Dict[str, Any]]: async def get_user_ratings(self, username: str) -> Optional[Dict[str, Any]]:
"""Get user ratings from Lichess API""" """Get user ratings from Lichess API"""
try: try: