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类。按照以下步骤修正:
- 添加正确的导入语句
在使用这些类的文件顶部添加导入:
// CommonJS 导入方式 const { Modal, MessageActionRow, TextInputComponent } = require('discord.js'); // ES模块导入方式(若项目使用import语法) import { Modal, MessageActionRow, TextInputComponent } from 'discord.js';
- 修正后的完整代码示例
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
相关产品推荐
相关产品推荐

