switch to using Pydantic for bot config

This commit is contained in:
Alex Ott
2022-12-30 05:48:56 -08:00
parent 6cccd74e34
commit 150fc67bfd
5 changed files with 24 additions and 46 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
TOKEN=<discord bot token>
DECLARE_GLOBAL_COMMANDS=<testing guild id>
OWNER_IDS=<your user id>
OWNER_IDS=[<your user id>, <other user ids>]
PREFIX="./"
+6 -6
View File
@@ -5,16 +5,16 @@ import hikari
import lightbulb
import miru
from bot.api_client import OasstApiClient
from bot.config import Config
from bot.settings import Settings
config = Config.from_env()
settings = Settings()
bot = lightbulb.BotApp(
token=config.token,
token=settings.token,
logs="DEBUG",
prefix=config.prefix,
default_enabled_guilds=config.declare_global_commands,
owner_ids=config.owner_ids,
prefix=settings.prefix,
default_enabled_guilds=settings.declare_global_commands,
owner_ids=settings.owner_ids,
intents=hikari.Intents.ALL,
)
-37
View File
@@ -1,37 +0,0 @@
# -*- coding: utf-8 -*-
"""Configuration for the bot."""
import logging
from dataclasses import dataclass
from os import getenv
from dotenv import load_dotenv # type: ignore
load_dotenv()
logger = logging.getLogger(__name__)
@dataclass
class Config:
"""Configuration for the bot."""
token: str
declare_global_commands: int
owner_ids: list[int]
prefix: str
@classmethod
def from_env(cls):
token = getenv("TOKEN", None)
if token is None:
logger.error("Invalid token, please set the TOKEN environment variable.")
exit(1)
return cls(
token=token,
declare_global_commands=int(getenv("DECLARE_GLOBAL_COMMANDS", 0)),
owner_ids=[int(x) for x in getenv("OWNER_IDS", "").split(",")],
prefix=getenv("PREFIX", "./"),
)
+15
View File
@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
"""Configuration for the bot."""
from pydantic import BaseSettings, Field
class Settings(BaseSettings):
"""Settings for the bot."""
token: str = Field(env="TOKEN", default="")
declare_global_commands: int = Field(env="DECLARE_GLOBAL_COMMANDS", default=0)
owner_ids: list[int] = Field(env="OWNER_IDS", default_factory=list)
prefix: str = Field(env="PREFIX", default="./")
class Config(BaseSettings.Config):
env_file = ".env"
+2 -2
View File
@@ -6,6 +6,6 @@ hikari-lightbulb # command handler
hikari-miru # modals and buttons
hikari[speedups]
loguru
pydantic
python-dotenv # .env file support
uvloop; os_name != 'nt'
uvloop; os_name != 'nt' # Faster drop-in replacement for asyncio event loop