RuntimeError: Event loop is closed Asyncio Scheduler

Viewed 69

I am making a python bot that every day posts the food in my school to a discord server.

For this python bot to publish every weekday I am using Scheduler. When posting the first message everything looks fine, but the second message gives an error. (BTW i am running this on a rpi and the plan is to use cronjob but thats not really working for me right now so this is temporary solution)

Here is my code:

timer.py:

import schedule
import time
from temp import getTemp
import send

def callSend():
    send.doSend(getTemp())
    print("Ran funtion callSend")
    
schedule.every().monday.at("10:30").do(callSend)
schedule.every().tuesday.at("07:45").do(callSend)
schedule.every().wednesday.at("07:45").do(callSend)
schedule.every().thursday.at("20:38").do(callSend)
schedule.every().thursday.at("20:39").do(callSend)
schedule.every().friday.at("07:45").do(callSend)

while True:
    schedule.run_pending()
    time.sleep(1)

send.py:

from compose import getMessage
import discord
def doSend(temp="unknown"):
    TOKEN = "actual_token_here"

    client = discord.Client()

    @client.event
    async def on_ready():
        print("connected")
        global cpu_temp
        mathcatten = client.get_channel(actualchannelnumberhere)
        logchatten = client.get_channel(actualchannelnumberhere)
        
        message = getMessage()
        
        await logchatten.send(message)
        await logchatten.send("Meesage for today is now sent! It said:")
        await logchatten.send(message)

        await client.close()
        print("closed client")
        
        
    print("ran client")
    client.run(TOKEN)
    print("doSend is done")     

FYI the channel "logchatten" is a log channel and will be replaced with "matchatten" in the final version. (logchatten and matchatten is swedish btw)

I know that the error has something to do with me closing the client (await client.close()) but I have no idea what to do to solve it since break() doesn't work and if I don't close the connection I am assuming the bot will stay connected until I stop the script manually.

0 Answers
Related