mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Rework discord webhook dispatcher
This commit is contained in:
parent
628c234400
commit
a387c12769
@ -1,9 +1,12 @@
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
import requests
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
from app.classes.shared.helpers import Helpers
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
helper = Helpers()
|
||||
|
||||
class WebhookHandler:
|
||||
@staticmethod
|
||||
@ -27,20 +30,65 @@ class WebhookHandler:
|
||||
return ["server_start", "server_stop", "server_crash", "server_backup"]
|
||||
|
||||
@staticmethod
|
||||
def send_discord_webhook(title, url, message, color):
|
||||
dataset = {
|
||||
def send_discord_webhook(server_name, title, url, message, color):
|
||||
"""
|
||||
Sends a message to a Discord channel via a webhook.
|
||||
|
||||
This method prepares a payload for the Discord webhook API using
|
||||
the provided details, Crafty Controller version, and the current UTC datetime.
|
||||
It dispatches this payload to the specified webhook URL.
|
||||
|
||||
Parameters:
|
||||
- server_name (str): Name of the server, used as 'author' in the Discord embed.
|
||||
- title (str): Title of the message in the Discord embed.
|
||||
- url (str): URL of the Discord webhook.
|
||||
- message (str): Main content of the message in the Discord embed.
|
||||
- color (int): Color code for the side stripe in the Discord message.
|
||||
|
||||
Returns:
|
||||
None. Sends the message to Discord without returning any value.
|
||||
|
||||
Note:
|
||||
Uses 'requests' to send the webhook payload. Request times out after 10 seconds
|
||||
to avoid indefinite hanging.
|
||||
"""
|
||||
# Grab Crafty System version
|
||||
version = helper.get_version_string()
|
||||
|
||||
# Get the current UTC datetime
|
||||
current_datetime = datetime.utcnow()
|
||||
|
||||
# Format the datetime to discord's required UTC string format
|
||||
# "YYYY-MM-DDTHH:MM:SS.MSSZ"
|
||||
formatted_datetime = (current_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3]
|
||||
+ 'Z')
|
||||
|
||||
# Prepare webhook payload
|
||||
payload = {
|
||||
"username": "Crafty Webhooks",
|
||||
"avatar_url": "https://docs.craftycontrol.com/img/favicon.ico",
|
||||
"avatar_url": (
|
||||
"https://gitlab.com/crafty-controller/crafty-4/-",
|
||||
"/raw/master/app/frontend/static/assets/images/Crafty_4-0.png"),
|
||||
"embeds": [
|
||||
{
|
||||
"title": title,
|
||||
"description": message,
|
||||
"color": color,
|
||||
"title": title,
|
||||
"description": message,
|
||||
"color": color,
|
||||
"author": {
|
||||
"name": server_name
|
||||
},
|
||||
"footer": {
|
||||
"text": f"Crafty Controller v.{version}"
|
||||
},
|
||||
"timestamp": formatted_datetime
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
# Dispatch webhook
|
||||
requests.post(
|
||||
url,
|
||||
data=json.dumps(dataset),
|
||||
data=json.dumps(payload),
|
||||
headers={"Content-type": "application/json"},
|
||||
timeout=10, # 10 seconds, so we don't hang indefinitly
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user