简体   繁体   English

如何使用 discord.py 制作更改状态的代码

[英]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()我想要一些代码,使用task.loop()每 30 分钟将状态从 Playing <help 切换到 Playing {number of servers} 服务器

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我需要使用client.guild而不是bot.guild的代码

You have to make the Stat variable global otherwise it won't work.您必须使Stat变量成为全局变量,否则它将不起作用。 You also can't use Stat = not Stat .您也不能使用Stat = not Stat Use Stat = None instead.改用Stat = None 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.如果您希望您的状态每 30 分钟更改一次,则必须每 30 分钟将Stat变量更改为 True、None 等。 But in your code, you're changing it to None every time.但是在您的代码中,您每次都将其更改为None

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:我不确定你是如何开始你的任务的,但是如果你得到'NoneType' object has no attribute 'change_presence' ,那么在on_ready事件中开始你的任务:

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

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

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