major-domo-v2/utils/team_utils.py
Cal Corum 2926664d2d CLAUDE: Remove constants.py and migrate to config-based constants
Eliminates redundant constants.py file by moving all constants to config.py.
All constants (except baseball positions) are now accessible via get_config().

Changes:
- config.py: Added baseball position sets as module-level constants
  * PITCHER_POSITIONS, POSITION_FIELDERS, ALL_POSITIONS remain static
  * All other constants now accessed via BotConfig instance

- constants.py: Deleted (redundant with config.py)

- Updated 27 files to use get_config() instead of constants module:
  * Commands: admin, help, league (3), players, profile, teams (3),
    transactions (3), utilities, voice
  * Services: league, player, team, trade_builder, transaction_builder
  * Utils: autocomplete, team_utils
  * Views: embeds
  * Tests: test_constants, test_services (3 files)
  * Examples: enhanced_player, migration_example

- tests/test_constants.py: Rewritten to test config values
  * All 14 tests pass
  * Now validates BotConfig defaults instead of constants module

Import Changes:
- Old: `from constants import SBA_CURRENT_SEASON`
- New: `from config import get_config` → `get_config().sba_current_season`
- Positions: `from config import PITCHER_POSITIONS, ALL_POSITIONS`

Benefits:
- Single source of truth (config.py only)
- Cleaner architecture - no redundant wrapper
- All constants configurable via environment variables
- Reduced maintenance overhead
- Type safety with Pydantic validation

All configuration tests pass. Core refactoring complete.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-16 10:52:05 -05:00

109 lines
3.0 KiB
Python

"""
Team Utilities
Common team-related helper functions used across commands.
"""
from typing import Optional
import discord
from models.team import Team
from services.team_service import team_service
async def get_user_major_league_team(
user_id: int,
season: int = get_config().sba_current_season
) -> Optional[Team]:
"""
Get the major league team owned by a Discord user.
This is a very common pattern used across many commands, so it's
extracted into a utility function for consistency and reusability.
Args:
user_id: Discord user ID
season: Season to check (defaults to current season)
Returns:
Team object if user owns a major league team, None otherwise
"""
try:
major_league_teams = await team_service.get_teams_by_owner(
user_id,
season,
roster_type="ml"
)
if major_league_teams:
return major_league_teams[0] # Return first ML team
return None
except Exception:
# Silently fail and return None - let calling code handle the error
return None
async def validate_user_has_team(
interaction: discord.Interaction,
season: int = get_config().sba_current_season
) -> Optional[Team]:
"""
Validate that a user has a major league team and send error message if not.
This combines team lookup with standard error messaging for consistency.
Args:
interaction: Discord interaction object
season: Season to check (defaults to current season)
Returns:
Team object if user has a team, None if not (error message already sent)
"""
user_team = await get_user_major_league_team(interaction.user.id, season)
if not user_team:
await interaction.followup.send(
"❌ You don't appear to own a major league team in the current season.",
ephemeral=True
)
return None
return user_team
async def get_team_by_abbrev_with_validation(
team_abbrev: str,
interaction: discord.Interaction,
season: int = get_config().sba_current_season
) -> Optional[Team]:
"""
Get a team by abbreviation with standard error messaging.
Args:
team_abbrev: Team abbreviation to look up
interaction: Discord interaction object for error messaging
season: Season to check (defaults to current season)
Returns:
Team object if found, None if not (error message already sent)
"""
from config import get_config
try:
team = await team_service.get_team_by_abbrev(team_abbrev, season)
if not team:
await interaction.followup.send(
f"❌ Team '{team_abbrev}' not found.",
ephemeral=True
)
return None
return team
except Exception:
await interaction.followup.send(
f"❌ Error looking up team '{team_abbrev}'. Please try again.",
ephemeral=True
)
return None