如何在 discord.py v2 中为斜杠命令添加选项?

huangapple go评论88阅读模式
英文:

How do I add options to slash commands in discord.py v2?

问题

  1. async def embed_create(ctx: discord.Interaction, channels: discord.app_commands.choices[int]):
  2. TypeError: 'function' object is not subscriptable

这是我尝试的代码:

  1. @createGroup.command(name='channel', description='Create a channel with emoji.')
  2. @app_commands.rename(channels="Channels to choose from")
  3. @app_commands.describe(channels=[
  4. discord.app_commands.Choice(name="General", value=1),
  5. discord.app_commands.Choice(name="Media", value=2)
  6. ])
  7. async def embed_create(ctx: discord.Interaction, channels: discord.app_commands.choices[int]):
  8. if channels.name == "General":
  9. ctx.response.send_message("you chose general")
  10. elif channels.name == "Media":
  11. ctx.response.send_message("you chose media")
  1. <details>
  2. <summary>英文:</summary>
  3. I wanted to make an option in a slash command, but got
  1. async def embed_create(ctx:discord.Interaction, channels:discord.app_commands.choices[int]):

TypeError: 'function' object is not subscriptable

  1. this is what i tried:

@createGroup.command(name='channel', description='Create a channel with emoji.')
@app_commands.rename(channels="Channels to choose from")
@app_commands.describe(channels=[
discord.app_commands.Choice(name="General", value=1),
discord.app_commands.Choice(name="Media", value=2)
])
async def embed_create(ctx:discord.Interaction, channels:discord.app_commands.choices[int]):
if channels.name == "General":
ctx.response.send_message("you chose general")
elif channels.name == "Media":
ctx.response.send_message("you chose media")

  1. </details>
  2. # 答案1
  3. **得分**: -1
  4. 你可以在**__文档__**中查看一个[示例](https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=choices#discord.app_commands.choices)。
  5. ```py
  6. @app_commands.command()
  7. @app_commands.describe(fruits='fruits to choose from')
  8. @app_commands.choices(fruits=[
  9. Choice(name='apple', value=1),
  10. Choice(name='banana', value=2),
  11. Choice(name='cherry', value=3),
  12. ])
  13. async def fruit(interaction: discord.Interaction, fruits: Choice[int]):
  14. await interaction.response.send_message(f'你最喜欢的水果是{fruits.name}。')
  15. # 如果这不起作用,尝试
  16. @app_commands.command()
  17. @app_commands.describe(fruits='fruits to choose from')
  18. async def fruit(interaction: discord.Interaction, fruits: Literal['apple', 'banana', 'cherry']):
  19. await interaction.response.send_message(f'你最喜欢的水果是{fruits}。')

所以它只会是 channels: Choice[int]。

英文:

You can see an example in the documentation

  1. @app_commands.command()
  2. @app_commands.describe(fruits=&#39;fruits to choose from&#39;)
  3. @app_commands.choices(fruits=[
  4. Choice(name=&#39;apple&#39;, value=1),
  5. Choice(name=&#39;banana&#39;, value=2),
  6. Choice(name=&#39;cherry&#39;, value=3),
  7. ])
  8. async def fruit(interaction: discord.Interaction, fruits: Choice[int]): #here in this line the error lies, replace your app_commands etc etc with (see below)
  9. await interaction.response.send_message(f&#39;Your favourite fruit is {fruits.name}.&#39;)
  10. #If that does not work, try
  11. @app_commands.command()
  12. @app_commands.describe(fruits=&#39;fruits to choose from&#39;)
  13. async def fruit(interaction: discord.Interaction, fruits: Literal[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]):
  14. await interaction.response.send_message(f&#39;Your favourite fruit is {fruits}.&#39;)

so it‘d just be channels: Choice[int]

huangapple
  • 本文由 发表于 2023年6月29日 22:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定