From 73cc6aa0e94384deee89287b6d47fb879e0a93d4 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sun, 26 Sep 2021 22:12:52 +0200 Subject: [PATCH] Add slash command, support multiple restos & add date to embed --- cogs/school.py | 23 ++-------------- cogs/slash/school_slash.py | 37 +++++++++++++++++++++++++ data/embeds/food.py | 55 ++++++++++++++++++++++++++++++++++++++ functions/eten.py | 2 +- 4 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 cogs/slash/school_slash.py create mode 100644 data/embeds/food.py diff --git a/cogs/school.py b/cogs/school.py index 4325057..01f8f09 100644 --- a/cogs/school.py +++ b/cogs/school.py @@ -1,4 +1,5 @@ from data import schedule +from data.embeds.food import Menu from decorators import help import discord from discord.ext import commands @@ -21,27 +22,7 @@ class School(commands.Cog): # @commands.check(checks.allowedChannels) @help.Category(category=Category.School) async def eten(self, ctx, day: str = None): - day_dt = les.find_target_date(day if day else None) - day_dt = skip_weekends(day_dt) - day = intToWeekday(day_dt.weekday()) - - # Create embed - menu = eten.etenScript(day_dt) - embed = discord.Embed(colour=discord.Colour.blue()) - embed.set_author(name="Menu voor {}".format(day.lower())) - - if "gesloten" in menu[0].lower(): - embed.description = "Restaurant gesloten" - else: - embed.add_field(name="🥣 Soep:", value=menu[0], inline=False) - embed.add_field(name="🍴 Hoofdgerechten:", value=menu[1], inline=False) - - if menu[2]: - embed.add_field(name="❄️ Koud:", value=menu[2], inline=False) - - if menu[3]: - embed.add_field(name="🥦 Groenten:", value=menu[3], inline=False) - + embed = Menu(day).to_embed() await ctx.reply(embed=embed, mention_author=False) @commands.command(name="Les", aliases=["Class", "Classes", "Sched", "Schedule"], usage="[Dag]*") diff --git a/cogs/slash/school_slash.py b/cogs/slash/school_slash.py new file mode 100644 index 0000000..d664f99 --- /dev/null +++ b/cogs/slash/school_slash.py @@ -0,0 +1,37 @@ +from discord.ext import commands +from dislash import SlashInteraction, slash_command, Option, OptionType, OptionChoice + +from data.embeds.food import Menu, restos +from startup.didier import Didier + + +class SchoolSlash(commands.Cog): + def __init__(self, client: Didier): + self.client: Didier = client + + @slash_command( + name="eten", + description="Menu voor een bepaalde resto op een bepaalde dag", + options=[ + Option( + "dag", + description="Dag", + type=OptionType.STRING + ), + Option( + "resto", + description="Resto", + type=OptionType.STRING, + choices=list( + OptionChoice(v, k) for k, v in restos.items() + ) + ) + ] + ) + async def _food_slash(self, interaction: SlashInteraction, dag: str = None, resto: str = "sterre"): + embed = Menu(dag, resto).to_embed() + await interaction.reply(embed=embed) + + +def setup(client: Didier): + client.add_cog(SchoolSlash(client)) diff --git a/data/embeds/food.py b/data/embeds/food.py new file mode 100644 index 0000000..65d2519 --- /dev/null +++ b/data/embeds/food.py @@ -0,0 +1,55 @@ +from dataclasses import dataclass, field +from datetime import datetime +from typing import Optional, Tuple, Dict + +import discord + +from functions import les, eten +from functions.timeFormatters import skip_weekends, intToWeekday +from functions.stringFormatters import leading_zero as lz + + +restos: Dict[str, str] = { + "ardoyen": "Ardoyen", + "coupure": "Coupure", + "debrug": "De Brug", + "dunant": "Dunant", + "heymans": "Heymans", + "kantienberg": "Kantienberg", + "merelbeke": "Merelbeke", + "sterre": "Sterre" +} + + +@dataclass +class Menu: + day: Optional[str] = None + resto: str = "sterre" + _day: datetime = field(init=False) + _menu: Tuple[str, str, str, str] = field(init=False) + + def __post_init__(self): + self._day = les.find_target_date(self.day if self.day else None) + self._day = skip_weekends(self._day) + self.day = intToWeekday(self._day.weekday()) + self._menu = eten.etenScript(self._day) + + def to_embed(self) -> discord.Embed: + embed = discord.Embed(colour=discord.Colour.blue()) + date_formatted = f"{lz(self._day.day)}/{lz(self._day.month)}/{self._day.year}" + embed.set_author(name=f"Menu voor {self.day.lower()} {date_formatted}") + embed.title = f"Resto {restos[self.resto]}" + + if "gesloten" in self._menu[0].lower(): + embed.description = "Restaurant gesloten" + else: + embed.add_field(name="🥣 Soep:", value=self._menu[0], inline=False) + embed.add_field(name="🍴 Hoofdgerechten:", value=self._menu[1], inline=False) + + if self._menu[2]: + embed.add_field(name="❄️ Koud:", value=self._menu[2], inline=False) + + if self._menu[3]: + embed.add_field(name="🥦 Groenten:", value=self._menu[3], inline=False) + + return embed diff --git a/functions/eten.py b/functions/eten.py index 84e031a..1d6220b 100644 --- a/functions/eten.py +++ b/functions/eten.py @@ -49,7 +49,7 @@ def etenScript(dag: datetime, resto: str = "sterre"): for v in menu["vegetables"]: menuGroenten += ("* {}\n".format(v)) - except Exception as e: + except Exception: menuSoep += "Restaurant gesloten" menuGroenten += "Restaurant gesloten" menuHoofdgerechten += "Restaurant gesloten"