Skip to content
Advertisement

Sending a message 2 hours after the last message discord py

So, I can’t figure out how to make my discord bot send a message 2 hours after the last message. The problem for me is that I want the bot to reset the 2 hours timer if a new message comes, but I don’t know how to do that without breaking the program. Can anyone help me out?

Advertisement

Answer

To point you in a general direction. You want something like

await asyncio.sleep(60*60*2)
do_stuff

One way would be to take the above lines in a function and run them as a task in asyncio and you somewhere store it as a persitent variable.
If the command arrives you take the old task, cancel it, start a new one.

async def nested():
    await asyncio.sleep(60*60*20)
    return 42

async def schedule():
    # Schedule nested() to run soon concurrently
    # with "main()".
    bot.mytask = asyncio.create_task(nested())

    # "task" can now be used to cancel "nested()", or
    # can simply be awaited to wait until it is complete:
    await bot.mytask

async def cancelit():
    bot.mytask.cancel()
    # reshedule it...
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement