【发布时间】:2021-03-15 09:44:04
【问题描述】:
我每天 00:00 将代码放在一起运行任务(恰好是一天变化的时间)。但这给了我一行两个错误。
coma_schedule.change_interval(hours=interval.hours, minutes=interval.minutes, seconds=interval.seconds)
AttributeError: 'datetime.timedelta' object has no attribute 'hours'
AttributeError: type object 'coma_schedule' has no attribute 'change_interval'
好吧,因为我的代码来自 discord.py 的官方 Discord 服务器的 tag(我猜类似于 snifet),它应该可以工作。我不明白为什么这会给我错误。
(几乎)完整代码
import discord
import datetime
import asyncio
import psycopg2 as pg2
from discord.ext import commands, tasks
class coma_schedule(commands.Cog, name='coma_schedule'):
def __init__(self, bot):
print('[COMA SCHEDULER] coma scheduler is ready')
self.bot = bot
self.coma_schedule.start()
def cog_unload(self):
self.coma_schedule.cancel()
@tasks.loop(hours=24)
async def coma_schedule(self):
now = datetime.datetime.now()
nextday = now + datetime.timedelta(days=1)
# Do my stuff
# Do my stuff ends
now = datetime.datetime.now()
interval = nextday - now
coma_schedule.change_interval(hours=interval.hours, minutes=interval.minutes, seconds=interval.seconds)
@coma_schedule.before_loop
async def coma_schedule_before(self):
now = datetime.datetime.now()
nextday = datetime.datetime.now()
nextday.replace(hour=0, minute=0, second=0)
if nextday < now:
nextday.replace(day=now.day + 1)
await asyncio.sleep((nextday - now).total_seconds())
def setup(bot):
bot.add_cog(coma_schedule(bot))
我不知道我应该只用那个 AttributeError 解决什么问题,所以我真的需要帮助。
【问题讨论】:
标签: python scheduled-tasks discord.py