python - Discord.py task.loop 用于更改齿轮不工作的状态

标签 python discord.py

import discord
from discord.ext import commands, tasks

from itertools import cycle

status = cycle(['status 1', 'status 2', 'status 3'])

class Example(commands.Cog):


    def __init__(self, dBot):
        self.dBot = dBot
        self.dBot.change_stats.start()

    #EVENTS
    @commands.Cog.listener()
    async def on_member_join(self, context, member):
        await context.send(f'Member {member.mention} has joined!')

    #TASKS
    @tasks.loop(seconds=10.0)
    async def change_stats(self):
        await self.dBot.change_presence(activity=discord.Game(next(status)))

    #COMMANDS
    @commands.command()
    async def ping(self, context):
        await context.send("Pong!")

    @commands.command()
    async def clear(self, context, amount=5):
        await context.channel.purge(limit=amount)

def setup(dBot):
    dBot.add_cog(Example(dBot))

这是 cog 文件中上面的代码,其中任务无法正常工作,除此之外其他一切正常。

我不断收到的错误是“AttributeError:‘Bot’对象没有属性‘change_stats’”

感谢任何帮助,谢谢。

最佳答案

change_stats 将是 Example 对象的一个​​属性:

self.change_stats.start()

查看 Recipes 中的示例部分,您可以在其中找到 cogs 中任务的更多用法。

关于python - Discord.py task.loop 用于更改齿轮不工作的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58843328/

相关文章:

python - 用 nltk 解析最有效的方法

python - 删除文本 channel 中的所有消息

python-requests - Discord-py - 使用 Guild ID 作为 Guild 对象

python - 不和谐机器人在哪里可以找到 ffmpeg buildpack heroku

python - discord.py 机器人重写 AttributeError : 'Bot' object has no attribute 'send_message'

python - 是否有任何资源可以让 Python 2.7 开发人员跟上 Python 3.3 的速度?

python - 'int' 对象没有属性 '__getitem__'

Python拟合均匀分布

python - 使用 webpy 和 urllib2 发送文件

python - 尽管启用了管理员权限和两种意图,为什么我的机器人无法踢用户?