简体   繁体   中英

How to make code that changes status using discord.py

I want some code that switches the status from Playing <help to Playing {number of servers} servers every 30 mins using task.loop()

current code:

Stat = True

@tasks.loop(minutes=30)
async def change_status():
    new_status = "<help" if Stat else f"{len(client.guilds)} servers"
    await client.change_presence(status=discord.Status.online, activity=discord.Game(new_status))
    Stat = not Stat

error: Stat is not defined

I need code that uses client.guild not bot.guild please

You have to make the Stat variable global otherwise it won't work. You also can't use Stat = not Stat . Use Stat = None instead. If you want your status to change every 30 minutes you have to change the Stat variable every 30 minutes to True, None, and so on. But in your code, you're changing it to None every time.

Stat = True

@tasks.loop(minutes=30)
async def change_status():
    global Stat
    if Stat:
        new_status = "<help"
        Stat = None
    else:
        new_status = f"{len(client.guilds)} servers"
        Stat = True
    
    await client.change_presence(status=discord.Status.online, activity=discord.Game(new_status))

I'm not sure how you are starting your task, but if you get 'NoneType' object has no attribute 'change_presence' , then start your task in the on_ready event:

@client.event
async def on_ready():
    change_status.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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