运行Discord机器人时出现TypeError: ModalBuilder is not a constructor错误
Discord机器人Modal报错解决
问题描述
我是编程新手,正在开发Discord机器人。在创建带Modal的Embed构建器命令时触发了报错,不确定是否与if (interaction.commandName === 'embed')这行代码有关。
代码文件
index.js
require("dotenv").config() const fs = require("fs") const { Client, Collection, ModalBuilder } = require('discord.js'); const client = new Client({intents: []}) client.commands = new Collection() client.once("ready", () => { console.log(`Ready! Logged in as ${client.user.tag}! I'm on ${client.guilds.cache.size} guild(s)!`) client.user.setActivity({name: "Bot by Keanu", type: "PLAYING"}) }) const commandFiles = fs.readdirSync("./src/commands").filter(file => file.endsWith(".js")) commandFiles.forEach(commandFile => { const command = require(`./commands/${commandFile}`) client.commands.set(command.data.name, command) }) client.on("interactionCreate", async (interaction) => { if(!interaction.isCommand()) return const command = client.commands.get(interaction.commandName) if(command) { try { await command.execute(interaction) } catch(error) { console.log(error) if(interaction.deferred || interaction.replied) { interaction.editReply("Fehler") } else { interaction.reply("Fehler") } } } }) client.login(process.env.DISCORD_BOT_TOKEN)
modal-embed.js
const { ActionRow } = require('@discordjs/builders'); const { TextInputStyle, InteractionType } = require('discord-api-types/v10'); const { interaction, ActionRowBuilder, ModalBuilder, Interaction } = require('discord.js'); const { SlashCommandBuilder } = require("@discordjs/builders") const { ModalBuilder } = require('@discordjs/modals') module.exports = { data: new SlashCommandBuilder().setName("embed").setDescription("Embed Creator"), async execute(interaction) { if (interaction.commandName === 'embed'){ const modal = new ModalBuilder() .setCustomId("embedModal") .setTitle("Embed Erstellen") const titleInput = new TextInputBuilder() .setCustomId("title") .setLable("Titel") .setStyle(TextInputStyle.Short) .setPlaceholder("Titel") const descriptionInput = new TextInputBuilder() .setCustomId("description") .setLable("Beschreibung") .setStyle(TextInputStyle.Paragraph) .setPlaceholder("Beschreibung") const authorInput = new TextInputBuilder() .setCustomId("author") .setLable("Autor") .setPlaceholder("Autor") .setStyle(TextInputStyle.Short) .setValue(interaction.user.username) modal.addComponents([ new ActionRowBuilder().addComponents(titleInput), new ActionRowBuilder().addComponents(descriptionInput), new ActionRowBuilder().addComponents(authorInput) ]) await interaction.showModal(modal) } } }
报错信息
TypeError: ModalBuilder is not a constructor at Object.execute (D:\Pc\bot-dc\src\commands\modal-embed.js:12:23) at Client.<anonymous> (D:\Pc\bot-dc\src\index.js:30:27) at Client.emit (node:events:513:28) at InteractionCreateAction.handle (D:\Pc\bot-dc\node_modules\discord.js\src\client\actions\InteractionCreate.js:83:12) at Object.module.exports [as INTERACTION_CREATE] (D:\Pc\bot-dc\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (D:\Pc\bot-dc\node_modules\discord.js\src\client\websocket\WebSocketManager.js:346:31) at WebSocketShard.onPacket (D:\Pc\bot-dc\node_modules\discord.js\src\client\websocket\WebSocketShard.js:482:22) at WebSocketShard.onMessage (D:\Pc\bot-dc\node_modules\discord.js\src\client\websocket\WebSocketShard.js:317:10) at WebSocket.onMessage (D:\Pc\bot-dc\node_modules\ws\lib\event-target.js:199:18) at WebSocket.emit (node:events:513:28)
解决方法
修复组件导入问题
新版discord.js(v14+)已内置ModalBuilder和TextInputBuilder,无需从@discordjs/modals单独导入,重复导入会导致构造函数冲突。同时要确保正确导入所需组件。移除冗余判断
if (interaction.commandName === 'embed')完全多余,因为该命令文件本身就绑定到embed斜杠命令,只有调用该命令才会执行execute函数。修正拼写错误
所有setLable改为setLabel(单词拼写错误)。
修改后的modal-embed.js代码:
const { SlashCommandBuilder } = require("@discordjs/builders"); const { TextInputStyle } = require('discord-api-types/v10'); const { ActionRowBuilder, ModalBuilder, TextInputBuilder } = require('discord.js'); module.exports = { data: new SlashCommandBuilder().setName("embed").setDescription("Embed Creator"), async execute(interaction) { const modal = new ModalBuilder() .setCustomId("embedModal") .setTitle("Embed Erstellen"); const titleInput = new TextInputBuilder() .setCustomId("title") .setLabel("Titel") .setStyle(TextInputStyle.Short) .setPlaceholder("Titel"); const descriptionInput = new TextInputBuilder() .setCustomId("description") .setLabel("Beschreibung") .setStyle(TextInputStyle.Paragraph) .setPlaceholder("Beschreibung"); const authorInput = new TextInputBuilder() .setCustomId("author") .setLabel("Autor") .setPlaceholder("Autor") .setStyle(TextInputStyle.Short) .setValue(interaction.user.username); modal.addComponents([ new ActionRowBuilder().addComponents(titleInput), new ActionRowBuilder().addComponents(descriptionInput), new ActionRowBuilder().addComponents(authorInput) ]); await interaction.showModal(modal); } };
额外注意:确保你的discord.js版本是v14或以上,Modal功能是v14新增的。如果版本过低,执行以下命令升级:
npm install discord.js@latest
内容的提问来源于stack exchange,提问作者Keanu
相关产品推荐
相关产品推荐

