Search code examples
discorddiscord.js

need someone to help me with this discord.js code


i have this code that logs messages to my other server and it works fine BUTTTT it sends them in message form example: '@dyno said: hello' thats the format but id prefer it to send them in embeds with the title as the user who sent the message and the description to have the message content heres my existing code

client.on('messageCreate', message => {


  if (message.author.bot) {
    return;
  }


  console.log("Message received:", message.content); // Checkpoint
  if (message.guild && message.guild.id === 'my_server_id') {
    console.log("Message received from the correct guild."); // Checkpoint
    const destinationChannel = client.channels.cache.get('my_destination_channel_id');
    if (destinationChannel) {
      console.log("Destination channel found."); // Checkpoint
      destinationChannel.send(`**${message.author.tag}** said: ${message.content}`);
    } else {
      console.log(`Destination channel not found in the destination guild.`);
    }
  }
});


Solution

  • Ok, what I can understand you want the messaged the bot send to you received as embedded message.

    Also fellow stackoverflow people I am not a professional and still learning

    If I understand this correctly and can't test it at the moment but reading the discord.js docs it should come out to this. https://discordjs.guide/popular-topics/embeds.html#embed-preview

    // at the top of your file
    const { EmbedBuilder } = require('discord.js');
    
    // inside a command, event listener, etc.
    const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle({message.author.tag})
        .addFields(
            { name: 'Message', value: {message.content} },
        )
    

    Channel.send({ embeds: [exampleEmbed] });

    What your code will become I hope if it works is this,

        // at the top of your file
    const { EmbedBuilder } = require('discord.js')
    client.on('messageCreate', message => {
    
    
      if (message.author.bot) {
        return;
      }
    
    
      console.log("Message received:", message.content); // Checkpoint
      if (message.guild && message.guild.id === 'my_server_id') {
        console.log("Message received from the correct guild."); // Checkpoint
        const destinationChannel = client.channels.cache.get('my_destination_channel_id');
        if (destinationChannel) {
          console.log("Destination channel found."); // Checkpoint
    const exampleEmbed = new EmbedBuilder()
    .setColor(0x0099FF)
    .setTitle(message.author.tag)
    .addFields(
        { name: 'Message', value: message.content },
    )
    
    destinationChannel.send({ embeds: [exampleEmbed] });
        } else {
          console.log(`Destination channel not found in the destination guild.`);
        }
      }
    });