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

Discord.js V14中awaitModalSubmit方法报错:无法读取未定义属性

Discord.js v14 工单机器人模态框报错:Cannot read properties of undefined (reading 'awaitModalSubmit')

问题重现

使用Discord.js v14开发工单机器人时,运行/ticket命令后机器人发送带4个按钮的嵌入消息,点击按钮本应弹出询问问题的模态框,但点击后控制台抛出错误,提示无法读取undefined的awaitModalSubmit和editReply属性。原代码尝试替换收集器为awaitModalSubmit方法后出现该问题。

错误原因

  1. interaction.showModal()无返回值:Discord.js v14中showModal()方法不返回任何对象,因此modalMessage变量为undefined,后续调用其方法必然报错。
  2. 交互类型判断错误:模态框提交的交互属于ModalSubmitInteraction,应使用isModalSubmit()判断,而非针对消息组件的isMessageComponent()。
  3. 过滤器逻辑无效:原过滤器中interaction.user.id === interaction.user.id恒为true,无法过滤其他用户的提交,需绑定触发模态框的用户ID。
  4. 回复编辑对象错误:编辑回复应直接使用原交互对象interaction,而非不存在的modalMessage。

修正后的代码

async function createTicketModal(interaction, ticketType) {
  if (interaction.deferred || interaction.replied) {
    return;
  }

  const modal = new ModalBuilder()
    .setCustomId('ticketModal')
    .setTitle(`${ticketType} - Ticket`);

  const questionModal = new TextInputBuilder()
    .setCustomId('questionModal')
    .setLabel('Please describe your problem.')
    .setStyle(TextInputStyle.Paragraph);

  const firstActionRow = new ActionRowBuilder().addComponents(questionModal);
  modal.addComponents(firstActionRow);

  // showModal无返回值,直接调用即可
  await interaction.showModal(modal);

  try {
    // 直接在原interaction上调用awaitModalSubmit
    const modalResponse = await interaction.awaitModalSubmit({
      // 绑定外部的用户ID,过滤非当前用户的提交
      filter: (i) => i.customId === 'ticketModal' && i.user.id === interaction.user.id,
      time: 60000,
    });

    // 判断是否为模态框提交交互
    if (modalResponse.isModalSubmit()) {
      // 从模态框组件中获取输入值
      const description = modalResponse.fields.getTextInputValue('questionModal');

      const channel = await interaction.guild.channels.create(`${interaction.user.username}-ticket`, {
        type: ChannelType.GuildText,
        // parent: 'PUT_YOUR_CATEGORY_ID_HERE',
        permissionOverwrites: [
          {
            id: interaction.guild.id,
            deny: PermissionFlagsBits.ViewChannel,
          },
          {
            id: interaction.user.id,
            allow: [
              PermissionFlagsBits.ViewChannel,
              PermissionFlagsBits.SendMessages,
              PermissionFlagsBits.AttachFiles
            ],
          },
        ],
      });

      if (!channel.isTextBased()) {
        await interaction.editReply({
          content: `Failed to create a text channel for your ${ticketType} ticket.`,
          embeds: [],
          components: [],
        });
        return;
      }

      const embed = new EmbedBuilder()
        .setTitle(`${ticketType} Ticket`)
        .setDescription(`Problem Description: ${description}`)
        .setColor('#0099ff');

      await channel.send({ content: `${interaction.user}`, embeds: [embed] });

      // 编辑原回复,移除组件
      await interaction.editReply({ components: [] });
      // 回复模态框提交,告知成功
      await modalResponse.reply({ content: 'Ticket created successfully!', ephemeral: true });
    }
  } catch (error) {
    console.error(error);
    // 异常时编辑原回复提示错误
    await interaction.editReply({
      content: `An error occurred while creating your ${ticketType} ticket. Please try again later.`,
      embeds: [],
      components: [],
    });
  }
}

额外说明

  • 注意使用ChannelType.GuildText替代字符串'text',v14中推荐使用枚举类型。
  • 权限使用PermissionFlagsBits枚举,避免字符串拼写错误。
  • 模态框提交后需调用modalResponse.reply()告知用户操作结果,否则Discord会显示交互失败提示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 04:37:48