首页 > 解决方案 > Discord.py> 如何安排一个操作?我尝试使用时间表

问题描述

我正在尝试每 30 分钟安排一次手术。但是这段代码不起作用。

def Advice():
    print("30 minutes")
@client.event
async def on_ready():
    schedule.every(30).minutes.do(Advice)

你能帮我么?

标签: python-3.xdiscord.pydiscord.py-rewrite

解决方案


可能是您正在寻找的。这是给你的一个例子。

class Heartbeat(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.heartbeat.start()

    def cog_unload(self):
        self.heartbeat.stop()

    @tasks.loop(seconds=45)
    async def heartbeat(self):
        if not self.bot.debug:
            try:
                self.bot.log.info("[UptimeRobot HeartBeat] - Sending heartbeat Request")
                req = await self.bot.session.get(os.getenv("UPTIMEROBOT_URL"))
                response = await req.json()
                self.bot.log.info(f"[UptimeRobot Heartbeat] - UptimeRobot says: {response['msg']}")
            except Exception as e:
                self.bot.sentry.capture_exception(e)

    @heartbeat.before_loop
    async def before_update_loop(self):
        await self.bot.wait_until_ready()


def setup(bot):
    bot.add_cog(Heartbeat(bot))

推荐阅读