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

discord.py创建带SelectMenu的slash_command报错求助

问题分析与解决

错误原因

  • 参数类型误用:Slash命令的参数不支持直接将discord.SelectMenu作为类型,SelectMenu属于消息组件,需通过发送带组件的消息实现选择功能。
  • 属性调用错误:discord.SelectMenu.options是类属性而非可调用方法,不能用()调用,这是触发"'member_descriptor' object is not callable"报错的直接原因。
  • 响应方式错误:interaction.response('successfull')写法不符合规范,必须使用await interaction.response.send_message()方法发送响应。
  • 选项判断逻辑错误:不能直接将选中值与新建的discord.SelectOption对象比较,需获取交互事件中选中的value字符串进行判断。

正确实现代码

import discord
from discord import app_commands
from datetime import timedelta, datetime

# 假设客户端已正确初始化(示例)
# intents = discord.Intents.default()
# client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)

# 创建选择菜单组件
report_select = discord.ui.Select(
    placeholder="选择反馈类型",
    options=[
        discord.SelectOption(label='Hacking', value='hack', description='遭遇账号入侵?'),
        discord.SelectOption(label='Harassment', value='harass', description='遭遇骚扰?'),
        discord.SelectOption(label='Discord ToS', value='tos', description='有人违反Discord服务条款?'),
        discord.SelectOption(label='Support', value='sup', description='需要技术支持?')
    ]
)

# 定义选择菜单的回调逻辑
async def report_select_callback(interaction: discord.Interaction):
    selected_value = report_select.values[0]
    if selected_value == 'hack':
        await interaction.response.send_message("入侵反馈已提交!", ephemeral=True)
    # 可扩展其他选项的处理逻辑
    else:
        await interaction.response.send_message(f"{selected_value}类型反馈已收到", ephemeral=True)

# 绑定回调函数到选择菜单
report_select.callback = report_select_callback

# 定义Slash命令
@tree.command(name='test', description='测试反馈菜单', guild=discord.Object(12345678))
async def slash_command(interaction: discord.Interaction):
    # 创建视图承载选择菜单
    view = discord.ui.View()
    view.add_item(report_select)
    # 发送带组件的消息
    await interaction.response.send_message("请选择反馈类型:", view=view, ephemeral=True)

# 同步命令到服务器(在客户端就绪时执行)
@client.event
async def on_ready():
    await tree.sync(guild=discord.Object(12345678))
    print(f'已登录为 {client.user}')

# 运行客户端
# client.run('你的机器人令牌')

关键说明

  • 用discord.ui.Select创建选择菜单,通过discord.ui.View承载组件并发送给用户。
  • 为选择菜单绑定回调函数,处理用户的选择交互事件。
  • 使用interaction.response.send_message()正确发送响应,ephemeral=True表示消息仅触发命令的用户可见。
  • 在on_ready事件中完成Slash命令到指定服务器的同步。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 01:07:01