问题
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