Add Twitch auth with django-allauth
This commit is contained in:
@ -1,13 +1,14 @@
|
||||
import logging
|
||||
import os
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from discord_webhook import DiscordWebhook
|
||||
from django.db.models.manager import BaseManager
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from django.utils import timezone
|
||||
|
||||
from core.models import DropCampaign, Game, GameSubscription, Owner, OwnerSubscription
|
||||
from core.discord import generate_game_message, generate_owner_message
|
||||
from core.models import DropCampaign, Game, GameSubscription, Owner, OwnerSubscription, User
|
||||
|
||||
if TYPE_CHECKING:
|
||||
import requests
|
||||
@ -16,16 +17,35 @@ if TYPE_CHECKING:
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def convert_time_to_discord_timestamp(time: timezone.datetime | None) -> str:
|
||||
"""Discord uses <t:UNIX_TIMESTAMP:R> for timestamps.
|
||||
@receiver(signal=post_save, sender=User)
|
||||
def handle_user_signed_up(sender: User, instance: User, created: bool, **kwargs) -> None: # noqa: ANN003, ARG001, FBT001
|
||||
"""Send a message to Discord when a user signs up.
|
||||
|
||||
Webhook URL is read from .env file.
|
||||
|
||||
Args:
|
||||
time: The time to convert to a Discord timestamp.
|
||||
|
||||
Returns:
|
||||
str: The Discord timestamp string. If time is None, returns "Unknown".
|
||||
sender (User): The model we are sending the signal from.
|
||||
instance (User): The instance of the model that was created.
|
||||
created (bool): Whether the instance was created or updated.
|
||||
**kwargs: Additional keyword arguments.
|
||||
"""
|
||||
return f"<t:{int(time.timestamp())}:R>" if time else "Unknown"
|
||||
if not created:
|
||||
logger.debug("User '%s' was updated.", instance.username)
|
||||
return
|
||||
|
||||
webhook_url: str | None = os.getenv("DISCORD_WEBHOOK_URL")
|
||||
if not webhook_url:
|
||||
logger.error("No webhook URL provided.")
|
||||
return
|
||||
|
||||
webhook = DiscordWebhook(
|
||||
url=webhook_url,
|
||||
content=f"New user signed up: '{instance.username}'",
|
||||
username="TTVDrops",
|
||||
rate_limit_retry=True,
|
||||
)
|
||||
response: requests.Response = webhook.execute()
|
||||
logger.debug(response)
|
||||
|
||||
|
||||
@receiver(signal=post_save, sender=DropCampaign)
|
||||
@ -79,47 +99,3 @@ def notify_users_of_new_drop(sender: DropCampaign, instance: DropCampaign, creat
|
||||
)
|
||||
response: requests.Response = webhook.execute()
|
||||
logger.debug(response)
|
||||
|
||||
|
||||
def generate_game_message(instance: DropCampaign, game: Game, sub: GameSubscription) -> str:
|
||||
"""Generate a message for a drop campaign.
|
||||
|
||||
Args:
|
||||
instance (DropCampaign): Drop campaign instance.
|
||||
game (Game): Game instance.
|
||||
sub (GameSubscription): Game subscription instance.
|
||||
|
||||
Returns:
|
||||
str: The message to send to Discord.
|
||||
"""
|
||||
game_name: str = game.name or "Unknown"
|
||||
description: str = instance.description or "No description provided."
|
||||
start_at: str = convert_time_to_discord_timestamp(instance.starts_at)
|
||||
end_at: str = convert_time_to_discord_timestamp(instance.ends_at)
|
||||
msg: str = f"{game_name}: {instance.name}\n{description}\nStarts: {start_at}\nEnds: {end_at}"
|
||||
|
||||
logger.info("Discord message: '%s' to '%s'", msg, sub.webhook.url)
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
def generate_owner_message(instance: DropCampaign, owner: Owner, sub: OwnerSubscription) -> str:
|
||||
"""Generate a message for a drop campaign.
|
||||
|
||||
Args:
|
||||
instance (DropCampaign): Drop campaign instance.
|
||||
owner (Owner): Owner instance.
|
||||
sub (OwnerSubscription): Owner subscription instance.
|
||||
|
||||
Returns:
|
||||
str: The message to send to Discord.
|
||||
"""
|
||||
owner_name: str = owner.name or "Unknown"
|
||||
description: str = instance.description or "No description provided."
|
||||
start_at: str = convert_time_to_discord_timestamp(instance.starts_at)
|
||||
end_at: str = convert_time_to_discord_timestamp(instance.ends_at)
|
||||
msg: str = f"{owner_name}: {instance.name}\n{description}\nStarts: {start_at}\nEnds: {end_at}"
|
||||
|
||||
logger.info("Discord message: '%s' to '%s'", msg, sub.webhook.url)
|
||||
|
||||
return msg
|
||||
|
Reference in New Issue
Block a user