From 3f7bacca2f05df6ccdfc26aa9982504657c44f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Fri, 28 Jun 2024 06:04:30 +0200 Subject: [PATCH] Add support for Discord webhooks --- .env.example | 1 + config/settings.py | 1 + core/discord.py | 31 +++++++++++++++++++++++++++ core/templates/index.html | 13 ++++++++++- core/urls.py | 1 + core/views.py | 45 +++++++++++++++++++++++++++++++++++++-- requirements.txt | 1 + twitch/api.py | 9 ++++++++ 8 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 core/discord.py diff --git a/.env.example b/.env.example index 47e3f66..a24a778 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ DJANGO_SECRET_KEY= DEBUG=True EMAIL_HOST_USER= EMAIL_HOST_PASSWORD= +DISCORD_WEBHOOK_URL= diff --git a/config/settings.py b/config/settings.py index 4883d2c..ce40619 100644 --- a/config/settings.py +++ b/config/settings.py @@ -59,6 +59,7 @@ DEFAULT_FROM_EMAIL: str = os.getenv( default="webmaster@localhost", ) SERVER_EMAIL: str = os.getenv(key="EMAIL_HOST_USER", default="webmaster@localhost") +DISCORD_WEBHOOK_URL: str = os.getenv(key="DISCORD_WEBHOOK_URL", default="") INSTALLED_APPS: list[str] = [ "core.apps.CoreConfig", diff --git a/core/discord.py b/core/discord.py new file mode 100644 index 0000000..59e9529 --- /dev/null +++ b/core/discord.py @@ -0,0 +1,31 @@ +import logging +from typing import TYPE_CHECKING + +from discord_webhook import DiscordWebhook +from django.conf import settings + +if TYPE_CHECKING: + from requests import Response + +logger: logging.Logger = logging.getLogger(__name__) + + +def send(message: str) -> None: + """Send a message to Discord. + + Args: + message: The message to send. + """ + webhook_url = str(settings.DISCORD_WEBHOOK_URL) + if not webhook_url: + logger.error("No Discord webhook URL found.") + return + + webhook = DiscordWebhook( + url=webhook_url, + content=message, + username="TTVDrops", + rate_limit_retry=True, + ) + response: Response = webhook.execute() + logger.debug(response) diff --git a/core/templates/index.html b/core/templates/index.html index 15808b8..59a1bd2 100644 --- a/core/templates/index.html +++ b/core/templates/index.html @@ -131,7 +131,18 @@

{{ game.display_name }}

- +
+ {% csrf_token %} + + +
+
+ {% csrf_token %} + + +