如何在discord.py中使用选项?



我正在制作一个可以发送随机模因的不和谐机器人,我想根据用户想要的模因数量添加一个连续feed命令,例如/feed number:15,将发送15个模因。

问题是如何使选项输入在命令上工作,我发现一些视频对我有一点帮助,但由于其他命令是híbrido命令,我可以弄清楚如何使这个工作。

这是我到目前为止的内容:


@bot.hybrid_command(name = "feed", with_app_command = True, description = "Feeds a specific number of memes")
@app_commands.guilds(discord.Object(id = guild_id))
async def test1(interaction: discord.Interaction, number: int):
await interaction.response.send_message("It's working!")

等待行,显然不会这样做任何提要,我知道,我把提要部分分开了,它可以工作,但我只是这样做是为了测试,所以如果我输入命令并输入一个数字,它应该返回我"它正在工作!",但这从来没有发生过,我想这可能与híbrido命令有关,但我真的不知道,我错过了什么?

我不认为输入选项是问题所在,因为我认为它没有任何问题,但肯定是错误的是你命名混合命令interaction的第一个参数,这是错误的,可能会造成混乱;实际的参数应该是contextctx。该对象是一个discord.ext.commands.Context对象。

您应该将其重命名为ctx以避免混淆。

@bot.hybrid_command(name = "feed", with_app_command = True, description = "Feeds a specific number of memes")
@app_commands.guilds(discord.Object(id = guild_id))
async def test1(ctx: commands.Context, number: int):
await ctx.send("It's working!")

你可能想在这里看到官方的例子

最新更新