Discord.js v14 TypeError: (intermediate value).setColor不是函数求助
问题修复方案
错误原因
Discord.js v14及以上版本中,嵌入消息的构造类是EmbedBuilder,而非你代码里的Embed。使用错误的类实例化对象后,对象不存在setColor这类方法,因此抛出类型错误。
修复步骤
- 修正导入语句,替换为正确的类名:
const { EmbedBuilder } = require("discord.js");
- 将实例化嵌入对象的代码中的
new Embed()改为new EmbedBuilder(),其余链式调用逻辑保持不变:
const embed = new EmbedBuilder() .setColor("#FF0000") .setTitle("🏓 Pong!") .setDescription(`Latency : ${client.ws.ping}ms`) .setTimestamp() .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
- (可选优化)如果你的斜杠命令没有提前延迟回复,建议将
interaction.followUp改为interaction.reply,避免触发交互超时错误:
interaction.reply({ embeds: [embed] });
修复后的完整代码
const { EmbedBuilder } = require("discord.js"); module.exports = { name: "ping", description: "Returns websocket latency", run: async (client, interaction) => { const embed = new EmbedBuilder() .setColor("#FF0000") .setTitle("🏓 Pong!") .setDescription(`Latency : ${client.ws.ping}ms`) .setTimestamp() .setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` }); interaction.reply({ embeds: [embed] }); }, };
内容的提问来源于stack exchange,提问作者Dorritos
相关产品推荐
相关产品推荐

