#๐Ÿ”’ Cancelling current bot task

41 messages ยท Page 1 of 1 (latest)

rare flame
#

So i have a code


                        if not variable:
                                await msg.channel.send("There are missing data... please wait")
                                def check(m, user):
                                        return user == msg.author.id and m.channel == msg.channel
                                if not status:
                                        await msg.channel.send("What would u like as a nickname?")
                                        reply = await bot.wait_for("message", timeout=60.0, check = check())
                                        try:
                                                await c.execute("INSERT INTO status(id, lvl, hp, hpr, xp, xpr, name, race, role, fighting_style, bal), VALUES(?,?,?,?,?,?,?,?,?,?,?)", (msg.author.id, 1, 10,10, 0, 15, reply.content, ))
                                        except asyncio.Timeout:
                                                await msg.channel.send("You took to long.")
                                                

If you see an error you dont need to correct, i havent run the program yet.

But i want to know if there's a way i can cancel the task after the timeout, so they have to try again.

urban bladeBOT
#

@rare flame

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rare flame
wispy echo
#

Check the docs for the function, it has examples

#

If the timeout is reached, an error is raised, which you can catch and handle

oak fog
#

You probably want an async with asyncio.timeout(...): ...

rare flame
#

I dont mean the statement alone, i want it to stop the task completely without stopping the whole on_message event.

oak fog
#

Which task do you want to cancel?

#

Show the whole async def

wispy echo
#

!paste

urban bladeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

oak fog
#

except asyncio.Timeout doesn't work

#

It's not an Exception class

#

You need except TimeoutError

wispy echo
#

Why would the SQL query timing out mean that the user took too long?

rare flame
rare flame
#

alr thanks

narrow cloak
#

@rare flame which module are you using for sql?

wispy echo
#

You're not going to continue the function while waiting for the user to respond

narrow cloak
#

and execute part in else

rare flame
#

either way thanks for pointing that out

narrow cloak
rare flame
#

I always assume it would continue the function even if the error propped

narrow cloak
#

no no thats okay, we learn from our mistakes

rare flame
rare flame
narrow cloak
rare flame
#

either way, thanks for the help appreciate it.

urban bladeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.