我的ping命令中存在bot.latency问题。
首先,我这样声明我的机器人实例:
bot = commands.Bot(
description='Genos Bot Help Section - prefix: ^ ',
command_prefix='^',
case_insensitive=True,
guild_subscriptions=True,
intents=intents
) # I have this in one line in my program, but I wrote it like that in order to be easily read.
在那之后,我发出了ping命令:
# I used commands.command because I have cogs for my discord bot.
@commands.command(name="ping")
async def ping(self, ctx: commands.Context):
"""Ping command!"""
await ctx.send(f'Pong! {round(bot.latency * 1000)} ms')
bot.latency
的问题是,尽管float
在文档中是如何编写的,但它还是返回了nan
,并且它无法进行转换,这给了我以下错误:
ValueError: cannot convert float NaN to integer
我该如何解决这个问题?
简单修复!
因为您使用的是cogs,所以self
是必需的。
这应该可以解决你的问题:
await ctx.send(f'Pong! {round(self.bot.latency * 1000)} ms')
完整代码:
@commands.command(name="ping")
async def ping(self, ctx: commands.Context):
"""Ping command!"""
await ctx.send(f'Pong! {round(self.bot.latency * 1000)} ms')