Initial webhook integration

This commit is contained in:
amcmanu3 2023-05-17 19:05:59 -04:00
parent cbfa40ce8d
commit a0abc6819b
2 changed files with 43 additions and 0 deletions

View File

@ -31,6 +31,7 @@ from app.classes.shared.console import Console
from app.classes.shared.helpers import Helpers
from app.classes.shared.file_helpers import FileHelpers
from app.classes.shared.null_writer import NullWriter
from app.classes.web.webhook_handler import WebhookHandler
with redirect_stderr(NullWriter()):
import psutil
@ -512,6 +513,9 @@ class ServerInstance:
if self.process.poll() is None:
logger.info(f"Server {self.name} running with PID {self.process.pid}")
Console.info(f"Server {self.name} running with PID {self.process.pid}")
WebhookHandler.send_discord_webhook(
"Crafty Controller", f"{self.name} started!", 65354
)
self.is_crashed = False
self.stats_helper.server_crash_reset()
self.record_server_stats()
@ -807,6 +811,9 @@ class ServerInstance:
logger.info(f"Stopped Server {server_name} with PID {server_pid}")
Console.info(f"Stopped Server {server_name} with PID {server_pid}")
WebhookHandler.send_discord_webhook(
"Crafty Controller", f"{self.name} was stopped!", 16748076
)
# massive resetting of variables
self.cleanup_server_object()
@ -890,6 +897,12 @@ class ServerInstance:
f"The server {name} has crashed and will be restarted. "
f"Restarting server"
)
WebhookHandler.send_discord_webhook(
"Crafty Controller",
f"{self.name} crashed! Crafty Controller is attempting to start it back up!",
16711680,
)
self.run_threaded_server(None)
return True
logger.critical(

View File

@ -0,0 +1,30 @@
import json
import logging
import requests
logger = logging.getLogger(__name__)
class WebhookHandler:
@staticmethod
def send_discord_webhook(title, message, color):
dataset = {
"username": "Crafty Webhooks",
"avatar_url": "https://docs.craftycontrol.com/img/favicon.ico",
"embeds": [
{
"title": title,
"description": message,
"color": color,
}
],
}
logger.debug(
"Webhook response: "
+ requests.post(
"https://discord.com/api/webhooks/1107017140004995081/leFCJ4g_Uw6ZwxaZXTLmi-L7njIFwVvFbf3JEHnAvUJAd90PoMknlivel0rosfnFed77",
data=json.dumps(dataset),
headers={"Content-type": "application/json"},
),
)