簡體   English   中英

如何使用 discord.py 制作更改狀態的代碼

[英]How to make code that changes status using discord.py

我想要一些代碼,使用task.loop()每 30 分鍾將狀態從 Playing <help 切換到 Playing {number of servers} 服務器

當前代碼:

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

錯誤:未定義統計

我需要使用client.guild而不是bot.guild的代碼

您必須使Stat變量成為全局變量,否則它將不起作用。 您也不能使用Stat = not Stat 改用Stat = None 如果您希望您的狀態每 30 分鍾更改一次,則必須每 30 分鍾將Stat變量更改為 True、None 等。 但是在您的代碼中,您每次都將其更改為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))

我不確定你是如何開始你的任務的,但是如果你得到'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