为什么我会收到 discord.py 属性错误:"str"对象没有属性"trigger_typing"



我不明白为什么会出现这个错误。代码中的一切对我来说都很好。也许是因为转换器不工作,但我的其他命令工作正常。

async def ship(self, ctx: commands.Context, user1: discord.Member = None, user2: discord.Member = None):
if user1 is None or user2 is None:
await ctx.send(embed=discord.Embed(
description=f"please mention two persons or provide two image urls! try: `{self.GetPrefix(ctx)}ship @person1 @person2` or `{self.GetPrefix(ctx)}ship [img url1] [img url2]`",
color=self.randomcolor()))
else:
await ctx.trigger_typing()
r1 = requests.get(user1.avatar_url)
with open("images/ship_avatar1.png", "wb") as f1:
f1.write(r1.content)
r2 = requests.get(user2.avatar_url)
with open("images/ship_avatar2.png", "wb") as f2:
f2.write(r2.content)
await self.ship("images/ship_avatar1.png", "images/ship_avatar2.png", "images/ship.png")
embed = discord.Embed()
embed.set_image(url="attachment://ship.png")
file = discord.File("images/ship.png", "ship.png")
await ctx.send(embed=embed, file=file)

我得到的错误是:

Ignoring exception in on_command_error
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/runner/Mob/cogs/fun.py", line 397, in ship
await self.ship("images/ship_avatar1.png", "images/ship_avatar2.png", "images/ship.png")
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 372, in __call__
return await self.callback(self.cog, *args, **kwargs)
File "/home/runner/Mob/cogs/fun.py", line 387, in ship
await ctx.trigger_typing()
AttributeError: 'str' object has no attribute 'trigger_typing'

我假设这个函数不在类中。因此,self值变成了Context类,ctx值变成了一个参数。你得到的错误是";AttributeError:"str"对象没有属性"trigger_typing"这意味着str对象不能访问函数CCD_ 1。从async函数中删除self值将是一个快速解决方案。

;船;不是命令的函数已经定义在"命令"中调用的同一类中;船;命令功能。重命名函数解决了问题。

最新更新