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

Discord Bot开发报错:module 'discord'无'Select'属性求助

解决Discord机器人下拉菜单报错问题

问题原因

你遇到的AttributeError: module 'discord' has no attribute 'Select'是因为:

  • 在discord.py v2.x及以上版本中,Select组件不属于discord根模块,而是位于discord.ui子模块
  • 旧版的components参数已被废弃,现在需要用View类来承载交互组件
  • wait_for("select_option")的用法不正确,应该通过View的回调函数处理选择事件

修正后的代码

import discord
from discord import app_commands

# 自定义View类,承载下拉菜单组件
class DropdownView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)  # 设置timeout为None保持菜单永久可用,也可设具体秒数

    # 定义下拉菜单及其选项
    @discord.ui.select(
        placeholder="Selecione algo",
        options=[
            discord.SelectOption(label="Opção 1", value="1"),
            discord.SelectOption(label="Opção 2", value="2"),
            discord.SelectOption(label="Opção 3", value="3")
        ]
    )
    async def select_callback(self, interaction: discord.Interaction, select: discord.ui.Select):
        # 处理用户选择后的响应
        await interaction.response.send_message(content=f"Você selecionou a opção {select.values[0]}", ephemeral=True)

@tree.command(guild=discord.Object(id=id_do_servidor), name='menu', description='Menu')
async def sm(interaction: discord.Interaction):
    # 创建View实例并发送消息
    view = DropdownView()
    await interaction.response.send_message("Escolha uma opção:", view=view)

关键改动说明

  • 引入discord.ui.Select和discord.ui.View来构建交互组件
  • 用自定义View类包裹下拉菜单,并通过装饰器定义选择回调函数
  • 将原代码中的components参数替换为view参数,符合discord.py v2的规范
  • 直接在回调函数中处理用户的选择操作,替代原来错误的wait_for用法

如果你的discord.py版本低于v2.x,建议升级到最新稳定版,因为旧版本的组件API已不再维护。升级命令:

pip install -U discord.py

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 11:42:41