F
F
FlensT2020-11-17 10:33:18
Python
FlensT, 2020-11-17 10:33:18

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?

Source

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)


small log

[Tue Nov 17 08:52:12 2020] Sleeping...
[Tue Nov 17 08:53:12 2020] Sleeping...
[Tue Nov 17 08:54:12 2020] Sleeping...
[Tue Nov 17 08:55:12 2020] Sleeping...
[Tue Nov 17 08:56:12 2020] Sleeping...
[Tue Nov 17 08:57:12 2020] Sleeping...
[Tue Nov 17 08:58:12 2020] Sleeping...
[Tue Nov 17 08:59:12 2020] Sleeping...
[Tue Nov 17 09:00:12 2020] Sleeping...
[Tue Nov 17 09:01:12 2020] Sleeping...
[Tue Nov 17 09:02:12 2020] Sleeping...
[Tue Nov 17 09:03:12 2020] Sleeping...
[Tue Nov 17 09:04:12 2020] Sleeping...
[Tue Nov 17 09:05:12 2020] Sleeping...
[Tue Nov 17 09:06:12 2020] Sleeping...
[Tue Nov 17 09:07:12 2020] Sleeping...
[Tue Nov 17 09:08:12 2020] Sleeping...
[Tue Nov 17 09:09:12 2020] Sleeping...
[Tue Nov 17 09:10:12 2020] Sleeping...
[Tue Nov 17 09:11:12 2020] Sleeping...
[Tue Nov 17 09:12:12 2020] Sleeping...
[Tue Nov 17 09:13:12 2020] Sleeping...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-11-17
@FlensT

There is a suspicion that you are using a two-digit value as the "nearest" time.
%Hformats the current hour in two-digit format ( 00, 01, …, 23).
https://docs.python.org/3/library/datetime.html#st...
5LUtz43.png
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...
77NNwy.png
while True

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question