2023-09-28 19:17:47 +01:00
|
|
|
from datetime import datetime
|
|
|
|
from app.classes.web.webhooks.base_webhook import WebhookProvider
|
|
|
|
|
|
|
|
|
|
|
|
class DiscordWebhook(WebhookProvider):
|
2023-09-28 19:15:01 -04:00
|
|
|
def _construct_discord_payload(self, server_name, title, message, color, bot_name):
|
2023-09-28 19:17:47 +01:00
|
|
|
"""
|
|
|
|
Constructs the payload required for sending a Discord webhook notification.
|
|
|
|
|
|
|
|
This method prepares a payload for the Discord webhook API using the provided
|
|
|
|
message content, the Crafty Controller version, and the current UTC datetime.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
server_name (str): The name of the server triggering the notification.
|
|
|
|
title (str): The title for the notification message.
|
|
|
|
message (str): The main content of the notification message.
|
|
|
|
color (int): The color code for the side stripe in the Discord embed message.
|
2023-10-06 19:20:53 +01:00
|
|
|
bot_name (str): Override for the Webhook's name set on creation
|
2023-09-28 19:17:47 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
tuple: A tuple containing the constructed payload (dict) incl headers (dict).
|
2023-10-06 19:25:30 +01:00
|
|
|
|
|
|
|
Note:
|
|
|
|
- Discord embed designer
|
|
|
|
- https://discohook.org/
|
2023-09-28 19:17:47 +01:00
|
|
|
"""
|
|
|
|
current_datetime = datetime.utcnow()
|
|
|
|
formatted_datetime = (
|
|
|
|
current_datetime.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
|
|
|
)
|
|
|
|
|
2023-09-28 19:07:03 -04:00
|
|
|
# Convert the hex to an integer
|
|
|
|
sanitized_hex = color[1:] if color.startswith("#") else color
|
|
|
|
color_int = int(sanitized_hex, 16)
|
|
|
|
|
2023-09-28 19:17:47 +01:00
|
|
|
headers = {"Content-type": "application/json"}
|
|
|
|
payload = {
|
2023-09-28 19:15:01 -04:00
|
|
|
"username": bot_name,
|
2023-09-28 19:17:47 +01:00
|
|
|
"avatar_url": self.WEBHOOK_PFP_URL,
|
|
|
|
"embeds": [
|
|
|
|
{
|
|
|
|
"title": title,
|
|
|
|
"description": message,
|
2023-09-28 19:07:03 -04:00
|
|
|
"color": color_int,
|
2023-09-28 19:17:47 +01:00
|
|
|
"author": {"name": server_name},
|
|
|
|
"footer": {"text": f"Crafty Controller v.{self.CRAFTY_VERSION}"},
|
|
|
|
"timestamp": formatted_datetime,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload, headers
|
|
|
|
|
|
|
|
def send(self, server_name, title, url, message, **kwargs):
|
|
|
|
"""
|
|
|
|
Sends a Discord webhook notification using the given details.
|
|
|
|
|
|
|
|
The method constructs and dispatches a payload suitable for
|
|
|
|
Discords's webhook system.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
server_name (str): The name of the server triggering the notification.
|
|
|
|
title (str): The title for the notification message.
|
|
|
|
url (str): The webhook URL to send the notification to.
|
|
|
|
message (str): The main content or body of the notification message.
|
2023-09-28 19:07:03 -04:00
|
|
|
color (str, optional): The color code for the embed's side stripe.
|
2023-09-28 19:17:47 +01:00
|
|
|
Defaults to a pretty blue if not provided.
|
2023-10-06 19:20:53 +01:00
|
|
|
bot_name (str): Override for the Webhook's name set on creation
|
2023-09-28 19:17:47 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: "Dispatch successful!" if the message is sent successfully, otherwise an
|
|
|
|
exception is raised.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
Exception: If there's an error in dispatching the webhook.
|
|
|
|
"""
|
2023-09-28 19:07:03 -04:00
|
|
|
color = kwargs.get("color", "#005cd1") # Default to a color if not provided.
|
2023-09-28 19:15:01 -04:00
|
|
|
bot_name = kwargs.get("bot_name", self.WEBHOOK_USERNAME)
|
2023-09-28 19:17:47 +01:00
|
|
|
payload, headers = self._construct_discord_payload(
|
2023-09-28 19:15:01 -04:00
|
|
|
server_name, title, message, color, bot_name
|
2023-09-28 19:17:47 +01:00
|
|
|
)
|
|
|
|
return self._send_request(url, payload, headers)
|