Skip to content

fix: bot configuration loading from environment variables#131

Merged
JacobCoffee merged 2 commits intomainfrom
feature/fix-bot-config
Nov 23, 2025
Merged

fix: bot configuration loading from environment variables#131
JacobCoffee merged 2 commits intomainfrom
feature/fix-bot-config

Conversation

@JacobCoffee
Copy link
Owner

Summary

Fixed bot service startup failure caused by improper environment variable loading in BotSettings and LogSettings.

Changes

  • Configuration Loading Fix (services/bot/src/byte_bot/config.py):

    • Removed env_prefix="BOT_" from BotSettings to allow direct environment variable reading
    • Removed env_prefix="LOG_" from LogSettings for consistency
    • Changed load_settings() to use default constructor instead of model_validate({})
    • Added explicit validation_alias to critical fields for clarity
    • Used Field(...) for required discord_token to satisfy type checker
  • Tests (tests/unit/bot/test_config.py):

    • Added comprehensive test coverage for configuration loading
    • Tests verify environment variable loading, command prefix assembly, presence URL, and log settings
    • All 6 new tests pass, total test suite: 1042 tests passing

Root Cause

The load_settings() function was calling BotSettings.model_validate({}) with an empty dictionary, which prevented Pydantic Settings from reading environment variables. Additionally, the env_prefix="BOT_" was causing Pydantic to look for BOT_DISCORD_TOKEN instead of DISCORD_TOKEN.

Verification

  • All CI checks pass (lint, format, type-check, test)
  • Bot successfully loads configuration from .env file
  • Bot starts without validation errors (fails later on fake token, as expected)
  • Tests confirm proper environment variable loading

Test Plan

  • Run make ci - all checks pass
  • Test configuration loading: uv run python -c "from byte_bot.config import bot_settings"
  • Test bot startup: make run-dev-bot (loads config successfully)

🤖 Generated with Claude Code

JacobCoffee and others added 2 commits November 23, 2025 16:40
Changes:
- Removed env_prefix from BotSettings and LogSettings to allow direct env var reading
- Changed load_settings() to use default constructor instead of model_validate({})
- Added explicit validation_alias to fields for clarity
- Removed redundant discord_token field validator that was causing validation errors
- Used Field(...) for required discord_token to satisfy type checker

The issue was that model_validate({}) bypasses environment variable loading,
and the env_prefix="BOT_" was causing Pydantic to look for BOT_DISCORD_TOKEN
instead of DISCORD_TOKEN.

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

Co-Authored-By: Claude <noreply@anthropic.com>
Added comprehensive tests for BotSettings and LogSettings to ensure:
- Environment variables are properly loaded
- Command prefix is assembled based on environment
- Presence URL is assembled correctly
- Log settings have appropriate defaults
- load_settings() returns correct tuple types

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

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @JacobCoffee, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@railway-app
Copy link

railway-app bot commented Nov 23, 2025

🚅 Deployed to the byte-pr-131 environment in byte

Service Status Web Updated (UTC)
byte ◻️ Removed (View Logs) Nov 23, 2025 at 10:47 pm

@railway-app railway-app bot temporarily deployed to byte (byte / byte-pr-131) November 23, 2025 22:44 Destroyed
@github-actions
Copy link

Documentation preview will be available shortly at https://jacobcoffee.github.io/byte-docs-preview/131

@JacobCoffee
Copy link
Owner Author

i dont think i agree with this at fucking all but its not my say im just watching :)

@JacobCoffee JacobCoffee merged commit 42ed8cf into main Nov 23, 2025
4 checks passed
@JacobCoffee JacobCoffee deleted the feature/fix-bot-config branch November 23, 2025 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant