How can I embed messages using a Discord bot?

19,086

You can use a MessageEmbed, like programmerRaj said, or use the embed property in MessageOptions:

const {MessageEmbed} = require('discord.js')

const embed = new MessageEmbed()
  .setTitle('some title')
  .setDescription('some description')
  .setImage('image url')

// Discord.js v13
// These two are the same thing
channel.send({embeds: [embed]})
channel.send({
  embeds: [{
    title: 'some title',
    description: 'some description',
    image: {url: 'image url'}
  }]
})

// Discord.js v12
// These two are the same thing
channel.send(embed)
channel.send({
  embed: {
    title: 'some title',
    description: 'some description',
    image: {url: 'image url'}
  }
})

To send an embed of users' message in a particular channel, you can do something like this, where client is your Discord.js Client:

// The channel that you want to send the messages to
const channel = client.channels.cache.get('channel id')

client.on('message',message => {
  // Ignore bots
  if (message.author.bot) return
  // Send the embed
  const embed = new MessageEmbed()
    .setDescription(message.content)
    .setAuthor(message.author.tag, message.author.displayAvatarURL())
  channel.send({embeds: [embed]}).catch(console.error)
  // Discord.js v12:
  // channel.send(embed).catch(console.error)
})

Note that the above code will send the embed for every message not sent by a bot, so you will probably want to modify it so that it only sends it when you want it to.

I recommend reading Discord.js' guide on embeds (archive) or the documentation linked above for more information on how to use embeds.

Share:
19,086

Related videos on Youtube

Eva Hatz
Author by

Eva Hatz

Updated on June 04, 2022

Comments

  • Eva Hatz
    Eva Hatz almost 2 years

    I want to code a bot that will embed a user's sent message in a specific channel. If you know anything about GTA RP servers, it's like a Twitter or Instagram bot.

    Here's an example:

    screenshot of example embeds

    I think it's something about the console.log and the author's name, but I'm not sure so that's why I'm here. How can I embed users' messages like this?

  • Eva Hatz
    Eva Hatz over 3 years
    I did this and it works perfectry with messages but when I try to add an image with this code ".setImage(message.attachments)" it doesnt work. Am I doing something wrong? I also tried .setImage(message.attachments.first) so now I dont get an error but the Image is in a different message (not embed)
  • cherryblossom
    cherryblossom over 3 years
    @EvaHatz first is a method and setImage accepts a URL, so you’ll need to do .setImage(message.attachments.first().url).