Search code examples
javascriptnode.jsdiscorddiscord.js

how to use variables as input for embed message? Discord.js


I got a question how to use a variable as a input for an embed message. When somebody message: asl hey @somebody 123, I want that the bot sends a embed message with the information given. I already made a script that haves args, so messageA{arg1} etc but now i want to use it in the embed message.

I tried this:

  if(msg.content === "test9") {
    const examplesEmbed = new MessageEmbed()
    const args = msg.content.trim().split(/ +/g);
      let bericht = args[0]; // Remember arrays are 0-based!.
      let noemen = args[1];
      let pers = args[2]
    
  .setTitle('Info about your request')
    .setAuthor({ name: 'Jordy'})
    .setDescription('Self-made bot "by jordy" self scripted etc. Yes even this :')
    .addFields(
        { name: 'Regular field title', value: bericht},
        { name: '\u200B', value: '\u200B' },
        { name: 'Inline field title', value: 'Some value here', inline: true },
    )
    .addField('Inline field title', 'Some value here', true)
    .setImage('https://i.imgur.com/AfFp7pu.png')
    .setTimestamp()
    .setFooter({ text: 'Some footer text here', iconURL: 'https://i.imgur.com/AfFp7pu.png' });
  msg.channel.send({ embeds: [examplesEmbed] });
  }
})


In an other script its the A has value for ' hello' but I get an error. how do I fix this?

Solution

  • The solution to this is quite simple, in order to use variables within strings in JS, you must use backticks (`).

    You can use either of the following instead:

        { name: 'Regular field title', value: `${messageA}` },
    
        { name: 'Regular field title', value: messageA },