javascript 嵌入错误setTitle无法读取属性null [已关闭]

bbmckpt7  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(133)

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
2小时前关门了。
Improve this question
我一直在尝试按照discord.js上的指南创建嵌入,但在正确编写代码后,在执行嵌入命令时,它在终端中给出了一个错误。
这个机器人仍然需要大量的实现,但代码只是给出了错误,这是有点奇怪的实现似乎是正确的

  • -目前正在处理嵌入--
    我需要帮助,请~~
const {SlashCommandBuilder, EmbedBuilder} = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
    .setName("embed")
    .setDescription("Returns an Embed"),
    async execute(interaction,client){
        const embed = new EmbedBuilder()
        const user = interaction.options.getUser('target')
        .setTitle('This is totally an embed')
        .setDescription('This might Work ? Maybe')
        .setColor(0x18e1ee)
        .setImage(client.user.displayAvatarURL())
        .setThumbnail(client.user.displayAvatarURL())
        .setTimestamp(Date.now())
        .setAuthor({
            url: `https://ww4.gogoanimes.org/`,
            iconURL: interaction.user.displayAvatarURL(),
            name: interaction.user.tag
        })
        .setFooter({
            iconURL: client.user.displayAvatarURL(),
            text: client.user.tag
        })
        .setURL('https://discord.gg/kMb9XZAq')
        .addFields([
            {
                name: 'Field 1',
                value: 'Field value 1',
                inline: true
            },
            {
                name: `Field 2`,
                value: `Field value 2`,
                inline: true
            }
        ]);
        await interaction.reply({embeds: [embed]})
    },
}

我在终端得到的错误是:(https://i.stack.imgur.com/IAox0.png
我只希望嵌入代码能够正常工作,以便可以在所有不同命令上使用它的良好UI

dl5txlt9

dl5txlt91#

初始化嵌入后,要初始化另一个变量。应该先初始化用户变量,然后再初始化嵌入。

const user = interaction.options.getUser('target')
        const embed = new EmbedBuilder()
            .setTitle('This is totally an embed')
            .setDescription('This might Work ? Maybe')
            .setColor(0x18e1ee)
            .setImage(client.user.displayAvatarURL())
            .setThumbnail(client.user.displayAvatarURL())
            .setTimestamp(Date.now())
            .setAuthor({
                url: `https://ww4.gogoanimes.org/`,
                iconURL: interaction.user.displayAvatarURL(),
                name: interaction.user.tag
            })
            .setFooter({
                iconURL: client.user.displayAvatarURL(),
                text: client.user.tag
            })
            .setURL('https://discord.gg/kMb9XZAq')
            .addFields([
                {
                    name: 'Field 1',
                    value: 'Field value 1',
                    inline: true
                },
                {
                    name: `Field 2`,
                    value: `Field value 2`,
                    inline: true
                }
            ]);

希望能有所帮助

hi3rlvi2

hi3rlvi22#

你试图设置null的标题,但它必须是EmbedBuilder()。这很容易修复。

const embed = new EmbedBuilder()
const user = interaction.options.getUser('target') // This line should not be here
        .setTitle('This is totally an embed')
        .setDescription('This might Work ? Maybe')
        // ...

应改为:

const user = interaction.options.getUser('target')
const embed = new EmbedBuilder()
        .setTitle('This is totally an embed')
        .setDescription('This might Work ? Maybe')
        // ...

这里我们将const user = ...行移到上面,这样.setTitle()实际上设置了EmbedBuilder()的标题。
之前:

const user = interaction.options.getUser('target').setTitle('This is totally an embed')

之后:

const embed = new EmbedBuilder().setTitle('This is totally an embed')

相关问题