добавлена проверка на существование добавляемого игрока
This commit is contained in:
parent
a5d6481495
commit
ecd252cdad
2 changed files with 45 additions and 18 deletions
|
|
@ -166,7 +166,20 @@ class LichessBot:
|
|||
username = update.message.text.strip()
|
||||
user_id = update.effective_user.id
|
||||
|
||||
if username:
|
||||
if not username:
|
||||
await update.message.reply_text(
|
||||
"❌ 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)
|
||||
|
|
@ -183,11 +196,6 @@ class LichessBot:
|
|||
await update.message.reply_text(
|
||||
f"✅ Игрок {username} успешно добавлен!"
|
||||
)
|
||||
else:
|
||||
await update.message.reply_text(
|
||||
"❌ Username не может быть пустым. Попробуйте еще раз."
|
||||
)
|
||||
return WAITING_FOR_USERNAME
|
||||
|
||||
return ConversationHandler.END
|
||||
|
||||
|
|
|
|||
|
|
@ -118,6 +118,25 @@ class LichessAPI:
|
|||
logger.error(f"Error getting puzzles period: {e}")
|
||||
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]]:
|
||||
"""Get user ratings from Lichess API"""
|
||||
try:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue