首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.py bot \ bot.latency不顾浮子返回nan

Discord.py bot \ bot.latency不顾浮子返回nan
EN

Stack Overflow用户
提问于 2020-11-11 08:16:08
回答 1查看 295关注 0票数 0

我的ping命令中的bot.latency有问题。

首先,我宣布了我的机器人实例如下:

代码语言:javascript
复制
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命令:

代码语言:javascript
复制
# 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的问题是,它返回nan,尽管float是如何在文档中编写的,并且它无法进行转换,给出了以下错误:

代码语言:javascript
复制
ValueError: cannot convert float NaN to integer

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2022-10-23 14:28:32

很容易修好!

因为您使用的是齿轮,所以需要self

这应该可以解决您的问题:

代码语言:javascript
复制
await ctx.send(f'Pong! {round(self.bot.latency * 1000)} ms')

整个代码:

代码语言:javascript
复制
@commands.command(name="ping")
  async def ping(self, ctx: commands.Context):
     """Ping command!"""
     await ctx.send(f'Pong! {round(self.bot.latency * 1000)} ms')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64782743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档