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

添加/group invite子命令后@bot.modal无法执行的求助

解决Discord Interactions库中Group子命令添加后Modal回调失效的问题

以下是针对你遇到问题的具体排查和解决步骤:

1. 检查Modal自定义ID的唯一性

确认/group create触发的Modal的custom_id没有与其他命令(包括/group invite)的组件、Modal ID重复。Interactions库完全依赖custom_id匹配回调,重复ID会导致回调绑定混乱,甚至完全不触发目标逻辑。

示例:确保创建群组的Modal使用独有的ID,比如group_create_modal,而非通用的group_modal这类可能被复用的命名。

2. 确认子命令的注册结构正确性

使用SlashCommandGroup统一管理子命令,避免注册结构错误导致命令逻辑异常:

from interactions import SlashCommandGroup, InteractionContext, Modal, ShortText

# 正确注册群组命令组
group_cmd = SlashCommandGroup("group", "群组管理相关命令")

@group_cmd.subcommand(description="创建新群组")
async def create(ctx: InteractionContext):
    # 触发创建群组的Modal
    create_modal = Modal(
        ShortText(label="群组名称", custom_id="group_name_input"),
        custom_id="group_create_modal",
        title="创建新群组"
    )
    await ctx.send_modal(create_modal)

@group_cmd.subcommand(description="邀请成员加入群组")
async def invite(ctx: InteractionContext):
    # 邀请逻辑代码
    await ctx.send("邀请功能待实现", ephemeral=True)

确保两个子命令都正确挂载到同一个命令组下,没有重复注册或嵌套错误。

3. 验证Modal回调的注册方式

Modal回调需在全局/命令组同级位置注册,且custom_id与触发的Modal完全匹配:

@bot.modal(custom_id="group_create_modal")
async def handle_group_create(ctx: InteractionContext, group_name: str):
    # 处理Modal提交的逻辑
    await ctx.send(f"成功创建群组:{group_name}", ephemeral=True)

注意:不要在子命令函数内部注册Modal回调,否则会导致绑定失效。

4. 更新Interactions库版本

部分旧版本的discord-py-interactions存在子命令与Modal交互的兼容性bug,执行以下命令更新到最新稳定版:

pip install -U discord-py-interactions

5. 强制同步命令到Discord服务器

添加新子命令后,必须同步命令结构到Discord,否则服务器端的命令缓存会与本地逻辑不一致:

# 在机器人启动时执行一次同步(生产环境避免频繁调用)
await bot.sync_commands()

6. 排查回调内部异常

在Modal回调中添加异常捕获,确认是否是回调触发后被异常中断导致无反馈:

@bot.modal(custom_id="group_create_modal")
async def handle_group_create(ctx: InteractionContext, group_name: str):
    try:
        await ctx.send(f"处理群组创建:{group_name}")
        # 你的业务逻辑
    except Exception as e:
        await ctx.send(f"回调执行出错:{str(e)}", ephemeral=True)
        print(f"Modal回调异常:{e}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 03:07:33