Python 尝试在cog中使用client.latency时出错
我正在尝试对我的机器人使用ping命令,它的代码在一个cog中。我有一个关于什么是错误的想法,但我不知道如何修复它,因为我是新的。无论何时使用“f.ping”命令,都会出现以下错误:Python 尝试在cog中使用client.latency时出错,python,discord,discord.py,Python,Discord,Discord.py,我正在尝试对我的机器人使用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
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))
我已经将问题/错误缩小到{roundclient.latency*1000}ms部分,但我不知道如何修复它。该命令在删除该部分后运行良好。非常感谢您的帮助。您有两个错误
第一:f字符串引号不正确:
错:
await ctx.send(f'Pong!' ({round(client.latency * 1000)}ms))
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
对:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
await ctx.send(f'Pong! ({round(self.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(client.latency * 1000)}ms)')
对:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
await ctx.send(f'Pong! ({round(self.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)')
对:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
await ctx.send(f'Pong! ({round(self.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(client.latency * 1000)}ms)')
对:
await ctx.send(f'Pong! ({round(client.latency * 1000)}ms)')
await ctx.send(f'Pong! ({round(self.client.latency * 1000)}ms)')
看来你有两个不同的错误,让我们帮助你回到正轨 首先,您的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))
如果您想阅读有关self和f-string的内容,以下是一些有用的链接:
您似乎有两个不同的错误,让我们帮助您回到正轨 首先,您的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))
如果您想阅读有关self和f-string的内容,以下是一些有用的链接: