Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

python - How can I create a random status changer within a cog?

The goal I had in mind was that my discord bot changes it's status after some time to a random status I added to a list.

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

I've just been following some tutorials on this and am still new to programming with Python. This is what I put inside a cog, within the main bot.py file this works fine but when putting it in a cog it gives me the following error: discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.status' raised an error: AttributeError: 'Bot' object has no attribute 'random_status_loop'

Is there something I might be missing, do I have to add a specific attribute for it to work? Any help is appreciated.

question from:https://stackoverflow.com/questions/65540673/how-can-i-create-a-random-status-changer-within-a-cog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The function random_status_loop() belongs to the Status class, and not self.client.

Try self.random_status_loop.start() intead of self.client.random_status_loop_start()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...