Python SDK for Discord Bot Orchestrator
Project description
Discord Bot Orchestrator SDK
A Python SDK for interacting with the Discord Bot Orchestrator API. This SDK provides a clean, modular, and easy-to-use interface for managing Discord bots programmatically.
Installation
pip install discord-orchestrator-sdk
Quick Start
from discord_orchestrator import OrchestratorClient
# Initialize the client
client = OrchestratorClient(
base_url="http://localhost:8000",
api_key="orc_your_api_key_here"
)
# Check system health
health = client.health.check()
print(f"Status: {health.status}, Running bots: {health.bots_running}")
# List all bots
bots = client.bots.list()
for bot in bots:
print(f"{bot.name}: {bot.status}")
# Create a new bot
bot = client.bots.create(
name="MyAutomationBot",
discord_token="your_discord_token"
)
# Start the bot
bot.start()
# Execute a command
result = bot.execute(
"send_message",
channel_id="123456789",
content="Hello from the SDK!"
)
print(f"Result: {result.status}")
# Clean up
client.close()
Features
Bot Management
# List bots
bots = client.bots.list()
# Get a specific bot
bot = client.bots.get(bot_id=1)
# Create a bot
bot = client.bots.create(
name="MyBot",
discord_token="...",
config={"auto_restart": True}
)
# Update a bot
bot = client.bots.update(bot_id=1, name="NewName")
# Delete a bot
client.bots.delete(bot_id=1)
# Lifecycle management
bot.start()
bot.stop()
bot.restart()
# Get detailed status
status = bot.status()
print(f"PID: {status.process_pid}, Connected: {status.is_connected}")
Idempotent Operations (for Automation)
# Get or create a bot - safe to call multiple times
bot = client.bots.get_or_create(
name="MyAutomationBot",
discord_token="..." # Only used if creating
)
# Find bot by name (returns None if not found)
bot = client.bots.find_by_name("MyBot")
# Check if bot exists
if client.bots.exists(name="MyBot"):
print("Bot exists!")
# Ensure bot is running (idempotent)
bot.ensure_running() # Starts if stopped, no-op if running
Command Execution
# Synchronous execution
result = client.commands.execute(
bot_id=1,
action="send_message",
payload={"channel_id": "123", "content": "Hello!"},
timeout=30.0
)
print(f"Status: {result.status}, Data: {result.data}")
# Asynchronous execution
async_result = client.commands.execute_async(
bot_id=1,
action="long_running_task",
payload={"param": "value"}
)
print(f"Tracking ID: {async_result.correlation_id}")
# Command history
history = client.commands.history(bot_id=1, limit=10)
for cmd in history:
print(f"{cmd.action}: {cmd.status}")
# Via bot object
result = bot.execute("send_message", channel_id="123", content="Hello!")
Metrics
# Get bot metrics
metrics = client.metrics.get(bot_id=1, period="24h")
for point in metrics["metrics"]:
print(f"CPU: {point['cpu_percent']}%, Memory: {point['memory_mb']}MB")
# Get latest metrics
latest = client.metrics.latest(bot_id=1)
# Uptime statistics
uptime = client.metrics.uptime(bot_id=1, period="7d")
print(f"Uptime: {uptime['stats']['uptime_percent']}%")
# System-wide summary
summary = client.metrics.summary()
print(f"Total bots: {summary['totals']['total_bots']}")
Slash Commands
# Create a slash command (idempotent)
cmd = client.slash_commands.get_or_create(
bot_id=bot.id,
name="ask",
description="Ask a question",
options=[
{"name": "question", "description": "Your question", "type": 3, "required": True}
]
)
# Sync commands to Discord (registers them)
result = client.slash_commands.sync(bot_id=bot.id)
print(f"Synced {result.synced_count} commands")
# List all slash commands
commands = client.slash_commands.list(bot_id=bot.id)
# Update a command
client.slash_commands.update(
bot_id=bot.id,
command_id=cmd.id,
description="Ask me anything!"
)
# Delete a command
client.slash_commands.delete(bot_id=bot.id, command_id=cmd.id)
Real-Time Events
from discord_orchestrator import OrchestratorClient
from discord_orchestrator.realtime import RealtimeClient
client = OrchestratorClient(
base_url="http://localhost:8000",
api_key="orc_your_api_key"
)
# Get your bot
bot = client.bots.get_or_create(name="MyBot")
bot.ensure_running()
# Connect to real-time events
realtime = RealtimeClient(client.config)
realtime.connect()
realtime.subscribe(bot.id)
# Handle slash command events
@realtime.on_slash_command
def handle_command(event):
print(f"Command: {event.command_name}")
print(f"User: {event.user.username}")
print(f"Options: {event.options}")
# Respond to the command
bot.respond_to_command(
interaction_id=event.interaction_id,
content="Hello from the SDK!"
)
# Handle all events
@realtime.on_event
def handle_event(event):
print(f"Event type: {event.event_type}")
# Wait for events (blocks)
realtime.wait()
# Or disconnect when done
realtime.disconnect()
Webhooks
# List webhooks
webhooks = client.webhooks.list()
# Create a webhook
webhook = client.webhooks.create(
name="Status Notifications",
url="https://example.com/webhook",
events=["bot.started", "bot.stopped", "bot.error"]
)
print(f"Secret: {webhook.secret}") # Save this!
# Update a webhook
webhook.update(is_active=False)
# Test a webhook
result = webhook.test()
print(f"Success: {result['success']}")
# Get delivery history
deliveries = webhook.deliveries(limit=20)
for d in deliveries:
print(f"{d.event_type}: {d.success}")
# Available event types
events = client.webhooks.events()
for e in events:
print(f"{e.type}: {e.description}")
Health Checks
# Full health check
health = client.health.check()
print(f"Status: {health.status}")
print(f"Database: {health.database}")
print(f"Bots: {health.bots_running}/{health.bots_total}")
if health.is_healthy:
print("All systems operational")
# Readiness check (for load balancers)
if client.health.ready():
print("Ready to accept requests")
# Liveness check (for container orchestrators)
if client.health.live():
print("Service is alive")
Error Handling
from discord_orchestrator import (
OrchestratorError,
AuthenticationError,
NotFoundError,
ValidationError,
TimeoutError,
)
try:
bot = client.bots.get(999)
except NotFoundError:
print("Bot not found")
except AuthenticationError:
print("Invalid API key")
except ValidationError as e:
print(f"Validation error: {e.details}")
except TimeoutError:
print("Request timed out")
except OrchestratorError as e:
print(f"Error: {e.message}, Code: {e.code}")
Context Manager
# Automatically close the client
with OrchestratorClient(
base_url="http://localhost:8000",
api_key="..."
) as client:
bots = client.bots.list()
# Client is automatically closed when exiting the block
Configuration
from discord_orchestrator import OrchestratorClient, OrchestratorConfig
# Full configuration
client = OrchestratorClient(
base_url="http://localhost:8000",
api_key="orc_your_api_key",
timeout=30.0, # Request timeout in seconds
verify_ssl=True, # Verify SSL certificates
max_retries=3, # Max retry attempts
retry_delay=1.0, # Base delay between retries
user_context="user-123", # Optional: Multi-tenant user isolation
)
# Or use config object
config = OrchestratorConfig(
base_url="http://localhost:8000",
api_key="orc_your_api_key"
)
Automation Integration Example
from discord_orchestrator import OrchestratorClient
def send_discord_message(channel_id: str, content: str, user_id: str) -> dict:
"""Send a Discord message with multi-tenant isolation.
Args:
channel_id: Discord channel ID
content: Message content
user_id: User ID for multi-tenant isolation
"""
with OrchestratorClient(
base_url="http://localhost:8000",
api_key="orc_your_api_key",
user_context=user_id # Each user's bots are isolated
) as client:
# Get or create the bot (idempotent)
# Bot names are unique per user, not globally
bot = client.bots.get_or_create(
name="MyBot",
discord_token="your_bot_token"
)
# Ensure it's running
bot.ensure_running()
# Send the message
result = bot.execute(
"send_message",
channel_id=channel_id,
content=content
)
return {
"success": result.status == "success",
"data": result.data
}
API Reference
OrchestratorClient
The main entry point for the SDK.
bots: Bot management operationscommands: Command execution operationsinteractions: Interaction response operationsmetrics: Metrics query operationsslash_commands: Slash command CRUD operationswebhooks: Webhook management operationshealth: Health check operations
BotsResource
list()- List all botsget(bot_id)- Get bot by IDcreate(name, discord_token, config)- Create a botupdate(bot_id, ...)- Update a botdelete(bot_id)- Delete a botstart(bot_id)- Start a botstop(bot_id)- Stop a botrestart(bot_id)- Restart a botstatus(bot_id)- Get detailed statusfind_by_name(name)- Find bot by nameget_by_name(name)- Get bot by name (raises if not found)exists(name=None, bot_id=None)- Check if bot existsget_or_create(name, discord_token, config)- Idempotent get/create
BotInstance
- All
Botmodel attributes (id, name, status, owner_id, etc.) start(),stop(),restart()- Lifecycle managementstatus()- Get detailed statusexecute(action, **payload)- Execute commandexecute_async(action, **payload)- Execute async commandmetrics(period)- Get metricsuptime(period)- Get uptime statsrefresh()- Refresh data from serverensure_running(timeout)- Ensure bot is runningupdate(...)- Update botdelete()- Delete botrespond_to_command(interaction_id, content, embeds, ephemeral)- Respond to slash command
CommandsResource
execute(bot_id, action, payload, timeout)- Sync executionexecute_async(bot_id, action, payload)- Async executionhistory(bot_id, action, status, limit, offset)- Query historyhistory_for_bot(bot_id, limit, offset)- Bot-specific historyget_history_detail(history_id)- Get execution details
MetricsResource
get(bot_id, period, resolution)- Get historical metricslatest(bot_id)- Get latest metricsuptime(bot_id, period)- Get uptime statisticssummary()- Get system-wide summary
WebhooksResource
list()- List all webhooksget(webhook_id)- Get webhook by IDcreate(name, url, events, headers)- Create webhookupdate(webhook_id, ...)- Update webhookdelete(webhook_id)- Delete webhooktest(webhook_id)- Send test eventregenerate_secret(webhook_id)- Regenerate signing secretdeliveries(webhook_id, limit, offset)- Get delivery historyevents()- List available event types
HealthResource
check()- Full health checkready()- Readiness checklive()- Liveness check
SlashCommandsResource
list(bot_id)- List all slash commands for a botcreate(bot_id, name, description, options)- Create a slash commandget(bot_id, command_id)- Get a specific commandupdate(bot_id, command_id, ...)- Update a commanddelete(bot_id, command_id)- Delete a commandsync(bot_id)- Sync commands to Discordfind_by_name(bot_id, name)- Find command by nameget_or_create(bot_id, name, description, options)- Idempotent get/createensure(bot_id, name, description, options)- Create or update to match definition
InteractionsResource
respond(bot_id, interaction_id, content, embeds, ephemeral)- Respond to a slash command
RealtimeClient
connect()- Connect to orchestrator WebSocketdisconnect()- Disconnect from WebSocketsubscribe(bot_id)- Subscribe to events for a botunsubscribe(bot_id)- Unsubscribe from bot eventson_slash_command(handler)- Register slash command handler (decorator)on_event(handler)- Register generic event handler (decorator)on_error(handler)- Register error handler (decorator)wait(timeout)- Wait for events (blocking)is_connected- Check connection status
License
MIT License
Project details
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 discord_orchestrator_sdk-0.1.1.tar.gz.
File metadata
- Download URL: discord_orchestrator_sdk-0.1.1.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1d940c8dbc3dfb233aee4f38320017a52cadc966f5448d4578a448ca828cf18
|
|
| MD5 |
393815ef7e14c9488abc53e3dfbd12c8
|
|
| BLAKE2b-256 |
72088bd33e75ae80b7f2e19bda53ea7309fa625b241f2044eed9aad281e41c6f
|
File details
Details for the file discord_orchestrator_sdk-0.1.1-py3-none-any.whl.
File metadata
- Download URL: discord_orchestrator_sdk-0.1.1-py3-none-any.whl
- Upload date:
- Size: 33.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70eb18622a0eb6ad603f72d2801e87a2c2855a86d73af263098f8717551a5ca1
|
|
| MD5 |
3cde716236f81fd354c889e669fb86ef
|
|
| BLAKE2b-256 |
eb566909800b7e8ca7de5ae2611b3d8a96de40013696071739c535797fb6b6f4
|