如何在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点打印嘿它的时间。但这是行不通的。早上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传递什么参数,我不能在齿轮中留空,而且self也会出错,所以我真的不知道该传递什么。

解决方法

您的问题在这一行:

schedule.every().day.at("10:00").do(job)

您正在将函数/方法job传递到调度程序中,而没有绑定对象。因此,当作业运行时,调度程序将其称为“裸方法”,因此它不会为该方法提供self参数。

我不确定在SmanageCog定义的类级别上拥有代码会怎么回事,但是如果您的代码不在类定义之外,则可以执行以下操作:

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)

也不属于类定义。