diff options
author | 2020-09-24 16:37:23 +0530 | |
---|---|---|
committer | 2020-09-24 16:37:23 +0530 | |
commit | d83c3f058e1e6f0b249894331485daa27399ea0e (patch) | |
tree | 86d4dbb5cf8a1c0a078d0d6545368ee489cad64c | |
parent | added starting date and ending date of zodiac sign in json (diff) |
changed type of month and merged 2 static method into 1
-rw-r--r-- | bot/exts/valentines/valentine_zodiac.py | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/bot/exts/valentines/valentine_zodiac.py b/bot/exts/valentines/valentine_zodiac.py index e020548c..611d6e60 100644 --- a/bot/exts/valentines/valentine_zodiac.py +++ b/bot/exts/valentines/valentine_zodiac.py @@ -14,38 +14,35 @@ log = logging.getLogger(__name__) LETTER_EMOJI = ':love_letter:' HEART_EMOJIS = [":heart:", ":gift_heart:", ":revolving_hearts:", ":sparkling_heart:", ":two_hearts:"] -zodiac_signs = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", +ZODIAC_SIGNS = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"] -with open(Path("bot/resources/valentines/zodiac_explanation.json"), "r", encoding="utf8") as file: - """Load zodiac zodiac explanation from static JSON resource.""" - zodiac_fact = load(file) -year = datetime.now().year -zodiac_date = {"Aries": (datetime(year, 1, 20), datetime(year, 2, 18))} - - class ValentineZodiac(commands.Cog): """A Cog that returns a counter compatible zodiac sign to the given user's zodiac sign.""" def __init__(self, bot: commands.Bot): self.bot = bot - self.zodiacs = self.load_json() + self.zodiacs, self.zodiac_fact = self.load_comp_json() @staticmethod - def load_json() -> dict: + def load_comp_json() -> dict: """Load zodiac compatibility from static JSON resource.""" + p1 = Path("bot/resources/valentines/zodiac_explanation.json") p = Path("bot/resources/valentines/zodiac_compatibility.json") + with p1.open(encoding="utf8") as json_data: + zodiac_fact = load(json_data) with p.open(encoding="utf8") as json_data: zodiacs = load(json_data) - return zodiacs + return zodiacs, zodiac_fact def zodiac_sign_verify(self, zodiac: str) -> discord.Embed: """Gives informative zodiac embed.""" c_zodiac = zodiac.capitalize() + zodiac_fact = self.zodiac_fact embed = discord.Embed() embed.color = Colours.pink - if c_zodiac in zodiac_signs: + if c_zodiac in ZODIAC_SIGNS: log.info("Making zodiac embed") embed.title = f"__{c_zodiac}__" embed.description = zodiac_fact[f"{c_zodiac}"]["About"] @@ -62,10 +59,15 @@ class ValentineZodiac(commands.Cog): def zodiac_date_verifer(self, query_datetime: datetime) -> str: """Returns zodiac sign by checking month and date.""" - for zodiac_name, date_range in zodiac_date.items(): - if (date_range[0] <= query_datetime <= date_range[1]): + for zodiac_name, date_range in self.zodiac_fact.items(): + value_start = datetime(2020, date_range["start_at"][0], date_range["start_at"][1]).date() + value_end = datetime(2020, date_range["end_at"][0], date_range["end_at"][1]).date() + if value_start <= query_datetime.date() <= value_end: zodiac = zodiac_name - log.info("Wrong Zodiac date or month provided") + break + else: + zodiac = None + log.info("Wrong Zodiac date or month provided") return zodiac log.info("Zodiac name sent") @@ -101,8 +103,11 @@ class ValentineZodiac(commands.Cog): @partner_zodiac.command(name="date") async def date_and_month(self, ctx: commands.Context, month: int, date: int) -> None: """Provides information about zodiac sign by taking month and date as input.""" - zodiac_sign_based_on_month_and_date = self.zodiac_date_verifer(datetime(year, month, date)) - log.info("zodiac sign based on month and date received") + try: + zodiac_sign_based_on_month_and_date = self.zodiac_date_verifer(datetime(2020, month, date)) + log.info("zodiac sign based on month and date received") + except ValueError as e: + await ctx.send(f'You cannot do that, {e}') if zodiac_sign_based_on_month_and_date is None: log.info("zodiac sign based on month and date returned None") final_embed = discord.Embed() |