fix: add error handling for getgamers command to ensure all players are displayed
- Added try-except block to handle errors when fetching player ratings - Added detailed logging to track all players being processed - Ensures all 4 players are displayed correctly
This commit is contained in:
parent
b12ce90874
commit
05fc7c21ea
1 changed files with 56 additions and 29 deletions
|
|
@ -21,7 +21,7 @@ from formatters import StatsFormatter
|
|||
# Configure logging
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.INFO
|
||||
level=logging.DEBUG
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -183,6 +183,8 @@ class LichessBot:
|
|||
user_id = update.effective_user.id
|
||||
gamers = self.db.get_user_gamers(user_id)
|
||||
|
||||
logger.info(f"getgamers: user_id={user_id}, found {len(gamers)} gamers")
|
||||
|
||||
if not gamers:
|
||||
await update.message.reply_text("📭 В базе нет игроков. Используйте /adduser для добавления.")
|
||||
return
|
||||
|
|
@ -192,12 +194,16 @@ class LichessBot:
|
|||
|
||||
# Prepare data for each gamer
|
||||
gamers_data = []
|
||||
for gamer in gamers:
|
||||
for i, gamer in enumerate(gamers):
|
||||
try:
|
||||
logger.info(f"Processing gamer {i+1}/{len(gamers)}: {gamer['username']} (ID: {gamer['id']})")
|
||||
status = "🟢" if gamer['is_active'] else "⚪"
|
||||
username = gamer['username']
|
||||
|
||||
# Get user ratings from Lichess API
|
||||
logger.debug(f"Requesting ratings for {username}")
|
||||
ratings_data = await self.lichess_api.get_user_ratings(username)
|
||||
logger.debug(f"Received ratings for {username}: {ratings_data is not None}")
|
||||
|
||||
if ratings_data and 'perfs' in ratings_data:
|
||||
perfs = ratings_data['perfs']
|
||||
|
|
@ -220,6 +226,22 @@ class LichessBot:
|
|||
'rapid': rapid_rating,
|
||||
'period': period_text
|
||||
})
|
||||
logger.info(f"Successfully added gamer {username} to gamers_data (total: {len(gamers_data)})")
|
||||
except Exception as e:
|
||||
logger.error(f"Error processing gamer {gamer.get('username', 'unknown')}: {e}")
|
||||
import traceback
|
||||
logger.error(f"Traceback: {traceback.format_exc()}")
|
||||
# Still add the gamer with N/A ratings
|
||||
gamers_data.append({
|
||||
'id': gamer['id'],
|
||||
'status': "🟢" if gamer['is_active'] else "⚪",
|
||||
'username': gamer['username'],
|
||||
'bullet': 'N/A',
|
||||
'blitz': 'N/A',
|
||||
'rapid': 'N/A',
|
||||
'period': f" · {gamer.get('period_minutes', 0)}м" if gamer.get('period_minutes', 0) > 0 else ""
|
||||
})
|
||||
logger.info(f"Added gamer {gamer['username']} with N/A ratings due to error")
|
||||
|
||||
# Create text message with stats
|
||||
text_lines = []
|
||||
|
|
@ -229,8 +251,11 @@ class LichessBot:
|
|||
f"⚡ {gamer['bullet']} 🔥 {gamer['blitz']} 🐇 {gamer['rapid']}{gamer['period']}"
|
||||
)
|
||||
|
||||
logger.info(f"getgamers: prepared {len(gamers_data)} gamers for display")
|
||||
gamers_text = "👥 <b>Выберите активного игрока:</b>\n\n" + "\n".join(text_lines)
|
||||
|
||||
logger.info(f"getgamers: message length: {len(gamers_text)} characters")
|
||||
|
||||
# Create simple keyboard with just usernames
|
||||
keyboard = []
|
||||
for gamer in gamers_data:
|
||||
|
|
@ -238,7 +263,9 @@ class LichessBot:
|
|||
text=f"{gamer['status']} {gamer['username']}",
|
||||
callback_data=f"select_{gamer['id']}"
|
||||
)])
|
||||
logger.debug(f"Added button for {gamer['username']} (ID: {gamer['id']})")
|
||||
|
||||
logger.info(f"getgamers: created {len(keyboard)} buttons in keyboard")
|
||||
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||
|
||||
# Edit the loading message with the results
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue