refactor: streamline message handling and embed creation
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 49s

This commit is contained in:
2025-10-05 11:47:39 +02:00
parent ab33cbe005
commit f19359cbee

60
main.py
View File

@@ -18,26 +18,6 @@ load_dotenv()
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
MSG_FOOTER = "THC - Toke Hash Coordinated"
MSG_REMINDER = "This is your 5 minute reminder to 420!"
MSG_HALFTIME_REMINDER = "Half-time in 5 minutes!"
MSG_HALFTIME = "Half-time!"
MSG_NOTIFICATION = "Blaze it!"
COL_BLUE = 0x3498db
COL_ORANGE = 0xe67e22
COL_GREEN = 0x2ecc71
COL_UNKNOWN = 0x95a5a6
messages = {
"reminder_halftime": {"text": MSG_HALFTIME_REMINDER, "color": COL_ORANGE},
"halftime": {"text": MSG_HALFTIME, "color": COL_GREEN},
"reminder": {"text": MSG_REMINDER, "color": COL_ORANGE},
"420": {"text": MSG_NOTIFICATION, "color": COL_GREEN},
"unknown": {"text": "Unknown notification type", "color": COL_UNKNOWN}
}
def load_timezones() -> list[dict]:
"""Load timezones from csv file."""
@@ -77,42 +57,43 @@ def load_countries() -> list[dict]:
return countries
timezones = load_timezones()
countries = load_countries()
def get_message(type: str) -> dict[str, int]:
def create_embed(type: str) -> dict:
"""
Get the notification message based on the type.
Create a Discord embed message.
"""
msg = messages["unknown"]
color_orange = 0xe67e22
color_green = 0x2ecc71
messages = {
"reminder_halftime": {"text": "Half-time in 5 minutes!", "color": color_orange},
"halftime": {"text": "Half-time!", "color": color_green},
"reminder": {"text": "This is your 5 minute reminder to 420!", "color": color_orange},
"420": {"text": "Blaze it!", "color": color_green},
}
timezones = load_timezones()
countries = load_countries()
if type in messages:
msg = messages[type]
if type in ["halftime", "420"]:
# Add an image for halftime and 420
msg["image"] = {
"url": "https://copyparty.allucanget.biz/img/weed.png"}
if type == "420":
# Check where it's 4:20
tz_list = where_is_it_420(timezones, countries)
if tz_list:
tz_str = "\n".join(tz_list)
msg["text"] += f"\nIt's 4:20 in:\n{tz_str}"
else:
msg["text"] += " It's not 4:20 anywhere right now."
return msg
def create_embed(message: str) -> dict:
"""
Create a Discord embed message.
"""
msg = get_message(message)
else:
msg = {"text": "Unknown notification type", "color": 0xFF0000}
embed = {
"title": message.replace("_", " ").capitalize(),
"title": type.replace("_", " ").capitalize(),
"description": msg["text"],
"image": msg.get("image"),
"color": msg["color"],
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
"footer": {"text": MSG_FOOTER}
"footer": {"text": "THC - Toke Hash Coordinated"}
}
return embed
@@ -163,9 +144,10 @@ def where_is_it_420(timezones: list[dict], countries: list[dict]) -> list[str]:
tz_info = get_tz_info(tz, timezones)
if tz_info:
country = get_country_info(tz_info["country_code"], countries)
if country and country["country_name"] not in tz_list:
if country:
country_name = country["country_name"].strip().strip('"')
tz_list.append(country_name)
if country_name not in tz_list:
tz_list.append(country_name)
return tz_list