Discord Embed Issue

I am testing out some things for a new funny command I am going to start on soon but I have run into an issue. When the command is run this error appears:
(node:17768) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.image.url: Not a well formed URL.

Here is the code. It’s failing at the place where it’s supposed to put in the randomly selected image. The ‘images’ tag is defined at the top of the code but I just didn’t wanna show unneeded information. Btw, the ‘images’ tag is referring to a JSON file containing all the image links almost like a config.json file.

    const colors = [
        'green',
        'lime',
        'orange',
        'pink',
        'purple',
        'red',
        'white',
        'yellow',
        'black',
        'blue',
        'brown',
        'cyan'

    ]

    const color = colors[Math.floor(Math.random() * colors.length)]

    const startEmbed = new MessageEmbed()
        .setTitle('There is an imposter among us.')
        .setColor('#36393F')
        .setDescription('There is an imposter among us, but choose wisely you only have one try. \n\nChoose the one you think is the imposter by clicking the reactions below. ')
        .setThumbnail(`${images.color}_imp`)

    message.channel.send(startEmbed)

This is where the problems comes from:
.setThumbnail(${images.color}_imp)

The images label is referring to a json file where all the images are.

This is a small cutout of the images.json file. There is more colors in the original one. Don’t mind the “image_link_here” text, I just wanted to hide the links.
{ "green_imp":"image_link_here", "green_eject":"image_link_here", }

Sorry if I told too little information, this is my first time posting on here. Just ask me if you need more information to help me.

To test if this is an error with your code or the Discord API, can you console.log this and show us the result?

It console logs undefined_imp

It console logs undefined_imp

If this helps, this is how the images.json file looks like:
{ "green_imp":"image_link_here", "green_eject":"image_link_here", }

Don’t mind the “image_link_here”.

I’m pretty sure you need a link like https://www.mysite.com/images/imagename.jpeg or something. Malformed URL means whatever you’re passing in isn’t a link.

The ‘images’ label is referring to a .json file which is storing all the different links. Every option in the .json file is called <color>_<imp/eject> . I think the problem is that when the random color is selected it can’t choose from the .json file. When this part console logged: .setThumbnail(${images.color}_imp) it outputs undefind_imp . Is there a fix for this?

Can you send a screenshot/paste of the JSON data? You might need to use images.<color> instead…

This is how the .json file looks like.

which link do you want?

The problem is that the color should be randomly selected. I’m having a hard finding out how to do that.

var imp = color + "_imp"
...


.setThumbnail(${images.imp})

Try this. Not 100% sure if it will work

2 Likes

No need for a template literal here :wink:

1 Like

Looks like the same error occurs again, but now it console logs the correct names of the images.
image
Here is the whole error message it sent if that helps:
(node:1796) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.image.url: Not a well formed URL. at RequestHandler.execute (file directory) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:1796) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag –unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:1796) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

.setThumbnail(images.imp)

1 Like

I tried that as well, when I did that nothing happened. No image appeared nor any console logs.

maybes images[imp]?

Wow! That actually worked! Thank you so much!

1 Like

good to see - don’t forget to mark the most helpful response as the solution!

And, the reason it worked is because string object keys are accessed with brackets instead of dots.

1 Like