1

So I need to know how to make my python discord bot do something at a certain time because I want it to clear all messages in a channel at around 5:00am when no one is on I will be using the date time module but the problem is I do not want it to interfere with the rest of my bot is there any way I can do this. I know that there are other threads about this but none of them bring up the part about interference with the rest of there bot. And I am using repl.it to run the bot in case that is needed to be known.

  • 1
    It would be helpful to share your code, and what you have tried already. It would also help to explain what you mean by "interfering". Importing a module in python doesn't normally interfere with existing code, unless you change the existing code. – Geekfish Mar 23 '19 at 17:26
  • i have not tried anything because the only way I can think of would be a while loop which would interfere with my code already there – spotandjake Mar 23 '19 at 19:26

1 Answers1

1

You can create an asynchronous background task. During bot startup, calculate how many seconds are left to 5 AM and then sleep the task until then.

There is no reason that it should "interfere" with the rest of your code. I assume that you tried using blocking functions such as time.sleep(). You can use asyncio.sleep() instead.

summer
  • 139
  • 4
  • and I need it to go off at 5 am which means I cannot use a time.sleep I need something more like if datetime = 5am how would I do that with asyncio – spotandjake Mar 23 '19 at 19:28
  • You can use ```asyncio.sleep()```. Simply calculate how many seconds there are to 5 AM when the bot starts up. You don't need to constantly check if datetime == 5 am. – summer Mar 23 '19 at 20:00
  • Lucy I was having trouble writing the code could you give me a code sample of what the complete code would look like – spotandjake Mar 25 '19 at 00:55