From 3c5fafadaa15fa19d7d6b478fb7c24052c699231 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Wed, 17 Sep 2025 21:19:28 +0200 Subject: [PATCH] main function tweaks --- main.py | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index bab4d7a..3e5e98b 100644 --- a/main.py +++ b/main.py @@ -44,27 +44,31 @@ def get_message(type: str) -> dict[str, int]: return messages["unknown"] -def send_notification(message: dict[str, int]) -> None: +def create_embed(message: str) -> dict: """ - Send a notification to the Discord webhook as an embed. + Create a Discord embed message. + """ + msg = get_message(message) + embed = { + "title": "Notification", + "description": msg["text"], + "color": msg["color"], + "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), + "footer": { + "text": "THC - Toke Hash Coordinated" + } + } + return embed - Args: - message (dict[str, int]): The message to send. + +def send_notification(message: str) -> None: + """ + Send a notification to the Discord webhook. """ if not WEBHOOK_URL: logging.error("WEBHOOK_URL not set") return - - embed = { - "title": "Notification", - "description": message["text"], - "color": message["color"], - "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), - "footer": { - "text": "Discord Webhook App" - } - } - + embed = create_embed(message) data = {"embeds": [embed]} try: response = requests.post(WEBHOOK_URL, json=data, timeout=10) @@ -79,26 +83,19 @@ def send_notification(message: dict[str, int]) -> None: logging.error(f"Error sending notification: {e}") -def test_notification() -> None: - """ - Send a test notification to verify the webhook. - """ - send_notification(get_message("test")) - - def main() -> None: """ Main function to run the scheduler. """ # Schedule notifications - schedule.every().hour.at(":15").do(send_notification, get_message("reminder")) - schedule.every().hour.at(":20").do(send_notification, get_message("notification")) - schedule.every().hour.at(":45").do(send_notification, get_message("reminder")) - schedule.every().hour.at(":50").do(send_notification, get_message("halftime")) + schedule.every().hour.at(":15").do(send_notification, "reminder") + schedule.every().hour.at(":20").do(send_notification, "notification") + schedule.every().hour.at(":45").do(send_notification, "reminder") + schedule.every().hour.at(":50").do(send_notification, "halftime") logging.info("Scheduler started.") # Test the notification on startup - test_notification() + send_notification("test") try: while True: