【问题标题】:How to make code that changes status using discord.py如何使用 discord.py 制作更改状态的代码
【发布时间】:2022-01-25 19:08:44
【问题描述】:

我想要一些代码,使用 task.loop() 每 30 分钟将状态从 Playing

当前代码:

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 变量上使用 global 关键字,而且,你是否将你的 Bot 定义为客户端或机器人? (我的意思是,你的 Bot 对象的变量名是什么?)
  • 我把它定义为客户端
  • 你是否将 Stat 变量设置为函数内部的全局变量?

标签: python discord.py


【解决方案1】:

您必须将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()

【讨论】:

  • 它对你有用吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 2016-12-14
  • 2019-05-26
  • 1970-01-01
  • 2020-09-21
相关资源
最近更新 更多