You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在discord.py斜杠命令中为参数设置可选选项列表?

Discord.py app_commands选项choices不生效的解决办法
  • 检查discord.py版本
    app_commands模块的choices参数在旧版本中可能存在兼容性问题,执行以下命令升级到最新稳定版:

    pip install -U discord.py
    
  • 重新同步命令到Discord
    修改选项的choices后,必须重新同步命令才能让Discord服务器识别更新。可以在客户端初始化时添加同步逻辑:

    import discord
    from discord import app_commands
    
    class MyClient(discord.Client):
        def __init__(self, *, intents: discord.Intents):
            super().__init__(intents=intents)
            self.tree = app_commands.CommandTree(self)
    
        async def setup_hook(self):
            # 同步到指定服务器(推荐,同步速度更快)
            await self.tree.sync(guild=discord.Object(id=你的服务器ID))
            # 全局同步(需要等待Discord审核,适合公共机器人)
            # await self.tree.sync()
    
    client = MyClient(intents=discord.Intents.default())
    
  • 验证命令定义格式
    确保option装饰器正确绑定到命令函数上,且参数类型与choices匹配(示例中name是字符串类型,choices对应字符串列表):

    @client.tree.command(name="choose_name", description="选择一个名字")
    @app_commands.option(name='name', description="Choose a name", choices=["Alice", "Bob", "James", "Charlie"])
    async def choose_name(interaction: discord.Interaction, name: str):
        await interaction.response.send_message(f"你选择了:{name}")
    
  • 检查choices拼写与格式
    确认choices列表中的每个元素都是字符串(或对应option指定的类型),没有多余的空格或拼写错误,否则Discord可能无法正确加载选项。

内容的提问来源于stack exchange,提问作者iddwsdgh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 11:09:56