This snippet sends Mars Photos captured by Rovers on it in the channel where the slash command was executed using [NASA's official API](https://api.nasa.gov/). Make sure to register a command named "mrp" via the [command builder](https://autocode.com/tools/discord/command-builder/).
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let event = context.params.event;
// defining all the rovers successfully landed on mars
const rovers = [
'sojourner',
'spirit',
'opportunity',
'curiosity',
'perseverance',
];
// using the Math.random() function to get photos from random rovers instead of a specific one
const rRover = rovers[Math.floor(Math.random() * rovers.length)];
// sending an HTTP request to the API to get mars photos info
let mimg = await lib.http.request['@1.1.6'].get({
url: `https://api.nasa.gov/mars-photos/api/v1/rovers/${rRover}/latest_photos?api_key=${process.env.NASAAPI_KEY}`,
});
let data = await mimg.data.latest_photos;
// getting a random object from the JSON data stream
let randomObj = data[Math.floor(Math.random() * data.length)];
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: event.channel_id,
content: `πͺ Mars Rover Photos πͺ`,
embeds: [
{
title: `Captured by ` + randomObj['rover'].name,
fields: [
{
name: `Captured using`,
value: randomObj['camera'].full_name,
inline: true,
},
{
name: `Captured on (earth date)`,
value: randomObj.earth_date,
inline: true,
},
{
name: `Rover Launched on`,
value: randomObj['rover'].launch_date,
inline: true,
},
{
name: `Rover Landed on`,
value: randomObj['rover'].landing_date,
inline: true,
},
{
name: `Rover Status`,
value: randomObj['rover'].status,
inline: true,
},
],
image: {url: randomObj.img_src},
color: 0xf09546,
},
],
});