From 3158d76e90a0719f6f97b25e991aaa356d90b192 Mon Sep 17 00:00:00 2001 From: Slipstream Date: Wed, 11 Jun 2025 14:27:09 -0600 Subject: [PATCH] Document dotenv requirement and add optional handling (#51) --- README.md | 7 +++++++ examples/basic_bot.py | 9 +++++++-- examples/component_bot.py | 10 ++++++++-- examples/context_menus.py | 9 +++++++-- examples/extension_management.py | 9 +++++++-- examples/hybrid_bot.py | 9 +++++++-- examples/message_history.py | 10 ++++++++-- examples/modal_command.py | 10 ++++++++-- examples/modal_send.py | 9 +++++++-- examples/sharded_bot.py | 10 ++++++++-- examples/voice_bot.py | 9 +++++++-- 11 files changed, 81 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 3661be5..a99e865 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,13 @@ pip install -e . Requires Python 3.10 or newer. +To run the example scripts, you'll need the `python-dotenv` package to load +environment variables. Install the development extras with: + +```bash +pip install "disagreement[dev]" +``` + ## Basic Usage ```python diff --git a/examples/basic_bot.py b/examples/basic_bot.py index d2836f1..5ad01d4 100644 --- a/examples/basic_bot.py +++ b/examples/basic_bot.py @@ -39,9 +39,14 @@ except ImportError: ) sys.exit(1) -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") -load_dotenv() +if load_dotenv: + load_dotenv() # Optional: Configure logging for more insight, especially for gateway events # logging.basicConfig(level=logging.DEBUG) # For very verbose output diff --git a/examples/component_bot.py b/examples/component_bot.py index 60a8405..d2f9753 100644 --- a/examples/component_bot.py +++ b/examples/component_bot.py @@ -37,9 +37,15 @@ from disagreement.interactions import ( InteractionResponsePayload, InteractionCallbackData, ) -from dotenv import load_dotenv -load_dotenv() +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") + +if load_dotenv: + load_dotenv() # Get the bot token and application ID from the environment variables token = os.getenv("DISCORD_BOT_TOKEN") diff --git a/examples/context_menus.py b/examples/context_menus.py index 8ceb1e7..9ba3656 100644 --- a/examples/context_menus.py +++ b/examples/context_menus.py @@ -15,9 +15,14 @@ from disagreement.ext.app_commands import ( ) from disagreement.models import User, Message -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") -load_dotenv() +if load_dotenv: + load_dotenv() BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN", "") APP_ID = os.environ.get("DISCORD_APPLICATION_ID", "") diff --git a/examples/extension_management.py b/examples/extension_management.py index 8ba86cc..b128e08 100644 --- a/examples/extension_management.py +++ b/examples/extension_management.py @@ -4,7 +4,11 @@ import asyncio import os import sys -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") # Allow running from the examples folder without installing if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__file__)): @@ -12,7 +16,8 @@ if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__fi from disagreement import Client -load_dotenv() +if load_dotenv: + load_dotenv() TOKEN = os.environ.get("DISCORD_BOT_TOKEN") diff --git a/examples/hybrid_bot.py b/examples/hybrid_bot.py index c6d3bdd..d4aeae3 100644 --- a/examples/hybrid_bot.py +++ b/examples/hybrid_bot.py @@ -36,9 +36,14 @@ from disagreement.enums import ( logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") -load_dotenv() +if load_dotenv: + load_dotenv() # --- Define a Test Cog --- diff --git a/examples/message_history.py b/examples/message_history.py index 104f6fa..d5c6ffc 100644 --- a/examples/message_history.py +++ b/examples/message_history.py @@ -10,9 +10,15 @@ if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__fi from disagreement.client import Client from disagreement.models import TextChannel -from dotenv import load_dotenv -load_dotenv() +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") + +if load_dotenv: + load_dotenv() BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN", "") CHANNEL_ID = os.environ.get("DISCORD_CHANNEL_ID", "") diff --git a/examples/modal_command.py b/examples/modal_command.py index 120747f..17e5da8 100644 --- a/examples/modal_command.py +++ b/examples/modal_command.py @@ -2,14 +2,20 @@ import os import asyncio -from dotenv import load_dotenv + +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") from disagreement import Client, ui from disagreement.enums import GatewayIntent, TextInputStyle from disagreement.ext.app_commands.decorators import slash_command from disagreement.ext.app_commands.context import AppCommandContext -load_dotenv() +if load_dotenv: + load_dotenv() token = os.getenv("DISCORD_BOT_TOKEN", "") application_id = os.getenv("DISCORD_APPLICATION_ID", "") diff --git a/examples/modal_send.py b/examples/modal_send.py index 0b15489..8e91dba 100644 --- a/examples/modal_send.py +++ b/examples/modal_send.py @@ -3,7 +3,11 @@ import os import sys -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) @@ -11,7 +15,8 @@ from disagreement import Client, GatewayIntent, ui # type: ignore from disagreement.ext.app_commands.decorators import slash_command from disagreement.ext.app_commands.context import AppCommandContext -load_dotenv() +if load_dotenv: + load_dotenv() TOKEN = os.getenv("DISCORD_BOT_TOKEN", "") APP_ID = os.getenv("DISCORD_APPLICATION_ID", "") diff --git a/examples/sharded_bot.py b/examples/sharded_bot.py index be4a554..0d5e756 100644 --- a/examples/sharded_bot.py +++ b/examples/sharded_bot.py @@ -9,9 +9,15 @@ if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__fi sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import disagreement -from dotenv import load_dotenv -load_dotenv() +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") + +if load_dotenv: + load_dotenv() TOKEN = os.environ.get("DISCORD_BOT_TOKEN") if not TOKEN: diff --git a/examples/voice_bot.py b/examples/voice_bot.py index a9b4766..077fccd 100644 --- a/examples/voice_bot.py +++ b/examples/voice_bot.py @@ -10,11 +10,16 @@ if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__fi from typing import cast -from dotenv import load_dotenv +try: + from dotenv import load_dotenv +except ImportError: # pragma: no cover - example helper + load_dotenv = None + print("python-dotenv is not installed. Environment variables will not be loaded") import disagreement -load_dotenv() +if load_dotenv: + load_dotenv() _TOKEN = os.getenv("DISCORD_BOT_TOKEN") _GUILD_ID = os.getenv("DISCORD_GUILD_ID")