RuntimeError:事件循环已关闭 Asyncio Scheduler

发布于 2025-01-16 22:08:12 字数 1742 浏览 4 评论 0原文

我正在制作一个 python 机器人,它每天都会将我学校的食物发布到不和谐服务器上。

为了让这个 python 机器人在每个工作日发布,我使用 Scheduler。发布第一条消息时,一切看起来都很好,但第二条消息给出了错误。 (顺便说一句,我正在 rpi 上运行它,计划是使用 cronjob,但这现在对我来说并不真正有用,所以这是临时解决方案)

这是我的代码:

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 频道“logchatten”是一个日志通道,在最终版本中将被替换为“matchatten”。 (顺便说一句,logchatten 和 matchatten 是瑞典语)

我知道该错误与我关闭客户端有关(await client.close()),但我不知道如何解决它,因为 < code>break() 不起作用,如果我不关闭连接,我假设机器人将保持连接,直到我手动停止脚本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文