亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試在 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條經驗 獲得超6個贊

您似乎有 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 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號