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

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

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

问题

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

这是我尝试的代码:

@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")

<details>
<summary>英文:</summary>

I wanted to make an option in a slash command, but got
async def embed_create(ctx:discord.Interaction, channels:discord.app_commands.choices[int]):

TypeError: 'function' object is not subscriptable



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")


</details>


# 答案1
**得分**: -1

你可以在**__文档__**中查看一个[示例](https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=choices#discord.app_commands.choices)。

```py
@app_commands.command()
@app_commands.describe(fruits='fruits to choose from')
@app_commands.choices(fruits=[
    Choice(name='apple', value=1),
    Choice(name='banana', value=2),
    Choice(name='cherry', value=3),
])
async def fruit(interaction: discord.Interaction, fruits: Choice[int]):
    await interaction.response.send_message(f'你最喜欢的水果是{fruits.name}。')

# 如果这不起作用,尝试

@app_commands.command()
@app_commands.describe(fruits='fruits to choose from')
async def fruit(interaction: discord.Interaction, fruits: Literal['apple', 'banana', 'cherry']):
    await interaction.response.send_message(f'你最喜欢的水果是{fruits}。')

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

英文:

You can see an example in the documentation


@app_commands.command()
@app_commands.describe(fruits=&#39;fruits to choose from&#39;)
@app_commands.choices(fruits=[
    Choice(name=&#39;apple&#39;, value=1),
    Choice(name=&#39;banana&#39;, value=2),
    Choice(name=&#39;cherry&#39;, value=3),
])
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)
    await interaction.response.send_message(f&#39;Your favourite fruit is {fruits.name}.&#39;)

#If that does not work, try 

@app_commands.command()
@app_commands.describe(fruits=&#39;fruits to choose from&#39;)
async def fruit(interaction: discord.Interaction, fruits: Literal[&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]):
    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:

确定