#How can I check whether someone responds to my modal or just presses cancel?

1 messages · Page 1 of 1 (latest)

twin mural
#

for a response I have used async def callback and that works but I can't understand how to see if someone pressed cancel

#

depending on the response from modal, I send an embed with 2 buttons which further completes the whole transaction

iron plover
#

Do you want to send a message when they press Cancel too?

twin mural
#

the second embed depends upon the value sent in the modal

iron plover
#

Can you show the Modal, please?

twin mural
#

code or the modal?

cobalt flume
#

Probably not the best name for a bot /nbr

#

Very cool idea though 😎

iron plover
twin mural
#

trying out new code for the main one

cobalt flume
#

Ah

#

I'm too lazy to set up multiple bots.

#

I just block the unstable features for people who aren't Devs haha

twin mural
cobalt flume
#

Hey can I just take a peek at the code? It'll help me with some modals I'm working on. (Don't worry I won't steal your code.)

#

nor will I steal your idea

#

I don't really dabble in crypto

twin mural
#

one sec

#
class MyModal(discord.ui.Modal):
            def __init__(self, *args, **kwargs) -> None:
                super().__init__(*args, **kwargs)
                self.add_item(discord.ui.InputText(label="Receiving Coin/Token (Ticker only)"))

            async def callback(self, interaction: discord.Interaction):
                if self.children[0].value is None:
                    print('ok this part works')
                    await message.channel.send(f'Please enter a valid Coin/Token in the popup. Kindly try again.')
                    await message.channel.send(f'{tip_prefix} {user} {what_has_been_tipped}')
                    return
                self.children[0].value = (self.children[0].value).upper()
#
if self.children[0].value is None:
                    print('ok this part works')
                    await message.channel.send(f'Please enter a valid Coin/Token in the popup. Kindly try again.')
                    await message.channel.send(f'{tip_prefix} {user} {what_has_been_tipped}')
                    return

this part was an attempt to see if someone presses cancel

iron plover
#

I wonder if you can use on_timeout() to build what you need. rooThink

twin mural
#

that's already there

#

but I used on_timeout for the buttons

iron plover
#

Then, in there, check if the field is empty or not.

#

If empty, assume they pressed Cancel.

twin mural
#

let me try the timeout thing for modal

cobalt flume
#

thonkus

twin mural
#
C:\Users\hp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ui\modal.py:103: RuntimeWarning: coroutine 'on_message.<locals>.MyModal.on_timeout' was never awaited
  self.loop.create_task(self.on_timeout(), name=f"discord-ui-view-timeout-{self.id}")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

#
 class MyModal(discord.ui.Modal):
            _timeout_again_and_again = True
            async def on_timeout(self):
                global _timeout_again_and_again
                if _timeout_again_and_again == False:
                    return
                elif _timeout_again_and_again == True:
                    await message.channel.send("Swap Cancelled, you took too long to enter an input.")
                    await message.channel.send(f'{tip_prefix} {user} {what_has_been_tipped}')
                    return

#

did this

iron plover
#

What do you need that boolean for?

#

You probably need to send a self.stop() when the timeout happens.

twin mural
iron plover
twin mural
#

how can I solve this

#

C:\Users\hp\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\ui\modal.py:103: RuntimeWarning: coroutine 'on_message.<locals>.MyModal.on_timeout' was never awaited
self.loop.create_task(self.on_timeout(), name=f"discord-ui-view-timeout-{self.id}")
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

twin mural
#

await interaction.response.send_modal(MyModal(title = "Swap Completion", timeout = 1)) is this ok

iron plover
#

Yes, probably.

twin mural
late bluff
#

you can't check if someone pressed cancel

#

it's not an event that discord broadcasts, all it does is dismiss the modal

iron plover
twin mural
#

I think the timeout thing would be my best bet

twin mural
iron plover
twin mural
#

ok I got it

#

the error is in the way I set the timeout

#

earlier I was using this:

#

await interaction.response.send_modal(MyModal(title = "Swap Completion", timeout = 30))

#

the timeout= 30 part is causing the problem

#

the on_timeout function is fine

iron plover
#

How do you know? Without that, the function doesn't even get called.

twin mural
#

used timeout for one of my buttons before:

#

and in this modal, it's treated as a literal

iron plover
twin mural
#

maybe that's the problem

twin mural
#

quite sure my on_timeout function is fine since it's the same as the timeout function my other two buttons use, which work perfectly

#

i saw setTimeout somewhere on SO but I think that was js

#

maybe something similar would help out

#

and if timeout doesn't work, I could make a variable with the value of 30 and then loop it such that it reduces to 0 in 30s and then I execute the on_timout function

#

not ideal, but should work

iron plover
#

I'm looking at the code for Pycord and Modal has a timeout parameter, although it's not listed in docs.