我要每天运行任务,00:00(discord.py)
I want to run task everyday, 00:00 (discord.py)
我每天将代码放在一起 运行 任务,每个 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
(我猜类似于 sniffet)并且它应该可以工作。我不明白为什么这会给我错误。
(差不多)完整代码
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
所以我真的需要帮助。
对于 AttributeError: 'datetime.timedelta' object has no attribute 'hours'
,您从另一个对象中减去一个 datetime
对象,因此您在 return 中得到一个 timedelta
对象。此对象没有 hours
属性:只有 days
、seconds
和 microseconds
。因此 AttributeError
.
不过,在你的情况下,因为它是 timedelta
整整一天,所以 now
和 nextday
的小时和分钟将相同,所以你可以得到它们从那里开始。
对于你的第二个问题,不太清楚,但我相信你正在尝试访问名称为 change_interval
的方法。如果是这样,则您正在尝试从对象名称 (coma_schedule
) 而不是当前实例 (self
) 访问函数。由于您尝试访问的函数不是静态的,因此您无法从 abc
访问它:您需要调用 self.change_interval
。
我每天将代码放在一起 运行 任务,每个 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
(我猜类似于 sniffet)并且它应该可以工作。我不明白为什么这会给我错误。
(差不多)完整代码
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
所以我真的需要帮助。
对于 AttributeError: 'datetime.timedelta' object has no attribute 'hours'
,您从另一个对象中减去一个 datetime
对象,因此您在 return 中得到一个 timedelta
对象。此对象没有 hours
属性:只有 days
、seconds
和 microseconds
。因此 AttributeError
.
不过,在你的情况下,因为它是 timedelta
整整一天,所以 now
和 nextday
的小时和分钟将相同,所以你可以得到它们从那里开始。
对于你的第二个问题,不太清楚,但我相信你正在尝试访问名称为 change_interval
的方法。如果是这样,则您正在尝试从对象名称 (coma_schedule
) 而不是当前实例 (self
) 访问函数。由于您尝试访问的函数不是静态的,因此您无法从 abc
访问它:您需要调用 self.change_interval
。