Discord.js v14.8中Embed消息交互回复出现isJSONEncodable错误
问题:Discord.js v14.8 + TypeScript 机器人/help命令报错TypeError: isJSONEncodable is not a function
环境依赖配置
"dependencies": { "@discordjs/rest": "^1.1.0", "@supabase/supabase-js": "^2.25.0", "@types/node": "^18.7.14", "discord.js": "14.8.0", "dotenv": "^16.0.2", "express": "^4.18.2", "fs": "^0.0.1-security" }, "devDependencies": { "@types/express": "^4.17.17", "typescript": "^4.9.5" }, "overrides": { "discord-api-types": "0.37.20" }
错误信息
执行/help或!help命令时触发如下错误:
/home/container/node_modules/discord.js/src/structures/MessagePayload.js:200 isJSONEncodable(embed) ? embed.toJSON() : this.target.client.options.jsonTransformer(embed), ^ TypeError: isJSONEncodable is not a function at /home/container/node_modules/discord.js/src/structures/MessagePayload.js:200:9 at Array.map (<anonymous>) at MessagePayload.resolveBody (/home/container/node_modules/discord.js/src/structures/MessagePayload.js:199:36) at TextChannel.send (/home/container/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:169:32) at Message.reply (/home/container/node_modules/discord.js/src/structures/Message.js:803:25) at /home/container/build/commands/help.js:17:17 at Generator.next (<anonymous>) at /home/container/build/commands/help.js:8:71 at new Promise (<anonymous>) at __awaiter (/home/container/build/commands/help.js:4:12)
相关代码
/help命令代码
import { SlashCommandBuilder, ChannelType, TextChannel, EmbedBuilder } from "discord.js" import { getThemeColor } from "../functions"; import { SlashCommand } from "../types"; const command : SlashCommand = { command: new SlashCommandBuilder() .setName("help") .setDescription("Shows the information about the bot") , execute: interaction => { interaction.reply({ embeds: [ new EmbedBuilder() .setAuthor({name: "XXX.XX", iconURL: "https://i.imgur.com/XXXX.png"}) .setTitle("XXXX") .setThumbnail("https://i.imgur.com/XXX.png") .setColor(getThemeColor("text")) .setDescription( "discord bot developed by <@XXXXXXXXX>." ) .addFields( { name: 'Application Commands', value: '`/help`, `/XXX`, `/xxx`, `/xx`, `/xx`' }, ) .setTimestamp() .setImage("https://i.imgur.com/XXXXX.png") .setFooter({ text: "Powered by XXXX", iconURL: interaction.client.user?.avatarURL() || undefined }) ] }) }, cooldown: 30 } export default command
/ping命令代码(可正常运行)
import { SlashCommandBuilder, ChannelType, TextChannel, EmbedBuilder } from "discord.js" import { getThemeColor } from "../functions"; import { SlashCommand } from "../types"; const command : SlashCommand = { command: new SlashCommandBuilder() .setName("ping") .setDescription("Shows the bot ping/latency.") , execute: interaction => { interaction.reply({ embeds: [ new EmbedBuilder() .setAuthor({name: "Test"}) .setDescription(`📡 Ping: ${interaction.client.ws.ping}ms`) .setColor(getThemeColor("text")) ] }) }, cooldown: 30 } export default command
原因分析
该错误源于依赖版本不兼容:
- 在
package.json的overrides字段中,强制指定了discord-api-types为0.37.20,但Discord.js v14.8.0要求的discord-api-types版本范围是^0.37.37。低版本的discord-api-types缺失isJSONEncodable函数的定义,导致Discord.js内部处理Embed时调用失败。 /ping命令能正常运行是因为其Embed结构简单,未触发需要调用该函数的代码分支;而/help的Embed包含author、thumbnail、image等复杂字段,触发了兼容性问题。
解决方法
方法1:移除discord-api-types的强制覆盖
删除package.json中的overrides字段:
// 删除以下内容 "overrides": { "discord-api-types": "0.37.20" }
方法2:升级discord-api-types到兼容版本
若必须保留overrides,将版本号改为Discord.js v14.8.0兼容的版本,例如0.37.37:
"overrides": { "discord-api-types": "0.37.37" }
后续操作
修改依赖配置后,执行以下命令更新依赖:
npm install
重新编译TypeScript代码并重启机器人即可。
内容的提问来源于stack exchange,提问作者user20951184
相关产品推荐
相关产品推荐

