如何解决尝试在齿轮中使用client.latency时出错
我正在尝试对我的机器人使用ping命令,而该命令的代码在嵌齿轮中。我对出什么问题有个主意,但是我不知道如何解决,因为我是新来的。每当我使用“ f.ping”命令时,都会出现以下错误:
Ignoring exception in command ping:
Traceback (most recent call last):
File "C:\Users\josep\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py",line 903,in invoke
await ctx.command.invoke(ctx)
File "C:\Users\josep\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py",line 847,in invoke
await self.prepare(ctx)
File "C:\Users\josep\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py",line 784,in prepare
await self._parse_arguments(ctx)
File "C:\Users\josep\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py",line 690,in _parse_arguments
transformed = await self.transform(ctx,param)
File "C:\Users\josep\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py",line 535,in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: client is a required argument that is missing.
这是我的ping.py代码:
import discord
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self,client):
self.client = client
@commands.command()
async def ping(self,ctx):
await ctx.send(f'Pong!' ({round(client.latency * 1000)}ms))
def setup(client):
client.add_cog(Ping(client))
我已将问题/错误缩小到({round(client.latency * 1000)}ms
部分,但是我不知道如何解决。删除该部分后,该命令运行正常。任何帮助表示赞赏。
解决方法
您遇到两个错误。
第一手:f字符串引用不正确:
错误:
await ctx.send(f'Pong!' ({round(client.latency * 1000)}ms))
右:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
第二:由于这是一个嵌齿轮,因此您应该使用self.client.latency,请记住 init 函数,您已为 self.client = client分配了
错误:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
右:
await ctx.send(f'Pong! ({round(self.client.latency * 1000)}ms)')
,
看来您有2个不同的错误,让我们帮助您重回正轨。
首先,您的f字符串有误。引号不正确,因此您需要将其放在正确的位置。
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
现在,您的另一个错误是因为您在类中编码,所以使用了 client.latency 而不是 self.client.latency 。因此,这将是正确的代码:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
导入不和谐 从discord.ext导入命令中
class Ping(commands.Cog):
def __init__(self,client):
self.client = client
@commands.command()
async def ping(self,ctx):
await ctx.send(f'Pong! ({round(self.client.latency * 1000)}ms'))
def setup(client):
client.add_cog(Ping(client))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。