refactor: streamline message handling and embed creation
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 49s
All checks were successful
Build and Deploy Docker Container / build-and-deploy (push) Successful in 49s
This commit is contained in:
60
main.py
60
main.py
@@ -18,26 +18,6 @@ load_dotenv()
|
|||||||
|
|
||||||
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL')
|
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]:
|
def load_timezones() -> list[dict]:
|
||||||
"""Load timezones from csv file."""
|
"""Load timezones from csv file."""
|
||||||
@@ -77,42 +57,43 @@ def load_countries() -> list[dict]:
|
|||||||
return countries
|
return countries
|
||||||
|
|
||||||
|
|
||||||
timezones = load_timezones()
|
def create_embed(type: str) -> dict:
|
||||||
countries = load_countries()
|
|
||||||
|
|
||||||
|
|
||||||
def get_message(type: str) -> dict[str, int]:
|
|
||||||
"""
|
"""
|
||||||
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:
|
if type in messages:
|
||||||
msg = messages[type]
|
msg = messages[type]
|
||||||
if type in ["halftime", "420"]:
|
if type in ["halftime", "420"]:
|
||||||
|
# Add an image for halftime and 420
|
||||||
msg["image"] = {
|
msg["image"] = {
|
||||||
"url": "https://copyparty.allucanget.biz/img/weed.png"}
|
"url": "https://copyparty.allucanget.biz/img/weed.png"}
|
||||||
if type == "420":
|
if type == "420":
|
||||||
|
# Check where it's 4:20
|
||||||
tz_list = where_is_it_420(timezones, countries)
|
tz_list = where_is_it_420(timezones, countries)
|
||||||
if tz_list:
|
if tz_list:
|
||||||
tz_str = "\n".join(tz_list)
|
tz_str = "\n".join(tz_list)
|
||||||
msg["text"] += f"\nIt's 4:20 in:\n{tz_str}"
|
msg["text"] += f"\nIt's 4:20 in:\n{tz_str}"
|
||||||
else:
|
else:
|
||||||
msg["text"] += " It's not 4:20 anywhere right now."
|
msg["text"] += " It's not 4:20 anywhere right now."
|
||||||
return msg
|
else:
|
||||||
|
msg = {"text": "Unknown notification type", "color": 0xFF0000}
|
||||||
|
|
||||||
def create_embed(message: str) -> dict:
|
|
||||||
"""
|
|
||||||
Create a Discord embed message.
|
|
||||||
"""
|
|
||||||
msg = get_message(message)
|
|
||||||
embed = {
|
embed = {
|
||||||
"title": message.replace("_", " ").capitalize(),
|
"title": type.replace("_", " ").capitalize(),
|
||||||
"description": msg["text"],
|
"description": msg["text"],
|
||||||
"image": msg.get("image"),
|
"image": msg.get("image"),
|
||||||
"color": msg["color"],
|
"color": msg["color"],
|
||||||
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
"timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
||||||
"footer": {"text": MSG_FOOTER}
|
"footer": {"text": "THC - Toke Hash Coordinated"}
|
||||||
}
|
}
|
||||||
return embed
|
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)
|
tz_info = get_tz_info(tz, timezones)
|
||||||
if tz_info:
|
if tz_info:
|
||||||
country = get_country_info(tz_info["country_code"], countries)
|
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('"')
|
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
|
return tz_list
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user