首页 > 解决方案 > 我的机器人没有以正确的延迟回复

问题描述

我创建了一个 ping 命令,使用以下代码响应机器人延迟:

# To ping the bot (latency)
  @commands.command()
  async def ping(self, ctx):
    try:
      latency = round(client.latency * 1000, 1)
      await ctx.send(f"Pong! Latency: {latency}ms")
    except:
      await ctx.send(f"Error getting latency")

但是,机器人回复“Pong!延迟:nanms”,而不是“Pong!延迟:ms”我没有发现代码有任何错误,但由于某种原因,它说它不是数字。你能帮我找出错误吗?

标签: pythondiscord.py

解决方案


用这个修改

    @commands.command()
    async def ping(self,ctx):
        await ctx.send(f"**{round(self.bot.latency * 1000)}** ms"))

不会是客户端延迟,而是self.bot.latency


推荐阅读