diff --git a/discord-bot/.env.example b/discord-bot/.env.example index 7c414a53..d32e80d1 100644 --- a/discord-bot/.env.example +++ b/discord-bot/.env.example @@ -1,4 +1,4 @@ TOKEN= DECLARE_GLOBAL_COMMANDS= -OWNER_IDS= +OWNER_IDS=[, ] PREFIX="./" diff --git a/discord-bot/bot/bot.py b/discord-bot/bot/bot.py index a328300a..2cf3d663 100644 --- a/discord-bot/bot/bot.py +++ b/discord-bot/bot/bot.py @@ -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, ) diff --git a/discord-bot/bot/config.py b/discord-bot/bot/config.py deleted file mode 100644 index fafcb308..00000000 --- a/discord-bot/bot/config.py +++ /dev/null @@ -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", "./"), - ) diff --git a/discord-bot/bot/settings.py b/discord-bot/bot/settings.py new file mode 100644 index 00000000..41c6ae52 --- /dev/null +++ b/discord-bot/bot/settings.py @@ -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" diff --git a/discord-bot/requirements.txt b/discord-bot/requirements.txt index 62b9b931..372bbd59 100644 --- a/discord-bot/requirements.txt +++ b/discord-bot/requirements.txt @@ -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