0

I'm creating a bot that I want to respond with rich embeds. Since I have over three dozen different args for this one command, I thought I could keep the code clean by moving the embed for each arg to a different .js file, then use the 'require' variable to call the file when the appropriate arg is used. Simple, right? However, whenever I run the file, the embed isn't rendered correctly.

I've tried removing those first couple lines because I thought that was specifying that it should be sent as a message instead of an embed, bu that just led to error messages (like "message is not defined" or "cannot send empty message")

Here's the code I'm using in the main command file, which works when I call a variable that's in the same file:

  else if (args[0] === 'butterfly') {
    return message.channel.send(require('./butterfly.js'))
  }

And here's the code in the second file that I'm pretty sure has the issue:

module.exports = (message) => { 
    message.channel.send({embed: {
        color: 16615352,
        author: {
          name: "TEST",
          url: "https://url.here",
          icon_url: "https://url.here"
        },
        description: "test",
        thumbnail: {
          url: "https://url.here"
        },
        fields: [{
            name: "Test",
            value: "Test",
            inline: true
          },
          {
            name: "Test2",
            value: "Test2",
            inline: true
          }
        ],
      }
    });
}

Currently, I can get the text to send, but it doesn't render as an embed and I just get the code text starting at "(message)". What do I use to specify that the code should be rendered as an embed?

1 Answers1

0

Problems:

  • In butterfly.js, you're declaring module.exports as a function. That means that require() is returning that function, but you don't call it. Therefore, it isn't actually being executed.

  • You're also trying to send a message inside of message.channel.send(), which would cause an error.


Solution:

In this setup, you'll notice the RichEmbed constructor. It's clean and simple, rather than explicitly defining the embed property of the message options. However, the main thing to understand is why it works: an embed is exported from butterfly.js; a new variable is declared as the embed and then used in the TextChannel.send() method.

const { RichEmbed } = require('discord.js');

const embed = new RichEmbed()
  .setColor(16615352)
  .setAuthor('TEST', 'iconURL', 'URL')
  .setDescription('test')
  .setThumbnail('iconURL')
  .addField('Test', 'Test', true)
  .addField('Test2', 'Test2', true);

module.exports = embed;
const embed = require('./butterfly.js');

return message.channel.send(embed)
  .catch(console.error);

Additional Explanation:

If you were to require a function from another file like you originally tried to do, these are examples of how to call it:

require('./someFunction.js')(params);
const someFunction = require('./someFunction.js');

someFunction(params);
slothiful
  • 5,548
  • 3
  • 12
  • 36
  • Thank you for the reply! Tried that out and the embed sent successfully; however, is it possible to reference different .js files from the main file? As I mentioned there's many different arguments for this command and I'm trying to find a way to send different embeds depending on that, but I can't figure out how to do that with the const embed only requiring one of these files. This might be an entirely separate issue, if you recommend it then I'll make a separate post. – sitruskeleton Jul 20 '19 at 20:29
  • [`require()`](https://nodejs.org/api/modules.html#modules_require_id) is doing just that. I'm not sure I understand what you're asking? – slothiful Jul 20 '19 at 20:31
  • sorry, I'll try to be more clear. Can I reference different files with one 'embed' variable? I thought I remembered trying that in the past and I got an error like "embed has already been declared" – sitruskeleton Jul 20 '19 at 20:33
  • Ah, okay. If you want to change the value of `embed`, you can declare it with `let` and assign a value with `embed = ...`. Alternatively, you could declare multiple of the same `embed` variables using `var`. For details about the difference, see [this answer](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var). – slothiful Jul 20 '19 at 20:35
  • success! used var and it worked perfectly. thank you so much! – sitruskeleton Jul 20 '19 at 20:39