dhooks-lite
dhooks-lite is a lightweight Python wrapper for interacting with Discord webhooks, focusing on essential functionality for sending messages and embeds. It provides a simpler alternative to the original `dhooks` library. The current version is 1.1.0, and it has a moderate release cadence, with the last update in mid-2023.
Common errors
-
requests.exceptions.MissingSchema: Invalid URL "your_webhook_url": No schema supplied. Perhaps you meant https://your_webhook_url?
cause The provided webhook URL is missing the 'http://' or 'https://' scheme prefix.fixEnsure your webhook URL starts with `https://discord.com/api/webhooks/...`. -
dhooks_lite.webhook.WebhookException: (400) Bad Request
cause This error typically indicates that the payload sent to Discord (message content, embeds, etc.) is malformed or violates Discord's API rules. It could also mean the webhook token is incorrect.fixReview your `send()` arguments, especially any embed dictionaries, to ensure they adhere to Discord's API specifications. Verify the webhook URL and token are correct. -
AttributeError: module 'dhooks' has no attribute 'Webhook'
cause You are trying to import `Webhook` from the original `dhooks` library while intending to use `dhooks-lite`. These are separate packages.fixChange your import statement to `from dhooks_lite import Webhook`.
Warnings
- gotcha dhooks-lite is a separate and 'lite' version of the original `dhooks` library. It does not provide all features or the exact same API as `dhooks`. Be aware of potential API differences if migrating or expecting full feature parity.
- gotcha Unlike some other Discord libraries (e.g., `discord.py` or the full `dhooks`), `dhooks-lite` does not include a dedicated `Embed` class. When sending embeds, you must construct a Python dictionary that strictly conforms to Discord's Embed Object API specification.
- gotcha Providing an invalid or expired Discord webhook URL will lead to errors. Common issues include HTTP 400 (Bad Request), 404 (Not Found), or network connection errors.
Install
-
pip install dhooks-lite
Imports
- Webhook
from dhooks import Webhook
from dhooks_lite import Webhook
Quickstart
import os
from dhooks_lite import Webhook
# It's recommended to use an environment variable for security.
# Replace with your actual webhook URL or set DISCORD_WEBHOOK_URL in your environment.
webhook_url = os.environ.get(
"DISCORD_WEBHOOK_URL",
"https://discord.com/api/webhooks/YOUR_ID/YOUR_TOKEN" # Placeholder, replace with your actual URL
)
if "YOUR_ID" in webhook_url:
print("Warning: Please replace the placeholder webhook URL or set DISCORD_WEBHOOK_URL environment variable.")
# For demonstration, we'll proceed, but real use requires a valid URL.
try:
webhook = Webhook(webhook_url)
# 1. Send a simple text message
webhook.send("Hello from `dhooks-lite`! This is a simple message.")
print("Sent simple message.")
# 2. Send a message with an embed (embeds must be dicts conforming to Discord API)
embed_data = {
"title": "dhooks-lite Embed Example",
"description": "This embed demonstrates sending structured data.",
"color": 0x3498DB, # Hex color code (e.g., Discord's blurple)
"fields": [
{"name": "Feature 1", "value": "Lightweight", "inline": True},
{"name": "Feature 2", "value": "Easy to Use", "inline": True}
],
"footer": {"text": "Powered by dhooks-lite"},
"timestamp": "2023-10-27T10:00:00.000Z" # ISO 8601 timestamp
}
webhook.send(embeds=[embed_data])
print("Sent message with embed.")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your Discord webhook URL is valid and active.")