2
2
3
3
import discord
4
4
from discord import app_commands
5
+ from discord .app_commands import Transform , Transformer
5
6
from discord .ext import commands
6
7
from loguru import logger
7
8
9
+ from cogs .error import TooFewOptionsError , TooManyOptionsError
8
10
from src .db_folder .databases import PollDatabase , VoteButtonDatabase
9
11
from src .jachym import Jachym
10
12
from src .ui .embeds import PollEmbed , PollEmbedBase
11
13
from src .ui .poll import Poll
12
14
from src .ui .poll_view import PollView
13
15
14
16
15
- def error_handling (answer : list [str ]) -> str :
16
- if len (answer ) > Poll .MAX_OPTIONS :
17
- return f"Zadal jsi příliš mnoho odpovědí, můžeš maximálně { Poll .MAX_OPTIONS } !"
18
- return f"Zadal jsi příliš málo odpovědí, můžeš alespoň { Poll .MIN_OPTIONS } !"
17
+ class OptionsTransformer (Transformer ):
18
+ async def transform (
19
+ self , interaction : discord .Interaction , option : str
20
+ ) -> TooManyOptionsError | TooFewOptionsError | list [str ]:
21
+ """
22
+ Transformer method to transformate a single string to multiple options. If they are not within parameters,
23
+ raises an error, else returns options.
19
24
25
+ Parameters
26
+ ----------
27
+ interaction: discord.Interaction
28
+ option: str
29
+
30
+ Returns
31
+ -------
32
+ List of strings
33
+
34
+ Raises:
35
+ -------
36
+ TooManyOptionsError, TooFewOptionsError
37
+
38
+ """
39
+ answers = [option for option in re .split ('"|"|“|„' , option ) if option .strip ()]
40
+ if len (answers ) > Poll .MAX_OPTIONS :
41
+ msg = f"Zadal jsi příliš mnoho odpovědí, můžeš maximálně { Poll .MAX_OPTIONS } !"
42
+ raise TooManyOptionsError (msg , interaction )
43
+ if len (answers ) < Poll .MIN_OPTIONS :
44
+ msg = f"Zadal jsi příliš málo odpovědí, můžeš alespoň { Poll .MIN_OPTIONS } !"
45
+ raise TooFewOptionsError (msg , interaction )
46
+ return answers
20
47
21
- class PollCreate (commands .Cog ):
22
- POLL_PARAMETERS = {
23
- "name" : "anketa" ,
24
- "description" : "Anketa pro hlasování. Jsou vidět všichni hlasovatelé." ,
25
- "question" : "Otázka, na kterou potřebuješ vědět odpověď" ,
26
- "answer" : 'Odpovědi, rozděluješ odpovědi uvozovkou ("), maximálně pouze 10 možností' ,
27
- "help" : """
28
- Jednoduchá anketa, která obsahuje otázku a odpovědi. Povoleno je 10 možností.
29
- """ ,
30
- }
31
-
32
- # Bugfix for iPhone users who have different font for aposthrofe
33
- REGEX_PATTERN = ['"' , "”" , "“" , "„" ]
34
48
49
+ class PollCreate (commands .Cog ):
35
50
def __init__ (self , bot : Jachym ):
36
51
self .bot = bot
37
52
@@ -45,29 +60,19 @@ def __init__(self, bot: Jachym):
45
60
answer = 'Odpovědi, rozděluješ odpovědi uvozovkou ("), maximálně pouze 10 možností' ,
46
61
)
47
62
async def pool (
48
- self ,
49
- interaction : discord .Interaction ,
50
- question : str ,
51
- answer : str ,
63
+ self ,
64
+ interaction : discord .Interaction ,
65
+ question : str ,
66
+ answer : Transform [ list [ str , ...], OptionsTransformer ] ,
52
67
) -> discord .Message :
53
- await interaction .response .send_message (
54
- embed = PollEmbedBase ("Dělám na tom, vydrž!" ),
55
- )
68
+ await interaction .response .send_message (embed = PollEmbedBase ("Nahrávám anketu..." ))
56
69
message = await interaction .original_response ()
57
70
58
- answers = [
59
- answer
60
- for answer in re .split ("|" .join (self .REGEX_PATTERN ), answer )
61
- if answer .strip ()
62
- ]
63
- if error_handling (answers ):
64
- return await message .edit (embed = PollEmbedBase (error_handling (answers )))
65
-
66
71
poll = Poll (
67
72
message_id = message .id ,
68
73
channel_id = message .channel .id ,
69
74
question = question ,
70
- options = answers ,
75
+ options = answer ,
71
76
user_id = interaction .user .id ,
72
77
)
73
78
0 commit comments