Skip to content

Commit

Permalink
Added a function that converts a plain text into datetime object. Nex…
Browse files Browse the repository at this point in the history
…t commit should aim for tasks.loop and database overlay fixes.

Signed-off-by: robertsokola <[email protected]>
  • Loading branch information
TheXer committed Jul 27, 2023
1 parent d3cd4c4 commit 3958343
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
28 changes: 26 additions & 2 deletions cogs/poll_command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import re

import dateparser
import discord
from discord import app_commands
from discord.app_commands import Transform, Transformer
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -74,6 +97,7 @@ async def pool(
question=question,
options=answer,
user_id=interaction.user.id,
date_created=date_time,
)

embed = PollEmbed(poll)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ python-dotenv==0.17.1

aiomysql>=0.0.22
pytest>=7.3.1
loguru>=0.7.0
loguru>=0.7.0
dateparser>=1.1.8
5 changes: 5 additions & 0 deletions src/ui/error_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3958343

Please sign in to comment.