Reputation: 21
So I have been trying to make a bot that will send user-given messages throughout the day with messages being a list of messages given and numMessages being the amount of messages you want sent throughout the day. Say for example if I had a list of 3 messages, but I only want them sent once per day, it will send the messages in order once per day on repeat until another command resets. So far it seems to want to work but it is exiting the try after the first message and ignoring the sleep command completely. I also wanted to experiment with this command to make it where the bot ends it's announcement sequence by calling another command, going back to waiting for another message. The code is down below if anyone can help me
if len(messages) > 6 or int(numMessages) > 6 or len(messages) < 1 or int(numMessages) < 1:
await ctx.send("There are too many or too few messages or numMessages specified. Please keep the annnouncements between 1 and 6.")
else:
currMess = 0
global endCheck
endCheck = 1
await ctx.send("Timer Started!")
while(endCheck):
await ctx.send(messages[currMess].replace("^", " "))
currMess += 1
if (currMess == len(messages)):
currMess = 0
await time.sleep(int(86400/numMessages))
except:
await ctx.send("numMessages must be a integer between 1-6. Please give a integer number")
await ctx.send("Timer Ended!")```
Upvotes: 0
Views: 319
Reputation: 11
time.sleep(time)
is not an async function so it can't be run with await
.
await
is exclusive for asynchronous functions!
Instead use asyncio.sleep(time)
from the asyncio module.
await time.sleep(time) # does not work with await
await asyncio.sleep(time) # works perfectly fine with the discord.py module and async functions
Upvotes: 1
Reputation: 1
I am not sure if await time.sleep() works like this.
Could you try using the built in sleep function of asyncio?
await asyncio.sleep(5)
And for your Exceptions, maybe try to print your error with
except Exception as ex:
print(ex)
Upvotes: 0