如何使用discord.py生成更改状态的代码



我想要一些代码,可以将状态从Playing<使用task.loop()每30分钟播放{服务器数量}服务器的帮助

当前代码:

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

错误: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()

相关内容

最新更新