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

Discord.js中技术上能否在Modal组件内添加Button按钮?

结论

从技术实现层面,Discord.js 框架不支持在 Modal 弹窗组件中添加 Button 按钮,这个限制并非来自框架本身,而是Discord官方API的底层强制规则。

限制规则说明

  • Discord 接口对Modal的可承载组件有明确约束:Modal内的每个动作行(ActionRow)仅允许放置文本输入框(TextInputComponent),Button、下拉选择菜单等其他交互组件都不允许挂载到Modal结构中。
  • 你编写的代码语法本身没有错误,但提交到Discord接口时会直接触发参数校验错误,无法正常拉起弹窗。
  • 官方开发文档中也明确标注了Modal的组件类型限制,不存在可以突破该限制的框架配置项。

现有代码问题

你当前尝试的实现逻辑不符合接口规则,问题出在将Button放入动作行后挂载到Modal的步骤:

const modal = new Modal()
  .setCustomId('Vote')
  .setTitle('Vote TItle');

const favoriteColorInput = new TextInputComponent()
  .setCustomId('favoriteColorInput')
  .setLabel("Vote Title")
  .setStyle('SHORT');

const hobbiesInput = new TextInputComponent()
  .setCustomId('hobbiesInput')
  .setLabel("Value")
  .setStyle('SHORT');

const addButton = new MessageButton()
  .setCustomId('addButton')
  .setLabel('ADD')
  .setStyle('SUCCESS');

const firstActionRow = new MessageActionRow().addComponents(favoriteColorInput);
const secondActionRow = new MessageActionRow().addComponents(hobbiesInput);
const addButtonActionRow = new MessageActionRow().addComponents(addButton);

// 传入带Button的动作行会触发接口报错,无法正常展示Modal
modal.addComponents(firstActionRow, secondActionRow, addButtonActionRow);
await interaction.showModal(modal);

替代实现方案

如果要实现你预期的多字段动态添加效果,可以采用以下绕路方案,不需要在Modal内嵌Button:

  • 交互流程拆分:先发送带「ADD」按钮的普通消息,用户每次点击按钮就弹出单输入框的Modal,用户提交输入后更新原消息展示已添加的内容,循环该流程实现动态添加多组值的效果
  • 静态字段方案:直接在Modal中预置足够数量的文本输入框,让用户一次性填完所有需要提交的字段,不需要动态添加操作

预期效果参考

预期效果示意图


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 08:24:15