简体   繁体   English

如何在discord.py中的cogs中使用调度库?

[英]How to use schedule library in cogs in discord.py?

I want the bot to do a job everyday at a specific time, I know I can do that with schedule and that is pretty simple too.我希望机器人每天在特定时间完成一项工作,我知道我可以按计划完成这项工作,而且这也非常简单。 I tried that and that worked but now I am trying to arrange that into cogs and repeatedly getting errors.我试过了,那行得通,但现在我试图把它安排成齿轮并反复出错。

cog:齿轮:

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!")

according to the above code, bot would print hey its time at 10 am everyday.根据上面的代码,bot 会在每天上午 10 点打印嘿它的时间。 But this doesn't work.但这不起作用。 It throws me error at 10 am .它在上午 10 点让我出错。 Error would be like:错误会是这样的:

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'

I don't know what argument should I pass from def job , I can't leave that empty in a cog and self also gives error so I don't really know what to pass.我不知道我应该从def job传递什么参数,我不能在 cog 中留空,而 self 也会出错,所以我真的不知道要传递什么。

Your problem is in this line:你的问题在这一行:

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

You are passing in the function/method job into the scheduler with no object bound to it.您将函数/方法job传递到调度程序中,但没有绑定对象。 So when the job runs, the scheduler calls that function as a "naked method", and so it does not provide a self argument to the method.因此,当作业运行时,调度程序将该函数称为“裸方法”,因此它不会为该方法提供self参数。

I'm not sure what's going on with you having code at the class level of your SmanageCog definition, but if your code were outside the class definition, you could do something like this:我不确定您在 SmanageCog 定义的类级别拥有代码是怎么回事,但是如果您的代码在类定义之外,您可以执行以下操作:

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

and then you'd be providing a bound method to the scheduler and it would have an object to pass as self to the method.然后你会为调度程序提供一个绑定方法,它会有一个对象作为self传递给该方法。

Do you maybe want the schedule call in your constructor?, so:你可能想要在你的构造函数中调用 schedule 吗?,所以:

def __init__(self,bot):
    self.bot = bot
    schedule.every().day.at("10:00").do(self.job)

I bet the main loop:我打赌主循环:

while True:
    schedule.run_pending()
    time.sleep(1)

doesn't belong in the class definition either.也不属于类定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM