如何在 discord.py 的 cogs 中使用调度库?
How to use schedule library in cogs in discord.py?
我希望机器人每天在特定时间完成一项工作,我知道我可以按计划完成这项工作,而且这也非常简单。我试过了并且成功了,但现在我正在尝试将其安排到齿轮中并反复出现错误。
齿轮:
import discord
from discord.ext import commands, tasks
import discord.utils
from discord.utils import get
import schedule
import asyncio
import time
class SmanageCog(commands.Cog, name='Manager') :
def __init__(self,bot):
self.bot = bot
def job(self):
print("HEY IT'S TIME!")
schedule.every().day.at("10:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
def setup(bot):
bot.add_cog(SmanageCog(bot))
print("Manager is loaded!")
根据上面的代码,bot 会在每天上午 10 点打印 hey its time。但这不起作用。它在上午 10 点向我抛出错误。
错误类似于:
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 653, in load_extension
self._load_from_module_spec(spec, name)
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 599, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.smanage' raised an error: TypeError: job() missing 1 required positional arguments: 'self'
我不知道我应该从 def job
传递什么参数,我不能在 cog 中留空并且 self 也会给出错误所以我真的不知道要传递什么。
您的问题出在这一行:
schedule.every().day.at("10:00").do(job)
您将 function/method job
传递给调度程序,但没有绑定任何对象。因此,当作业运行时,调度程序将该函数称为“裸方法”,因此它不会向该方法提供 self
参数。
我不确定你的代码在 SmanageCog 定义的 class 级别是怎么回事,但如果你的代码在 class 定义之外,你可以这样做:
schedule.every().day.at("10:00").do(SmanageCog(bot).job)
然后您将向调度程序提供一个绑定方法,它将有一个对象作为 self
传递给该方法。
您是否希望在您的构造函数中调用计划?所以:
def __init__(self,bot):
self.bot = bot
schedule.every().day.at("10:00").do(self.job)
我赌主循环:
while True:
schedule.run_pending()
time.sleep(1)
也不属于 class 定义。
我希望机器人每天在特定时间完成一项工作,我知道我可以按计划完成这项工作,而且这也非常简单。我试过了并且成功了,但现在我正在尝试将其安排到齿轮中并反复出现错误。
齿轮:
import discord
from discord.ext import commands, tasks
import discord.utils
from discord.utils import get
import schedule
import asyncio
import time
class SmanageCog(commands.Cog, name='Manager') :
def __init__(self,bot):
self.bot = bot
def job(self):
print("HEY IT'S TIME!")
schedule.every().day.at("10:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
def setup(bot):
bot.add_cog(SmanageCog(bot))
print("Manager is loaded!")
根据上面的代码,bot 会在每天上午 10 点打印 hey its time。但这不起作用。它在上午 10 点向我抛出错误。 错误类似于:
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 653, in load_extension
self._load_from_module_spec(spec, name)
File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\ext\commands\bot.py", line 599, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.smanage' raised an error: TypeError: job() missing 1 required positional arguments: 'self'
我不知道我应该从 def job
传递什么参数,我不能在 cog 中留空并且 self 也会给出错误所以我真的不知道要传递什么。
您的问题出在这一行:
schedule.every().day.at("10:00").do(job)
您将 function/method job
传递给调度程序,但没有绑定任何对象。因此,当作业运行时,调度程序将该函数称为“裸方法”,因此它不会向该方法提供 self
参数。
我不确定你的代码在 SmanageCog 定义的 class 级别是怎么回事,但如果你的代码在 class 定义之外,你可以这样做:
schedule.every().day.at("10:00").do(SmanageCog(bot).job)
然后您将向调度程序提供一个绑定方法,它将有一个对象作为 self
传递给该方法。
您是否希望在您的构造函数中调用计划?所以:
def __init__(self,bot):
self.bot = bot
schedule.every().day.at("10:00").do(self.job)
我赌主循环:
while True:
schedule.run_pending()
time.sleep(1)
也不属于 class 定义。