Discord.py> How to schedule a operation? i tried using schedule

泪湿孤枕 提交于 2021-02-11 14:13:03

问题


I'm trying to schedule a operation every 30 minutes. But this code isn't working.

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

Can you please help me?


回答1:


This might be what you're looking for. Here's an example for you.

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))


来源:https://stackoverflow.com/questions/60624445/discord-py-how-to-schedule-a-operation-i-tried-using-schedule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!