为了账号安全,请及时绑定邮箱和手机立即绑定

尝试在 cog 中使用 client.latency 时出错

尝试在 cog 中使用 client.latency 时出错

侃侃尔雅 2023-05-23 16:13:25
我正在尝试对我的机器人使用 ping 命令,它的代码在 cog 中。我知道出了什么问题,但我不知道如何解决它,因为我是新手。每当我使用“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 discordfrom discord.ext import commandsclass 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,但我不知道如何解决它。该命令在删除该部分后工作得很好。任何帮助表示赞赏。
查看完整描述

2 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

您似乎有 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 导入命令导入 discord


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))

查看完整回答
反对 回复 2023-05-23
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

你有两个错误。

首先:f 字符串引号不正确:

错误的:

await ctx.send(f'Pong!' ({round(client.latency * 1000)}ms))

正确的:

await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')

第二:因为这是一个齿轮,你应该使用 self.client.latency,记住初始化函数,你分配了self.client = client

错误的:

await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')

正确的:

await ctx.send(f'Pong! ({round(self.client.latency * 1000)}ms)')


查看完整回答
反对 回复 2023-05-23
  • 2 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信