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

我正在尝试对我的机器人使用 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 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,但我不知道如何解决它。该命令在删除该部分后工作得很好。任何帮助表示赞赏。


侃侃尔雅
浏览 90回答 2
2回答

ibeautiful

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

慕哥6287543

你有两个错误。首先: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)')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python