首页 > 解决方案 > 为什么我得到 discord.py AttributeError: 'str' object has no attribute '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'

标签: pythonpython-3.xdiscord.pyattributeerrorconverters

解决方案


我假设这个函数不在一个类中。因此,self 值变成了一个 Context 类,而 ctx 值变成了一个参数。您得到的错误是“AttributeError: 'str' object has no attribute 'trigger_typing'”这意味着 str 对象无权访问该函数trigger_typing。从异步函数中删除 self 值将是一个快速修复。


推荐阅读