Search code examples
javascriptnode.jsdiscorddiscord.js

How do I send an embed with buttons to a specific channel in discord.js?


So I'm trying to send an embed to a specific channel (1068005047373414481) and I'd like to send an embed with the sort of new buttons using the ActionRow components. Keep in mind that the channel is not the same channel where the interaction will be ran. Anyways I've tried using the interaction.guild.channels.cache.get(id).send(embed); method but it seems to not work. I always end up getting a DiscordAPIError[50006]: Cannot send an empty message error. Either way I don't think you can add components to that message like you can do with interaction.reply({ embeds: embed, components: [row] });

Here is my current code:

client.once('ready', () => {
    console.log('Ready!')
    client.user.setActivity("all you rascals", {
        type: ActivityType.Watching,
    });
    var testing = new EmbedBuilder()
    .setTitle('test')

    client.guilds.cache.get(guild).channels.cache.get(channel).send(testing)
})

(This is for testing as I will implement the correct way into my bot once I find an answer)

When I run that I get this error:

C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293
        throw new DiscordAPIError.DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
              ^

DiscordAPIError[50006]: Cannot send an empty message
    at SequentialHandler.runRequest (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:293:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\handlers\SequentialHandler.cjs:99:14)
    at async REST.request (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\@discordjs\rest\dist\lib\REST.cjs:52:22)
    at async TextChannel.send (C:\Users\User\OneDrive\Documents\a\c\fbot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:175:15) {
  rawError: { message: 'Cannot send an empty message', code: 50006 },
  code: 50006,
  status: 400,
  method: 'POST',
  url: 'https://discord.com/api/v10/channels/1068005047373414481/messages',
  requestBody: {
    files: [],
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: undefined,
      components: undefined,
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    }
  }
}

Solution

  • interaction.reply({ embeds: embed, components: [row] });
    

    Is very close to what you need, remember that the embeds property must be an array of embeds, even if you only have one.

    The correct code would be

    interaction.reply({ embeds: [embed], components: [row] });