A Python wrapper for Discord's Webhook Events.
Notice: this project is designed for the latest version of Python. Compatibility with older versions is not guaranteed.
First, install the dependencies:
pip install starlette uvicorn pynacl
Or use:
pip install -r requirements.txt
from webhook_events import Application app = Application(...)
Application takes two arguments:
-
url_path (str):
The URL path to the webhook endpoint that you've specified on your application's Developer Portal page -> 'Webhooks'.
For example, if your endpoint is https://quackbots.xyz/webhook1, thenurl_pathshould be /webhook1 (it depends on your server/file configuration). -
verify_key (str):
Your application's public key, used to verify Discord's request signature.
You can find this on your application's Developer Portal page -> 'General Information'.
from webhook_events import Application, events from datetime import datetime app = Application(...) @app.on_event(events.ApplicationAuthorized) async def foo(event: events.ApplicationAuthorized, time: datetime): ...
on_event is the decorator for registering a function to call when an event of the specified type is received.
This decorator takes a single positional argument:
An event object (class variable) of your choice (e.g. events.ApplicationAuthorized).
The function you use with this decorator must be a coroutine function (async def) that takes two arguments, in this order:
- An event object (e.g.
events.ApplicationAuthorized) identical to the one passed to the decorator. - A
datetime.datetimeobject, representing the time when the event occurred.
from webhook_events import Application, events, start_listening app = Application(...) # Implement event handler(s) start_listening(host="0.0.0.0", port=8080, applications=[app])
start_listening takes four keyword arguments:
- host (str):
The host to run the application on (e.g.0.0.0.0) - port (int):
The port to run the application on. - applications (list[Application]):
A list ofApplicationobjects to handle webhook events for. - basic_log (bool):
Whether to log the following:
- (info) Received a PING.
- (info) Received an event.
- (warning) Signature verification failed.
- (warning) Received an event without data (should never happen).
- (info) Received a PING.
from webhook_events import Application, events, start_listening from datetime import datetime app1 = Application(url_path="/webhook1", verify_key="banana") app2 = Application(url_path="/webhook2", verify_key="apple") @app1.on_event(events.ApplicationAuthorized) async def foo(event: events.ApplicationAuthorized, time: datetime): username = event.user.username print(f"{username} authorized app1 at {time}!") @app2.on_event(events.ApplicationDeauthorized) async def bar(event: events.ApplicationDeauthorized, time: datetime): username = event.user.username print(f"{username} deauthorized app2 at {time}.") start_listening(host="0.0.0.0", port=8080, applications=[app1, app2])
You can use the types module to directly access all objects, including nested objects, enums, etc.
Example:
from webhook_events.types import User, DisplayNameEffect def check_user(user: User) -> bool: if user.display_name_styles.effect == DisplayNameEffect.NEON: return True return False
Note: the types module is not sorted by categories, therefore users are expected to find what they need by name.