moved class AlertEmbed from cog_notificator.py to alert_notifiy.py

This commit is contained in:
yrrad8 2023-10-30 11:43:37 +02:00
parent 110ab70349
commit 8b21ba4088
2 changed files with 102 additions and 98 deletions

View File

@ -1,3 +1,10 @@
import datetime
import discord
import db_access
from db_access import AreaDistrict
class Alert: class Alert:
""" """
Represents an HFC Alert Represents an HFC Alert
@ -41,3 +48,98 @@ class Alert:
data.get('title'), data.get('title'),
data.get('data'), data.get('data'),
data.get('desc')) 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)

View File

@ -72,104 +72,6 @@ class AlertReqs:
ret_dict = None ret_dict = None
return ret_dict 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 # noinspection PyUnresolvedReferences
class Notificator(commands.Cog): class Notificator(commands.Cog):
""" """