我试图在我的机器人上使用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部分,但我不知道如何修复它。该命令在移除该部分时运行非常好。任何帮助都是非常感谢的。
发布于 2020-08-15 03:12:41
似乎你有两个不同的错误,让我们帮助你回到正轨。
首先,您的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))发布于 2020-08-14 18:36:27
你犯了两个错误。
第一:F字符串引号不正确:
错:
await ctx.send(f'Pong!' ({round(client.latency * 1000)}ms))右图:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')第二个:由于这是一个cog,您应该使用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)')https://stackoverflow.com/questions/63417565
复制相似问题