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
|
# Configure logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
level=logging.INFO
|
level=logging.DEBUG
|
||||||
)
|
)
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -183,6 +183,8 @@ class LichessBot:
|
||||||
user_id = update.effective_user.id
|
user_id = update.effective_user.id
|
||||||
gamers = self.db.get_user_gamers(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:
|
if not gamers:
|
||||||
await update.message.reply_text("📭 В базе нет игроков. Используйте /adduser для добавления.")
|
await update.message.reply_text("📭 В базе нет игроков. Используйте /adduser для добавления.")
|
||||||
return
|
return
|
||||||
|
|
@ -192,34 +194,54 @@ class LichessBot:
|
||||||
|
|
||||||
# Prepare data for each gamer
|
# Prepare data for each gamer
|
||||||
gamers_data = []
|
gamers_data = []
|
||||||
for gamer in gamers:
|
for i, gamer in enumerate(gamers):
|
||||||
status = "🟢" if gamer['is_active'] else "⚪"
|
try:
|
||||||
username = gamer['username']
|
logger.info(f"Processing gamer {i+1}/{len(gamers)}: {gamer['username']} (ID: {gamer['id']})")
|
||||||
|
status = "🟢" if gamer['is_active'] else "⚪"
|
||||||
# Get user ratings from Lichess API
|
username = gamer['username']
|
||||||
ratings_data = await self.lichess_api.get_user_ratings(username)
|
|
||||||
|
# Get user ratings from Lichess API
|
||||||
if ratings_data and 'perfs' in ratings_data:
|
logger.debug(f"Requesting ratings for {username}")
|
||||||
perfs = ratings_data['perfs']
|
ratings_data = await self.lichess_api.get_user_ratings(username)
|
||||||
bullet_rating = perfs.get('bullet', {}).get('rating', 'N/A')
|
logger.debug(f"Received ratings for {username}: {ratings_data is not None}")
|
||||||
blitz_rating = perfs.get('blitz', {}).get('rating', 'N/A')
|
|
||||||
rapid_rating = perfs.get('rapid', {}).get('rating', 'N/A')
|
if ratings_data and 'perfs' in ratings_data:
|
||||||
else:
|
perfs = ratings_data['perfs']
|
||||||
bullet_rating = blitz_rating = rapid_rating = 'N/A'
|
bullet_rating = perfs.get('bullet', {}).get('rating', 'N/A')
|
||||||
|
blitz_rating = perfs.get('blitz', {}).get('rating', 'N/A')
|
||||||
# Add period information if period > 0
|
rapid_rating = perfs.get('rapid', {}).get('rating', 'N/A')
|
||||||
period_minutes = gamer.get('period_minutes', 0)
|
else:
|
||||||
period_text = f" · {period_minutes}м" if period_minutes > 0 else ""
|
bullet_rating = blitz_rating = rapid_rating = 'N/A'
|
||||||
|
|
||||||
gamers_data.append({
|
# Add period information if period > 0
|
||||||
'id': gamer['id'],
|
period_minutes = gamer.get('period_minutes', 0)
|
||||||
'status': status,
|
period_text = f" · {period_minutes}м" if period_minutes > 0 else ""
|
||||||
'username': username,
|
|
||||||
'bullet': bullet_rating,
|
gamers_data.append({
|
||||||
'blitz': blitz_rating,
|
'id': gamer['id'],
|
||||||
'rapid': rapid_rating,
|
'status': status,
|
||||||
'period': period_text
|
'username': username,
|
||||||
})
|
'bullet': bullet_rating,
|
||||||
|
'blitz': blitz_rating,
|
||||||
|
'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
|
# Create text message with stats
|
||||||
text_lines = []
|
text_lines = []
|
||||||
|
|
@ -229,8 +251,11 @@ class LichessBot:
|
||||||
f"⚡ {gamer['bullet']} 🔥 {gamer['blitz']} 🐇 {gamer['rapid']}{gamer['period']}"
|
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)
|
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
|
# Create simple keyboard with just usernames
|
||||||
keyboard = []
|
keyboard = []
|
||||||
for gamer in gamers_data:
|
for gamer in gamers_data:
|
||||||
|
|
@ -238,7 +263,9 @@ class LichessBot:
|
||||||
text=f"{gamer['status']} {gamer['username']}",
|
text=f"{gamer['status']} {gamer['username']}",
|
||||||
callback_data=f"select_{gamer['id']}"
|
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)
|
reply_markup = InlineKeyboardMarkup(keyboard)
|
||||||
|
|
||||||
# Edit the loading message with the results
|
# Edit the loading message with the results
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue