From 3958343f41aeb487458e8840ff43b573f75691d9 Mon Sep 17 00:00:00 2001 From: robertsokola Date: Thu, 27 Jul 2023 15:28:07 +0200 Subject: [PATCH] Added a function that converts a plain text into datetime object. Next commit should aim for tasks.loop and database overlay fixes. Signed-off-by: robertsokola --- cogs/poll_command.py | 28 ++++++++++++++++++++++++++-- requirements.txt | 3 ++- src/ui/error_view.py | 5 +++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cogs/poll_command.py b/cogs/poll_command.py index 9b4b6c8..341a119 100644 --- a/cogs/poll_command.py +++ b/cogs/poll_command.py @@ -1,5 +1,7 @@ +import datetime import re +import dateparser import discord from discord import app_commands from discord.app_commands import Transform, Transformer @@ -9,7 +11,7 @@ from src.db_folder.databases import PollDatabase, VoteButtonDatabase from src.jachym import Jachym from src.ui.embeds import PollEmbed, PollEmbedBase -from src.ui.error_view import TooFewOptionsError, TooManyOptionsError +from src.ui.error_view import DatetimeNotRecognizedError, TooFewOptionsError, TooManyOptionsError from src.ui.poll import Poll from src.ui.poll_view import PollView @@ -46,6 +48,21 @@ async def transform( return answers +class DatetimeTransformer(Transformer): + async def transform(self, interaction: discord.Interaction, date_time: str) -> datetime.datetime: + parsed_datetime = dateparser.parse( + date_time, + languages=["cs", "en", "sk"], + ) + if not parsed_datetime: + msg = "Daný datum jsem bohužel nerozpoznal, zkusíš to znova?" + raise DatetimeNotRecognizedError(msg, interaction) + if parsed_datetime < datetime.datetime.now(): + msg = "Datum nemůžeš zakládat v minulosti!" + raise DatetimeNotRecognizedError(msg, interaction) + return parsed_datetime + + class PollCreate(commands.Cog): def __init__(self, bot: Jachym): self.bot = bot @@ -54,16 +71,22 @@ def __init__(self, bot: Jachym): name="anketa", description="Anketa pro hlasování. Jsou vidět všichni hlasovatelé.", ) - @app_commands.rename(question="otázka", answer="odpovědi") + @app_commands.rename( + question="otázka", + answer="odpovědi", + date_time="datum", + ) @app_commands.describe( question="Otázka, kterou chceš položit.", answer='Odpovědi, rozděluješ odpovědi uvozovkou ("), maximálně pouze 10 možností', + date_time="Den, na který anketa skončí.", ) async def pool( self, interaction: discord.Interaction, question: str, answer: Transform[list[str, ...], OptionsTransformer], + date_time: Transform[datetime.datetime, DatetimeTransformer] | None, ) -> discord.Message: await interaction.response.send_message(embed=PollEmbedBase("Nahrávám anketu...")) message = await interaction.original_response() @@ -74,6 +97,7 @@ async def pool( question=question, options=answer, user_id=interaction.user.id, + date_created=date_time, ) embed = PollEmbed(poll) diff --git a/requirements.txt b/requirements.txt index 79fb711..86f16d1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ python-dotenv==0.17.1 aiomysql>=0.0.22 pytest>=7.3.1 -loguru>=0.7.0 \ No newline at end of file +loguru>=0.7.0 +dateparser>=1.1.8 \ No newline at end of file diff --git a/src/ui/error_view.py b/src/ui/error_view.py index b6725f9..2d2df06 100644 --- a/src/ui/error_view.py +++ b/src/ui/error_view.py @@ -40,3 +40,8 @@ class TooFewOptionsError(PrettyError): class NoPermissionError(PrettyError): "Whether the user has no permissions to edit." pass + + +class DatetimeNotRecognizedError(PrettyError): + "Whether the datetime is not recognized." + pass