【问题标题】:How can i show guilds number on status?如何在状态上显示公会编号?
【发布时间】:2021-09-28 18:20:37
【问题描述】:

我正在使用 discord.py,我想显示我的机器人当前所在的服务器数量 它工作但它没有更新我应该修复什么?

我正在用 Python 写东西(学习中),我从网上看到了这段代码

这是我的代码:

@bot.event
async def on_ready():
  activity = discord.Game(name=f"k.cmd for help! | {len(bot.guilds)} Guilds", type=3)
  await bot.change_presence(status=discord.Status.idle, activity=activity)
  print(f"logged in as {bot.user}")

【问题讨论】:

  • 嗨,你重新运行你的机器人了吗?这段代码还会引发任何错误吗?
  • 它在我重新启动机器人时更新,但我想让它始终更新

标签: python discord discord.py


【解决方案1】:

不要像 RiveN 的回答中那样使用while True: 循环,这将基本上每秒更新一次机器人的状态,最终会导致速率受限且非常滞后。

使用discord.ext.tasks:

from discord.ext import tasks #at the start of your code

@tasks.loop(minutes = 1)
async def change_status():
    activity = discord.Game(name=f"k.cmd for help! | {len(bot.guilds)} Guilds", type=3)
    await bot.change_presence(status=discord.Status.idle, activity=activity)

@change_status.before_loop
async def before_changing_status():
    await bot.wait_until_ready()

change_status.start()
bot.run(your_token_here)

这将每分钟更新你的机器人的状态,并且在这样做时它不会变得滞后。

【讨论】:

  • 它给了我“on_ready traceback 中的异常”编辑:我得到它是因为我忘记删除 sry 的代码。
猜你喜欢
  • 1970-01-01
  • 2021-08-15
  • 1970-01-01
  • 2020-08-31
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多