如何修复 TypeError:不支持的类型注释<class 'discord.ext.commands.context.Context'>?

问题描述 投票:0回答:1

我正在使用 Discord.py 2.3.2 和 Interactions API 编写一个 Discord 机器人。运行我的代码片段时:

@commands.hybrid_command(name='help', with_app_command=True)
async def help(self, ctx: commands.Context):
    await ctx.reply("Test was good!")

我收到错误

line 65, in <module>
    async def help(self, ctx: commands.Context):
TypeError: unsupported type annotation <class 'discord.ext.commands.context.Context'>

造成这种情况的原因是什么以及我应该如何解决它?

我已尽力遵循 Interactions API 文档,但无济于事。我收到的错误位于直接从文档示例复制的行中。我的命令

@bot.tree.command()
async def test(interaction: discord.Interaction) -> None:
    await interaction.response.send_message("test successful")

工作得很好,但混合命令不行。

discord discord.py bots
1个回答
0
投票

根据我在评论中的理解,您没有使用 Cog,这就是为什么您不能使用

self
关键字,因此第一个参数将是 Context。 还将你的装饰器更改为

@bot.hybrid_command(name="help", with_app_command=True)

所以你的最终代码将如下所示:

@bot.hybrid_command(name="help", with_app_command=True)
async def help(ctx: commands.Context):
    await ctx.reply("Test was good!")
© www.soinside.com 2019 - 2024. All rights reserved.