#🔒 Timed action

37 messages · Page 1 of 1 (latest)

amber valve
#

What would be the best way for me to do somthing in like a day or a week?

gusty pecanBOT
#

@amber valve

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.

amber valve
#

so i pretty much going to give a role to somone then in a day remove that role what is the best way to do that considering bot downtimes e.g

#

i was thinking await asyncio.sleep(int(time)) but not a good idea is it

somber crag
#

maybe save the "remove the role" thing in a sort of agenda, with a timestamp on it; then check the agenda every minute or so

tribal cypress
#

because bot downtimes exist, you'd need to persist this in a database.

somber crag
#

the trouble with await is that if the bot crashes, it won't continue waiting once it comes back up

amber valve
#

yeah i thought that

somber crag
#

learn about sqlite; it's not hard

amber valve
somber crag
#

sqlite

amber valve
somber crag
#

best thing about it: it's already installed 🙂

amber valve
#

so log the time and check every so often?

somber crag
#

exactly

amber valve
somber crag
#

if the bot crashes, no big deal; the agenda is still saved to disk

amber valve
somber crag
#

unfortunately those docs are nowhere near enough to learn how to use it

amber valve
#

to help

somber crag
tribal cypress
#

have a table storing roles given to users, when they should expired, and whether they did. every minute (see discord.ext.tasks), request from the database all entries that should have expired by now but aren't marked as expired (that can be done with just a single SQL query, and if you make sure to have indexes on the right columns it'll even be very fast). If there's any, remove these roles and mark them as expired in the database.

amber valve
somber crag
#

if you're having trouble understanding SQL (it's a 60-year-old programming language! Of course it's hard to understand), I recommend https://www.postgresql.org/docs/current/tutorial.html -- it's for a different database, but it's pretty similar, and it's nicely written

tribal cypress
somber crag
#

ah TIL

amber valve
#

and use taks?

#

tasks,loop?

amber valve
#

ok

amber valve
#
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/bot.py", line 935, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 936, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1074, in get_code
  File "<frozen importlib._bootstrap_external>", line 1004, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/container/cogs/genaccess.py", line 54
    break
    ^^^^^
SyntaxError: 'break' outside loop
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.11/site-packages/discord/client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/main.py", line 22, in on_ready
    await client.load_extension(f"cogs.{filename[:-3]}")
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/bot.py", line 1013, in load_extension
    await self._load_from_module_spec(spec, name)
  File "/home/container/.local/lib/python3.11/site-packages/discord/ext/commands/bot.py", line 938, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.genaccess' raised an error: SyntaxError: 'break' outside loop (genaccess.py, line 54)```
#

!close

gusty pecanBOT
#
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.