Discord Bot报错:'Bot'对象无'Latency'属性,寻求解决方案
解决discord.py中Bot对象无Latency属性的错误
问题出在Python的大小写敏感性上——discord.py的Bot类提供的WebSocket延迟属性是小写的latency,而非你代码里写的大写开头的Latency。
修正后的代码如下:
import discord from discord.ext import commands, tasks intents = discord.Intents.all() intents.message_content = True client = commands.Bot(command_prefix = "+", intents=intents) @client.command(name="ping", help="This is a ping command") async def ping(ctx): await ctx.send(f"**Pong** Latency: {round(client.latency * 1000)}ms")
补充说明:client.latency返回的是WebSocket连接的延迟值(单位为秒),乘以1000转换为毫秒的处理是正确的,修正后执行+ping命令就能正常返回延迟数据了。
内容的提问来源于stack exchange,提问作者Phoenix
相关产品推荐
相关产品推荐

