From 110ab70349b59603a6cadfaecc3b576542335122 Mon Sep 17 00:00:00 2001 From: yrrad8 Date: Mon, 30 Oct 2023 11:40:05 +0200 Subject: [PATCH 1/3] moved class Alert from cog_notificator.py to alert_notifiy.py --- alert_notifiy.py | 43 +++++++++++++++++++++++++++++++++++++++++++ cog_notificator.py | 44 +------------------------------------------- 2 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 alert_notifiy.py diff --git a/alert_notifiy.py b/alert_notifiy.py new file mode 100644 index 0000000..760f4cd --- /dev/null +++ b/alert_notifiy.py @@ -0,0 +1,43 @@ +class Alert: + """ + Represents an HFC Alert + """ + def __init__(self, id: int, cat: int, title: str, districts: list[str], desc: str): + """ + Init an Alert instance + :param id: Alert ID + :param cat: Alert category + :param title: Alert title + :param districts: districts the alert is running for + :param desc: Alert description/extra info + """ + self.id = id + self.category = cat + self.title = title + self.districts = districts + self.description = desc + + @classmethod + def from_dict(cls, data: dict): + """ + Return a new Alert instance from an Alert-formatted dict (matching HFC alert requests) + + Dict format: + + { + "id": int, + "cat": int, + "title": str, + "data": list[str], + "desc": str + } + + :param data: A dict of matching format + + :return: The new Alert instance + """ + return cls(int(data.get('id', '0')), + int(data.get('cat', '0')), + data.get('title'), + data.get('data'), + data.get('desc')) diff --git a/cog_notificator.py b/cog_notificator.py index 6a5762a..aebd9c4 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -13,6 +13,7 @@ import db_access import errlogging from db_access import * from markdown import md +from alert_notifiy import Alert load_dotenv() AUTHOR_ID = int(os.getenv('AUTHOR_ID')) @@ -72,49 +73,6 @@ class AlertReqs: return ret_dict -class Alert: - """ - Represents an HFC Alert - """ - def __init__(self, id: int, cat: int, title: str, districts: list[str], desc: str): - """ - Init an Alert instance - :param id: Alert ID - :param cat: Alert category - :param title: Alert title - :param districts: districts the alert is running for - :param desc: Alert description/extra info - """ - self.id = id - self.category = cat - self.title = title - self.districts = districts - self.description = desc - - @classmethod - def from_dict(cls, data: dict): - """ - Return a new Alert instance from an Alert-formatted dict (matching HFC alert requests) - - Dict format: - - { - "id": int, - "cat": int, - "title": str, - "data": list[str], - "desc": str - } - - :param data: A dict of matching format - - :return: The new Alert instance - """ - return cls(int(data.get('id', '0')), - int(data.get('cat', '0')), - data.get('title'), - data.get('data'), - data.get('desc')) class AlertEmbed: From 8b21ba4088ada3bf2b5aa0dda97cdceba8cdcd4a Mon Sep 17 00:00:00 2001 From: yrrad8 Date: Mon, 30 Oct 2023 11:43:37 +0200 Subject: [PATCH 2/3] moved class AlertEmbed from cog_notificator.py to alert_notifiy.py --- alert_notifiy.py | 102 +++++++++++++++++++++++++++++++++++++++++++++ cog_notificator.py | 98 ------------------------------------------- 2 files changed, 102 insertions(+), 98 deletions(-) diff --git a/alert_notifiy.py b/alert_notifiy.py index 760f4cd..91c64be 100644 --- a/alert_notifiy.py +++ b/alert_notifiy.py @@ -1,3 +1,10 @@ +import datetime +import discord + +import db_access + +from db_access import AreaDistrict + class Alert: """ Represents an HFC Alert @@ -41,3 +48,98 @@ class Alert: data.get('title'), data.get('data'), data.get('desc')) + +class AlertEmbed: + """ + A class representing an AlertEmbed + + :var embed: discord.Embed ready for sending + :var alert: Alert containing alert data + :var district: db_access.AreaDistrict | str containing district data + """ + + def __init__(self, alert: Alert | dict, district: db_access.AreaDistrict | str): + """ + Initiating the AlertEmbed class directly is equivalent to AlertEmbed.generic_alert, but is not recommended. + + Please use AlertEmbed.generic_alert instead. + + :var embed: discord.Embed ready for sending + :var alert: Alert containing alert data + :var district: db_access.AreaDistrict | str containing district data + """ + self.embed = discord.Embed(color=discord.Color.from_str('#FF0000')) + self.district = district + if isinstance(alert, dict): + self.alert = Alert.from_dict(alert) + else: + self.alert = alert + + if isinstance(self.district, AreaDistrict): + self.embed.title = f'התראה ב{self.district.name}' + self.embed.add_field(name=self.alert.title, value=f'איזור {self.district.area.name}') + + else: + self.embed.title = f'התראה ב{self.district}' + self.embed.add_field(name=self.alert.title, value='') + + self.embed.add_field(name='נכון ל', value=datetime.datetime.now().strftime("%H:%M:%S\n%d/%m/%Y"), inline=False) + self.embed.add_field(name='מידע נוסף', value=self.alert.description) + + @classmethod + def generic_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): + """ + Returns a new Generic AlertEmbed + :param alert: Alert object + :param district: AreaDistrict object (District containing Area, check db_access.AreaDistrict) + :return: New AlertEmbed instance + """ + ret_alem = cls(alert, district) + return ret_alem + + @classmethod + def missile_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): + """ + Returns a new Missile AlertEmbed + + Similar to Generic AlertEmbed, but contains Migun Time + + :param alert: Alert object + :param district: AreaDistrict object (District containing Area, check db_access.AreaDistrict) + :return: New AlertEmbed instance + """ + ret_alem = cls.generic_alert(alert, district) + + if (not isinstance(district, str)) and (district.migun_time is not None): + ret_alem.embed.insert_field_at(index=1, name='זמן מיגון', value=f'{district.migun_time} שניות', inline=False) + return ret_alem + + ret_alem.embed.insert_field_at(index=1, name='זמן מיגון', value='שגיאה באחזרת המידע', inline=False) + return ret_alem + + @classmethod + def auto_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): + """ + Tired of having to CHOOSE an alert type all the time? Well this is JUST for you! + + Introducing... auto_alert! Just init it like any other alert, and it will return the fitting alert right then and there*! + + *"then and there" does not include any computer, end-user, developer, or any other type of tomfoolery. + + (Hopefully now I'll never have to write documentation again >:) ) + + :param alert: Alert object or alert dict. + :param district: District object (from db_access) + :return: AlertEmbed object + """ + if isinstance(alert, dict): + alert_obj = Alert.from_dict(alert) + else: + alert_obj = alert + + match alert_obj.category: + case 1: + return cls.missile_alert(alert_obj, district) + case _: + return cls.generic_alert(alert_obj, district) + diff --git a/cog_notificator.py b/cog_notificator.py index aebd9c4..8faffef 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -72,104 +72,6 @@ class AlertReqs: ret_dict = None return ret_dict - - - -class AlertEmbed: - """ - A class representing an AlertEmbed - - :var embed: discord.Embed ready for sending - :var alert: Alert containing alert data - :var district: db_access.AreaDistrict | str containing district data - """ - - def __init__(self, alert: Alert | dict, district: db_access.AreaDistrict | str): - """ - Initiating the AlertEmbed class directly is equivalent to AlertEmbed.generic_alert, but is not recommended. - - Please use AlertEmbed.generic_alert instead. - - :var embed: discord.Embed ready for sending - :var alert: Alert containing alert data - :var district: db_access.AreaDistrict | str containing district data - """ - self.embed = discord.Embed(color=discord.Color.from_str('#FF0000')) - self.district = district - if isinstance(alert, dict): - self.alert = Alert.from_dict(alert) - else: - self.alert = alert - - if isinstance(self.district, AreaDistrict): - self.embed.title = f'התראה ב{self.district.name}' - self.embed.add_field(name=self.alert.title, value=f'איזור {self.district.area.name}') - - else: - self.embed.title = f'התראה ב{self.district}' - self.embed.add_field(name=self.alert.title, value='') - - self.embed.add_field(name='נכון ל', value=datetime.datetime.now().strftime("%H:%M:%S\n%d/%m/%Y"), inline=False) - self.embed.add_field(name='מידע נוסף', value=self.alert.description) - - @classmethod - def generic_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): - """ - Returns a new Generic AlertEmbed - :param alert: Alert object - :param district: AreaDistrict object (District containing Area, check db_access.AreaDistrict) - :return: New AlertEmbed instance - """ - ret_alem = cls(alert, district) - return ret_alem - - @classmethod - def missile_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): - """ - Returns a new Missile AlertEmbed - - Similar to Generic AlertEmbed, but contains Migun Time - - :param alert: Alert object - :param district: AreaDistrict object (District containing Area, check db_access.AreaDistrict) - :return: New AlertEmbed instance - """ - ret_alem = cls.generic_alert(alert, district) - - if (not isinstance(district, str)) and (district.migun_time is not None): - ret_alem.embed.insert_field_at(index=1, name='זמן מיגון', value=f'{district.migun_time} שניות', inline=False) - return ret_alem - - ret_alem.embed.insert_field_at(index=1, name='זמן מיגון', value='שגיאה באחזרת המידע', inline=False) - return ret_alem - - @classmethod - def auto_alert(cls, alert: Alert | dict, district: db_access.AreaDistrict | str): - """ - Tired of having to CHOOSE an alert type all the time? Well this is JUST for you! - - Introducing... auto_alert! Just init it like any other alert, and it will return the fitting alert right then and there*! - - *"then and there" does not include any computer, end-user, developer, or any other type of tomfoolery. - - (Hopefully now I'll never have to write documentation again >:) ) - - :param alert: Alert object or alert dict. - :param district: District object (from db_access) - :return: AlertEmbed object - """ - if isinstance(alert, dict): - alert_obj = Alert.from_dict(alert) - else: - alert_obj = alert - - match alert_obj.category: - case 1: - return cls.missile_alert(alert_obj, district) - case _: - return cls.generic_alert(alert_obj, district) - - # noinspection PyUnresolvedReferences class Notificator(commands.Cog): """ From bebb524409ede8645fac0d385fde0c82edb3061d Mon Sep 17 00:00:00 2001 From: yrrad8 Date: Mon, 30 Oct 2023 11:46:12 +0200 Subject: [PATCH 3/3] Rename from alert_notifiy.py to alert_maker.py --- alert_notifiy.py => alert_maker.py | 0 cog_notificator.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename alert_notifiy.py => alert_maker.py (100%) diff --git a/alert_notifiy.py b/alert_maker.py similarity index 100% rename from alert_notifiy.py rename to alert_maker.py diff --git a/cog_notificator.py b/cog_notificator.py index 8faffef..adf370a 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -13,7 +13,7 @@ import db_access import errlogging from db_access import * from markdown import md -from alert_notifiy import Alert +from alert_maker import AlertEmbed load_dotenv() AUTHOR_ID = int(os.getenv('AUTHOR_ID'))