Answer the question
In order to leave comments, you need to log in
Why doesn't the Python script fire at a certain time?
In general, the problem is this, I have a script that logs into the discord using the library discord.py
, but the client is not a bot. The purpose of this script is to put "+" in the text channel of the server at 9:00 every day. And it works if it is on the PC and it is set to the nearest time, and not 9:00. But as soon as I put it on EC2 from AWS, it just skips the right time and sleeps. What is the problem?
import discord
import asyncio
import time
client = discord.Client()
@client.event
async def on_ready():
print('Client logged as {}'.format(client.user))
channel = client.get_channel(id=ID КАНАЛА)
last_days = []
f = open('log.txt', 'a')
while True:
current_day = time.strftime("%d")
current_hour = time.strftime("%H")
if current_day not in last_days and current_hour == "9":
await channel.send("+")
last_days.append(current_day)
f.write(f"======================================\n")
f.write(f"[{time.ctime()}] Sending '+'...\n")
f.write(f"======================================\n")
else:
f.write(f"[{time.ctime()}] Sleeping...\n")
await asyncio.sleep(60)
client.run("МОЙ ТОКЕН", bot=False)
Answer the question
In order to leave comments, you need to log in
There is a suspicion that you are using a two-digit value as the "nearest" time.
%H
formats the current hour in two-digit format ( 00, 01, …, 23
).
https://docs.python.org/3/library/datetime.html#st...
By the way, also: The on_ready
event can happen more than once: It's not a good idea to
use it in asynchronous code. Use asyncio tasks for this. discord.py has an extension to easily manage asyncio tasks: https://discordpy.readthedocs.io/en/stable/ext/tas...
while True
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question