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): """