You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

制作Discord嵌入斜杠命令时遇CombinedError问题求助

问题描述

开发Discord Embed斜杠命令时触发CombinedError,包含两个ValidationError:

  • s.nullish期望undefined或null,却收到[Function: displayAvatarURL]
  • s.string期望字符串类型,却收到[Function: displayAvatarURL]

检查代码后未发现明显问题,调整代码反而引发更多错误,附上报错信息与代码:

报错信息

The error:
CombinedError (2)
  Received one or more errors

  1 ValidationError > s.nullish
  |   Expected undefined or null
  |
  |   Received:
  |   | [Function: displayAvatarURL]

  2 ValidationError > s.string
  |   Expected a string primitive
  |
  |   Received:
  |   | [Function: displayAvatarURL]

原代码

const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName("embed")
        .setDescription('Return an embed.'),
    async execute(interaction, client) {
        const embed = new EmbedBuilder()
            .setTitle(`This is an EMBED`)
            .setDescription('This is a EMBED description')
            .setColor(0xFA03CA)
            .setImage(client.user.displayAvatarURL())
            .setThumbnail(client.user.displayAvatarURL())
            .setTimestamp(Date.now())
            .setAuthor({
                url: `https://www.twitch.tv/x333en`,
                iconURL: interaction.user.displayAvatarURL(),
                name: interaction.user.tag
            })
            .setFooter({
                iconURL: client.user.displayAvatarURL(),
                text: client.user.tag
            })
            .setURL(`https://discord.gg/8YtDhpbCJf`)
            .addFields([
                {
                    name: `Field 1`,
                    value: `Field Value 1`,
                    inline: true
                },
                {
                    name: `Field 2`,
                    value: `Field value 2`,
                    inline: true
                }
            ]);

        await interaction.reply({
            embeds: [embed]
        })
    }
}
解决方案

报错原因是传递了displayAvatarURL方法本身而非执行后的字符串结果。Embed的iconURL、setImage等字段需要的是字符串格式的图片链接,而displayAvatarURL是一个方法,必须通过加括号()调用它才能返回对应的URL字符串。

检查代码中所有使用displayAvatarURL的位置,确保都添加了括号调用。修改后的代码如下:

const { SlashCommandBuilder, EmbedBuilder } = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName("embed")
        .setDescription('Return an embed.'),
    async execute(interaction, client) {
        const embed = new EmbedBuilder()
            .setTitle(`This is an EMBED`)
            .setDescription('This is a EMBED description')
            .setColor(0xFA03CA)
            .setImage(client.user.displayAvatarURL())
            .setThumbnail(client.user.displayAvatarURL())
            .setTimestamp(Date.now())
            .setAuthor({
                url: `https://www.twitch.tv/x333en`,
                iconURL: interaction.user.displayAvatarURL(),
                name: interaction.user.tag
            })
            .setFooter({
                iconURL: client.user.displayAvatarURL(),
                text: client.user.tag
            })
            .setURL(`https://discord.gg/8YtDhpbCJf`)
            .addFields([
                {
                    name: `Field 1`,
                    value: `Field Value 1`,
                    inline: true
                },
                {
                    name: `Field 2`,
                    value: `Field value 2`,
                    inline: true
                }
            ]);

        await interaction.reply({
            embeds: [embed]
        })
    }
}

(注:若原代码中存在漏写括号的情况,补充括号即可解决问题)

内容的提问来源于stack exchange,提问作者kazzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:41:05