A Python library for the Discord API.
Project description
Disagreement
A Python library for interacting with the Discord API, with a focus on bot development.
Features
- Asynchronous design using
aiohttp - Gateway and HTTP API clients
- Slash command framework
- Message component helpers
- Built-in caching layer
- Experimental voice support
- Helpful error handling utilities
Installation
python -m pip install -U pip
pip install disagreement
# or install from source for development
pip install -e .
Requires Python 3.11 or newer.
Basic Usage
import asyncio
import os
import disagreement
# Ensure DISCORD_BOT_TOKEN is set in your environment
client = disagreement.Client(token=os.environ.get("DISCORD_BOT_TOKEN"))
@client.on_event('MESSAGE_CREATE')
async def on_message(message: disagreement.Message):
print(f"Received: {message.content} from {message.author.username}")
if message.content.lower() == '!ping':
await message.reply('Pong!')
async def main():
if not client.token:
print("Error: DISCORD_BOT_TOKEN environment variable not set.")
return
try:
async with client:
await asyncio.Future() # run until cancelled
except KeyboardInterrupt:
print("Bot shutting down...")
# Add any other specific exception handling from your library, e.g., disagreement.AuthenticationError
if __name__ == '__main__':
asyncio.run(main())
Global Error Handling
To ensure unexpected errors don't crash your bot, you can enable the library's global error handler:
import disagreement
disagreement.setup_global_error_handler()
Call this early in your program to log unhandled exceptions instead of letting them terminate the process.
Configuring Logging
Use :func:disagreement.logging_config.setup_logging to configure logging for
your bot. The helper accepts a logging level and an optional file path.
import logging
from disagreement.logging_config import setup_logging
setup_logging(logging.INFO)
# Or log to a file
setup_logging(logging.DEBUG, file="bot.log")
Defining Subcommands with AppCommandGroup
from disagreement.ext.app_commands import AppCommandGroup
settings = AppCommandGroup("settings", "Manage settings")
@settings.command(name="show")
async def show(ctx):
"""Displays a setting."""
...
@settings.group("admin", description="Admin settings")
def admin_group():
pass
@admin_group.command(name="set")
async def set_setting(ctx, key: str, value: str):
...
## Fetching Guilds
Use `Client.fetch_guild` to retrieve a guild from the Discord API if it
isn't already cached. This is useful when working with guild IDs from
outside the gateway events.
```python
guild = await client.fetch_guild("123456789012345678")
roles = await client.fetch_roles(guild.id)
Sharding
To run your bot across multiple gateway shards, pass shard_count when creating
the client:
client = disagreement.Client(token=BOT_TOKEN, shard_count=2)
See examples/sharded_bot.py for a full example.
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
See the docs directory for detailed guides on components, slash commands, caching, and voice features.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file disagreement-0.0.1.tar.gz.
File metadata
- Download URL: disagreement-0.0.1.tar.gz
- Upload date:
- Size: 89.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0d1cb5f657cebeb1651a97e4669c5d4e6264af9cfe92b0c401fd9887c205bfd
|
|
| MD5 |
a204b038d9da69264edb8ffbfc9bf713
|
|
| BLAKE2b-256 |
e81f4a8cdfe658b27968380ce41f1f91aee4d2aab70cce3214a45c97e884c55d
|
File details
Details for the file disagreement-0.0.1-py3-none-any.whl.
File metadata
- Download URL: disagreement-0.0.1-py3-none-any.whl
- Upload date:
- Size: 67.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2416abdb0852a2d23a09894589de4e085efe27830fc1b077b8880c99e1d30526
|
|
| MD5 |
23cf8e844970d8779ce904f109825e02
|
|
| BLAKE2b-256 |
317268f443bc067e6a202efdb81db9c30dd8aa3ccd4c6f3335dbb41e2b3a0c97
|