From a133ca87cfd8bef9da8c54968c5aecfd6d231380 Mon Sep 17 00:00:00 2001 From: Paillat-dev Date: Sun, 9 Mar 2025 16:25:17 +0100 Subject: [PATCH] :sparkles: Add webhook event handling for application authorization --- examples/webhook_events.py | 49 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 examples/webhook_events.py diff --git a/examples/webhook_events.py b/examples/webhook_events.py new file mode 100644 index 0000000..bc9c466 --- /dev/null +++ b/examples/webhook_events.py @@ -0,0 +1,49 @@ +# Copyright (c) Paillat-dev +# SPDX-License-Identifier: MIT + +"""Basic Discord bot example using Pycord REST. + +This is a minimal example showing how to create slash commands. +""" + +import logging +import os + +from dotenv import load_dotenv + +from pycord_rest import App, ApplicationAuthorizedEvent + +# Load environment variables from .env file +load_dotenv() + + +# Set up logging +logging.basicConfig(level=logging.DEBUG) + +app = App() + + +# Register the event handler +@app.listen("on_application_authorized") +async def on_application_authorized(event: ApplicationAuthorizedEvent) -> None: + if not event.guild: + print(f"User {event.user.display_name} ({event.user.id}) installed the application.") + + else: + print( + f"Bot {event.user.display_name} ({event.user.id}) installed the application" # noqa: ISC003 + + f" to guild {event.guild.name} ({event.guild.id})." + ) + + +# Run the app +if __name__ == "__main__": + app.run( + token=os.environ["DISCORD_TOKEN"], + public_key=os.environ["DISCORD_PUBLIC_KEY"], + uvicorn_options={ + "host": "0.0.0.0", # noqa: S104 + "port": 8000, + "log_level": "info", + }, + ) diff --git a/pyproject.toml b/pyproject.toml index 04b904e..1b985e3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,7 +90,7 @@ exclude = [ [tool.ruff.lint] select = ["ALL"] -per-file-ignores = { "examples/**/*" = ["INP001", "ARG002"] } +per-file-ignores = { "examples/**/*" = ["INP001", "ARG002", "T201"] } extend-ignore = [ "N999", "D104",