JavaScript
x
11
11
1
class title_input_modal(Modal):
2
def __init__(self):
3
super().__init__("Audio Title Input")
4
5
self.add_item(InputText(label = "Audio Title", style = discord.InputTextStyle.short))
6
7
async def callback(self, interaction: discord.Interaction):
8
9
val = self.children[0].value
10
await interaction.response.send_message(val)
11
How can I make a method and store the value of the response? If I try accessing title_input_modal.children[0]
from another cog’s class method, this error pops up:-
JavaScript
1
15
15
1
Ignoring exception in view <View timeout=180.0 children=1> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Play' emoji=<PartialEmoji animated=False name='▶️' id=None> row=None>:
2
Traceback (most recent call last):
3
File "C:UsersChinmay KrishnaAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscorduiview.py", line 371, in _scheduled_task
4
await item.callback(interaction)
5
File "d:Programmingpythonintellijintellij_pythonAxC-777-Musicno_upload.py", line 137, in play_button_callback
6
print(title_input_modal.children[0].value)
7
AttributeError: type object 'title_input_modal' has no attribute 'children'
8
Ignoring exception in view <View timeout=180.0 children=1> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Play' emoji=<PartialEmoji animated=False name='▶️' id=None> row=None>:
9
Traceback (most recent call last):
10
File "C:UsersChinmay KrishnaAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesdiscorduiview.py", line 371, in _scheduled_task
11
await item.callback(interaction)
12
File "d:Programmingpythonintellijintellij_pythonAxC-777-Musicno_upload.py", line 137, in play_button_callback
13
print(title_input_modal.children[0].value)
14
AttributeError: type object 'title_input_modal' has no attribute 'children'
15
Advertisement
Answer
You can store the value in your class as a property and access it once the modal has been dismissed. To do so you will have to modify your class like this.
JavaScript
1
15
15
1
class title_input_modal(Modal):
2
def __init__(self):
3
super().__init__("Audio Title Input")
4
5
#
6
self.val = None
7
8
self.add_item(InputText(label = "Audio Title", style = discord.InputTextStyle.short))
9
10
async def callback(self, interaction: discord.Interaction):
11
12
self.val = self.children[0].value
13
await interaction.response.send_message(val)
14
self.stop()
15
Then from where you call your modal, you will have to initialise it like this so that you can then access its properties
JavaScript
1
5
1
title_modal = title_input_modal()
2
await send_modal(title_modal)
3
await title_modal.wait()
4
print(title_modal.val)
5
Without the stop and wait methods, your bot won’t wait for the modal interaction to be over, so you won’t be able to access the value from the user.