首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在齿轮箱中使用client.latency时出错

在齿轮箱中使用client.latency时出错
EN

Stack Overflow用户
提问于 2020-08-14 17:36:02
回答 2查看 302关注 0票数 1

我试图在我的机器人上使用ping命令,它的代码在一个齿轮上。我知道出了什么问题,但我不知道怎么解决它,因为我是新来的。每当我使用'f.ping‘命令时,我都会得到以下错误:

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

代码语言:javascript
复制
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部分,但我不知道如何修复它。该命令在移除该部分时运行非常好。任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-15 03:12:41

似乎你有两个不同的错误,让我们帮助你回到正轨。

首先,您的f-字符串出现了错误。引号是不正确的,所以你需要把它们放在正确的位置。

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

现在,您的另一个错误是因为您在类中编码,您使用的是client.latency而不是self.client.latency。因此,这将是正确的代码:

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

从discord.ext导入命令导入不和谐

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

如果您想阅读有关self和f-string的内容,下面是一些有用的链接:自我 F-字符串

票数 2
EN

Stack Overflow用户

发布于 2020-08-14 18:36:27

你犯了两个错误。

第一:F字符串引号不正确:

错:

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

右图:

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

第二个:由于这是一个cog,您应该使用self.client.latency,请记住init函数,您指定了self.client = client

错:

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

右图:

代码语言:javascript
复制
await ctx.send(f'Pong! ({round(self.client.latency * 1000)}ms)')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63417565

复制
相关文章

相似问题

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