英文:
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='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]): #here in this line the error lies, replace your app_commands etc etc with (see below)
await interaction.response.send_message(f'Your favourite fruit is {fruits.name}.')
#If that does not work, try
@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'Your favourite fruit is {fruits}.')
so it‘d just be channels: Choice[int]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论