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

discord.js v13报错TypeError: Modal is not a constructor求助

解决discord.js v13中TypeError: Modal is not a constructor错误

在使用discord.js v13.4.0开发机器人模态框功能时,出现TypeError: Modal is not a constructor错误,无法正常创建模态框。

原代码

const modal = new Modal()
  .setCustomId('MODALCREATION')
  .setTitle('Create a server')
  .addComponents([
    new MessageActionRow().addComponents(
      new TextInputComponent()
        .setCustomId('serverInvite')
        .setLabel("What is the server invite?")
        .setStyle('SHORT'),
    ),
    new MessageActionRow().addComponents(
      new TextInputComponent()
        .setCustomId('userId')
        .setLabel('Please enter the user ID:')
        .setStyle("SHORT"),
    )
  ])
await i.showModal(modal)

错误信息

TypeError: Modal is not a constructor
    at Client.<anonymous> (/home/runner/AwfulForsakenProfiles/index.js:1450:21)
    at Client.emit (node:events:525:35)
    at Client.emit (node:domain:489:12)
    at InteractionCreateAction.handle (/home/runner/AwfulForsakenProfiles/node_modules/discord.js/src/client/actions/InteractionCreate.js:73:12)

解决方案

错误的核心原因是当前代码作用域中未正确导入Modal、MessageActionRow和TextInputComponent类。按照以下步骤修正:

  1. 添加正确的导入语句
    在使用这些类的文件顶部添加导入:
// CommonJS 导入方式
const { Modal, MessageActionRow, TextInputComponent } = require('discord.js');

// ES模块导入方式(若项目使用import语法)
import { Modal, MessageActionRow, TextInputComponent } from 'discord.js';
  1. 修正后的完整代码示例
const { Modal, MessageActionRow, TextInputComponent } = require('discord.js');

// ... 其他业务代码 ...

const modal = new Modal()
  .setCustomId('MODALCREATION')
  .setTitle('创建服务器')
  .addComponents([
    new MessageActionRow().addComponents(
      new TextInputComponent()
        .setCustomId('serverInvite')
        .setLabel("服务器邀请链接是什么?")
        .setStyle('SHORT'),
    ),
    new MessageActionRow().addComponents(
      new TextInputComponent()
        .setCustomId('userId')
        .setLabel('请输入用户ID:')
        .setStyle("SHORT"),
    )
  ])
await i.showModal(modal)

额外检查项

  • 执行npm list discord.js确认discord.js版本确实为v13.4.0
  • 检查类名拼写:确保Modal、MessageActionRow、TextInputComponent首字母大写,无拼写错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:35:53