Search code examples
javascriptnode.jsdiscorddiscord.jsreferenceerror

How can I call the .username property in my embed ? (discord.js)


Hello everyone :) The title is a bit weird I guess but I am new to this and I'm trying to figure it out with my own words.

Basically what I have done is a "embeds-orders.js" file which will regroup a few embeds so I can call theses embeds in my main.js file without taking space in it.

My issue : I would like to write in the embed's description the username of who triggered it. I would've used "user.username" or something but I get a ReferenceError since I didn't define user. So I guess that I need to import something such as a class to my file in order to that ? I'm ready to learn from you guys :D

↓ Here is my "embeds-orders.js" file ↓

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

        const orderStarter = new MessageEmbed()

    .setAuthor("STARTER RECOVERY 🠗", "https://i.imgur.com/YOZP0xO.png")
        .setDescription(
            "Hello " + user.username + ", welcome." 
        )           /* ^^^^^^^^^^^^^ */
        .setColor("3232FF")
        .setThumbnail(
            "https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png"
        )
        .addFields(
            {
                name: "__Wanna Cancel ?__ 🠗",
                value: "Type `!cancel`",
                inline: true,
            }
        )
        
        module.exports =  {
            orderStarter
        }

Solution

  • If you need to dynamically specify who triggered the embed, you should make it a function.

    module.exports = (username) => {
     const { MessageEmbed } = require('discord.js');
    
     return new MessageEmbed()
      .setAuthor('STARTER RECOVERY 🠗', 'https://i.imgur.com/YOZP0xO.png')
      .setDescription(`Hello ${username}, welcome.')
      .setColor('3232FF')
      .setThumbnail('https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png')
      .addField('__Wanna Cancel ?__ 🠗', 'Type `!cancel`', true);
    };
    

    Then, when you need to send the embed, just pass the username as a parameter. For example, if this is taking place in a message event:

    const embed = require('./embeds-orders.js');
    
    client.on('message', (message) => {
     if (message.content === 'I want to order the embed')
      message.channel.send(embed(message.author.username));
    });
    

    Edit: in a messageReactionAdd event:

    const embed = require('./embeds-orders.js');
    
    client.on('messageReactionAdd', (reaction, user) => {
      // bla bla bla
      message.channel.send(embed(user.username));
    

    Edit #2: You can just edit the function to add more parameters.

    module.exports = (author, description, color) => {
     const { MessageEmbed } = require('discord.js');
    
     return new MessageEmbed()
      .setAuthor(...author) // author can be an array with the text and the url as elements
      .setDescription(description)
      .setColor(color)
      .setThumbnail(
       'https://media.discordapp.net/attachments/224553787824668673/716765453548126228/icon.png.fe88fd1ca437c86cde7cd99961673ef8.png'
      )
      .addField('__Wanna Cancel ?__ 🠗', 'Type `!cancel`', true);
    };
    
    const embed = require('./embeds-orders.js');
    
    client.on('messageReactionAdd', (reaction, user) => {
      // bla bla bla
     reaction.emoji.name === "Emoji Name"
      ? message.channel.send(embed(["author text", "url"], "description", "color"))
      : message.channel.send(
          embed(
            ["different author text", "url"],
            "different description",
            "different color"
          )
        );