0

so I've been trying to make my discord bot send a message every day at 12:30 UCT but i cant seem to get my code to work I'm not sure if its not working because of incorrect code or because its on replit or whatever else the issue could be as i get no errors from this it just send the message once it loads online and that's all.

    import datetime, asyncio

    bot = commands.Bot(command_prefix="+")
    
    Async def on_Ready():
    await schedule_daily_message()
    
    async def schedule_daily_message():
    now = datetime.datetime.now()
    then = now+datetime.timedelta(days=1)
    then.replace(hour=12, minute=30)
    wait_time = (then-now).total_seconds()
    await asyncio.sleep(wait_time)
    
    channel = bot.get_channel(Channel_id)
    
    await channel.send("Enemies Spawned!")

    client.run(os.getenv('TOKEN'))
Ghost
  • 1

1 Answers1

0

await asyncio.sleep is non blocking. Your script will execute beyond that statement. You will need to use time.sleep, which that will block all execution of code until the timer has run out.

See here for a more in depth explanation and how to use this in functions: asyncio.sleep() vs time.sleep()

A way to implement a function that posts a message to a channel after a delay could look like this:

async def send_after_delay(d, c, m):
    time.sleep(d)
    await c.send(m)

Calling this function asynchronously allows you to continue with code execution beyond it, while still waiting past the calling of the function to send the message.

the_strange
  • 296
  • 2
  • 10
  • d, c, and m are your delay, channel-Object and message to be posted in the channel respectively – the_strange Oct 25 '22 at 19:20
  • thank you so much, after looking though old youtube videos and forums so i could understand what d, c and m meant, i only wish i knew how to make this more time based rather then delay based – Ghost Oct 25 '22 at 19:38
  • also one more question, so I've got it all working now all thanks to you but there's one issues occurring, while the loop is started all other commands for my bot are unusable until the time has finished for my scheduled message – Ghost Oct 25 '22 at 20:12