Search code examples
discorddiscord.js

payload.getJSON is not a function when sending embed through webhook


Trying to make a basic webhook to send an embed. Sending a regular string of text works fine but trying to send the following embed:

hook.send({
  embeds: [new Discord.MessageEmbed()
    .setDescription(`${interaction.user.username} used ${interaction.commandName}`)
    .addFields(
      { name: "User ID", value: interaction.user.id, inline: true },
      { name: "Guild ID", value: `${interaction.guild ? interaction.guild.name : "DM"}`, inline: true },
    )
    .setColor("BLACK")
  ]
});

results in the following error:

[ERROR] Unhandled promise rejection: payload.getJSON is not a function.
TypeError: payload.getJSON is not a function
    at Webhook.send (C:\Users\user\Documents\GitHub\bot\node_modules\discord-webhook-node\src\classes\webhook.js:56:28)
    at Client.<anonymous> (C:\Users\user\Documents\GitHub\bot\Events\Interaction Create\interactionCreate.js:116:20)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Solution

  • I do not think that is the correct code that is causing an issue for you, I have just written the code in a basic discord bot and it works.

    Discord Embed showing command working fine.

    const { Client, Intents, WebhookClient, MessageEmbed } = require('discord.js');
    const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
    
    const hook = new WebhookClient({ url: 'https://discord.com/api/webhooks/ID/TOKEN' })
    
    client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    });
    
    client.on('interactionCreate', async interaction => {
        if (!interaction.isCommand()) return;
    
        if (interaction.commandName === 'ping') {
            hook.send({
                embeds: [new MessageEmbed()
                    .setDescription(`${interaction.user.username} used ${interaction.commandName}`)
                    .addFields(
                        { name: "User ID", value: interaction.user.id, inline: true },
                        { name: "Guild ID", value: `${interaction.guild ? interaction.guild.name : "DM"}`, inline: true },
                    )
                    .setColor("BLACK")
                ]
            });
        }
    });
    
    client.login('TOKEN');
    

    This is the code that I used to get it working, if you give your code another look at as I do not think the problem is this code.