Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在cog中创建随机状态更改器?_Python_Discord.py_Discord.py Rewrite - Fatal编程技术网

Python 如何在cog中创建随机状态更改器?

Python 如何在cog中创建随机状态更改器?,python,discord.py,discord.py-rewrite,Python,Discord.py,Discord.py Rewrite,我的目标是,我的discord机器人在一段时间后将其状态更改为随机状态,我将其添加到列表中 import discord import random from discord.ext import commands, tasks class Status(commands.Cog): def __init__(self, client): self.client = client self.client.random_status_loop.start

我的目标是,我的discord机器人在一段时间后将其状态更改为随机状态,我将其添加到列表中

import discord
import random

from discord.ext import commands, tasks

class Status(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.client.random_status_loop.start()

    @tasks.loop(seconds=5.0)
    async def random_status_loop(self):
        status = [
            'Value1',
            'Value2',
            'Value3',
            'Value4',
            'Value5',
            '',
            'Value6']
        await self.client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=(random.choice(status))))

def setup(client):
    client.add_cog(Status(client))
我刚刚学习了一些关于这方面的教程,对于使用Python编程仍然是新手。这是我放在cog中的内容,在主bot.py文件中,这可以正常工作,但当将其放在cog中时,会出现以下错误: discord.ext.commands.errors.Extension失败:扩展“cogs.status”引发错误:AttributeError:“Bot”对象没有属性“random\u status\u loop”

是否有我可能缺少的东西,我是否必须添加一个特定的属性才能工作?
非常感谢您的帮助。

函数
随机状态\u循环()
属于
状态类,而不是
self.client


尝试
self.random\u status\u loop.start()
intead of
self.client.random\u status\u loop\u start()

函数
random\u status\u loop()
属于
status
类,而不是
self.client


请尝试
self.random\u status\u loop.start()
intead of
self.client.random\u status\u loop\u start()

请记住,您将为此受到费率限制。Discord不喜欢你滥发它的API,每5秒更新一次状态太快了。试着每分钟/每几分钟做一次这样的尝试。记住你会因此而受到限制。Discord不喜欢你滥发它的API,每5秒更新一次状态太快了。试着每分钟/每隔几分钟做一次。