I’m writing a Discord bot to create and manage polls for a server I’m a part of, using the discord.py wrapper. I implemented a basic slash command to generate a poll that takes an optional integer to set the number of options to vote from (default value is 2):
@bot.tree.command(name="votazione")
async def make_poll(
interaction: discord.Interaction,
*,
titolo: str,
opzioni: int = 2,
canale: discord.TextChannel | None = None,
maggioranza: int | None = None
):
options = PollOptions(f"test per voto: {titolo}", opzioni)
await interaction.response.send_modal(options)
for option in options.children:
print(len(option.value))
Once called, it initiates a ui.Modal
object with an equivalent number of ui.TextInput
elements:
class PollOptions(ui.Modal):
def __init__(self, title: str, n: int):
super().__init__(title=title)
self.make_options(n)
def make_options(self, n):
for i in range(n):
self.add_item(ui.TextInput(label=f"Opzione {i+1}"))
async def on_submit(self, interaction: discord.Interaction):
await interaction.response.defer()
The idea here is to let the user input the text of the vote options to be used in the poll. I would then retrieve those with PollOptions.children.value
and construct the actual Poll
object.
The problem here is that that print
statement in the first code block is telling me that the values are all length 0 (which shouldn’t be the case, in all tests I’m writing non-empty strings into the text inputs before submitting).
What’s going on here? I’m not very familiar with the Discord API, and no error is showing up (I’m writing all exceptions to the logger), so I seriously have no clue.
What I tried:
- I made a slash command that creates a Modal and should give me back the user’s input
What I expected to happen:
- To get the user’s input as inserted in the TextInput fields of the Modal
What actually happened:
- Got empty strings instead