如何正确使用MessageEmbed?解决其非构造函数报错问题
解决Discord.js中TypeError: MessageEmbed is not a constructor错误
我在用discord.js开发Discord机器人时遇到了TypeError: MessageEmbed is not a constructor错误,报错堆栈如下:
TypeError: MessageEmbed is not a constructor at Client.<anonymous> (C:\Users\patry\Desktop\MythicBot\index.js:34:21) at Client.emit (node:events:512:28) at MessageCreateAction.handle (C:\Users\patry\Desktop\MythicBot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14) at module.exports [as MESSAGE_CREATE] (C:\Users\patry\Desktop\MythicBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\patry\Desktop\MythicBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31) at WebSocketManager.<anonymous> (C:\Users\patry\Desktop\MythicBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12) at WebSocketManager.emit (C:\Users\patry\Desktop\MythicBot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31) at WebSocketShard.<anonymous> (C:\Users\patry\Desktop\MythicBot\node_modules\@discordjs\ws\dist\index.js:1173:51) at WebSocketShard.emit (C:\Users\patry\Desktop\MythicBot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31) at WebSocketShard.onMessage (C:\Users\patry\Desktop\MythicBot\node_modules\@discordjs\ws\dist\index.js:988:14) Emitted 'error' event on Client instance at: at emitUnhandledRejectionOrErr (node:events:393:10) at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
我的代码:
const { Client, GatewayIntentBits, MessageEmbed } = require('discord.js'); const Discord = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, ], }); const prefix = '$'; // Możesz dostosować prefix komendy client.on('messageCreate', async (message) => { if (message.content.startsWith(prefix)) { const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if (command === 'wyslij') { // Sprawdź, czy użytkownik podał odpowiednie argumenty if (args.length < 3) { return message.reply('Za mało argumentów. Użyj: $wyslij IDkanalu Tresc Kolor'); } const channelID = args[0]; const content = args[1]; const color = args[2].toLowerCase(); // Przekonwertuj kolor na małe litery // Sprawdź, czy kanał istnieje const channel = client.channels.cache.get(channelID); if (!channel) { return message.reply('Podany kanał nie istnieje.'); } // Stwórz wiadomość embed const embed = new MessageEmbed() .setColor(color) .setDescription(content); // Wyślij wiadomość embed na wskazany kanał channel.send(embed); } } });
错误原因
你的代码使用的是Discord.js v14版本,这个版本里**MessageEmbed已被移除**,取而代之的是EmbedBuilder。原有的MessageEmbed构造函数不再存在,因此会抛出“不是构造函数”的错误。
修复步骤
- 修改导入语句:把原来导入的
MessageEmbed替换成EmbedBuilder - 替换实例化代码:将
new MessageEmbed()改为new EmbedBuilder() - 调整发送逻辑:v14中
channel.send()不再直接接受Embed实例,需要放在embeds数组中传递,比如channel.send({ embeds: [embed] })
修复后的完整代码
const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMembers, ], }); const prefix = '$'; // Możesz dostosować prefix komendy client.on('messageCreate', async (message) => { if (message.content.startsWith(prefix)) { const args = message.content.slice(prefix.length).trim().split(/ +/); const command = args.shift().toLowerCase(); if (command === 'wyslij') { // Sprawdź, czy użytkownik podał odpowiednie argumenty if (args.length < 3) { return message.reply('Za mało argumentów. Użyj: $wyslij IDkanalu Tresc Kolor'); } const channelID = args[0]; const content = args[1]; const color = args[2].toLowerCase(); // Przekonwertuj kolor na małe litery // Sprawdź, czy kanał istnieje const channel = client.channels.cache.get(channelID); if (!channel) { return message.reply('Podany kanał nie istnieje.'); } // Stwórz wiadomość embed const embed = new EmbedBuilder() .setColor(color) .setDescription(content); // Wyślij wiadomość embed na wskazany kanał channel.send({ embeds: [embed] }); } } });
额外说明
Discord.js v14对构建类做了统一调整,所有用于构建消息、嵌入、组件的类都统一命名为XXXBuilder(比如EmbedBuilder、SlashCommandBuilder等),目的是让API更清晰一致。
内容的提问来源于stack exchange,提问作者Njewiem
相关产品推荐
相关产品推荐

