disagreement/examples/voice_bot.py
Slipstreamm b039b2e948 refactor(init): Consolidate module imports and exports
This commit refactors the `disagreement/__init__.py` file to import and export new models, enums, and components.

The primary changes are:
- Add imports and exports for `Member`, `Role`, `Attachment`, `Channel`, `ActionRow`, `Button`, `SelectOption`, `SelectMenu`, `Embed`, `PartialEmoji`, `Section`, `TextDisplay`, `Thumbnail`, `UnfurledMediaItem`, `MediaGallery`, `MediaGalleryItem`, `Container`, and `Guild` from `disagreement.models`.
- Add imports and exports for `ButtonStyle`, `ChannelType`, `MessageFlags`, `InteractionType`, `InteractionCallbackType`, and `ComponentType` from `disagreement.enums`.
- Add `Interaction` from `disagreement.interactions`.
- Add `ui` and `ext` as top-level modules.
- Update `disagreement.ext/__init__.py` to expose `app_commands`, `commands`, and `tasks`.

These changes consolidate the library's public API, making new features more accessible.
The example files were also updated to use the direct imports from the `disagreement` package or its `ext` subpackage, improving readability and consistency.
2025-06-14 18:17:57 -06:00

54 lines
1.3 KiB
Python

"""Example bot demonstrating voice channel playback."""
import os
import asyncio
import sys
# If running from the examples directory
if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__file__)):
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from typing import cast
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
if load_dotenv:
load_dotenv()
_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
_GUILD_ID = os.getenv("DISCORD_GUILD_ID")
_CHANNEL_ID = os.getenv("DISCORD_VOICE_CHANNEL")
if not all([_TOKEN, _GUILD_ID, _CHANNEL_ID]):
print("Missing one or more required environment variables for voice connection")
sys.exit(1)
assert _TOKEN
assert _GUILD_ID
assert _CHANNEL_ID
TOKEN = cast(str, _TOKEN)
GUILD_ID = cast(str, _GUILD_ID)
CHANNEL_ID = cast(str, _CHANNEL_ID)
async def main() -> None:
client = Client(TOKEN)
await client.connect()
voice = await client.join_voice(GUILD_ID, CHANNEL_ID)
try:
await voice.play_file("welcome.mp3")
finally:
await voice.close()
await client.close()
if __name__ == "__main__":
asyncio.run(main())