This snippet gets NASA's Astronomy Picture of the Day posted daily into a Discord channel using [NASA's official API](https://api.nasa.gov/).
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// sending an HTTP request to the API to get APOD info
let apod = await lib.http.request['@1.1.6'].get({
url: `https://api.nasa.gov/planetary/apod?api_key=${process.env.NASAAPI_KEY}`,
});
let data = await apod.data;
// checks if `copyright` field is present in the JSON data - if, then includes `footer` field of embed to display copyright holder(s)
if ('copyright' in data) {
// checks if the post type is image & copyrighted
if (data.media_type === 'image') {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: process.env.NASAAPOD_CHANNEL_ID,
content: `π **Astronomy Picture of the Day** π`,
embeds: [
{
title: data.title,
description: data.explanation,
color: 0x0165b3,
image: {url: data.url},
footer: {text: `Copyright - ${data.copyright}`}, // please keep this due to NASA's restrictions, learn more about them here: https://www.nasa.gov/multimedia/guidelines/index.html
},
],
});
// sending a different message if the post is not an image & copyrighted
} else {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: process.env.NASAAPOD_CHANNEL_ID,
content: `π **Astronomy Picture of the Day** π`,
embeds: [
{
title: data.title,
description: data.explanation,
fields: [
{
name: `Due to Discord not allowing to display videos in rich embeds, you'll have to visit the webpage`,
value: `https://apod.nasa.gov/apod/astropix.html`,
},
],
color: 0x0165b3,
footer: {text: `Copyright - ${data.copyright}`}, // please keep this due to NASA's restrictions, learn more about them here: https://www.nasa.gov/multimedia/guidelines/index.html
},
],
});
}
// checks if the post type is a video and not copyrighted
} else if (data.media_type === 'video') {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: process.env.NASAAPOD_CHANNEL_ID,
content: `π **Astronomy Picture of the Day** π`,
embeds: [
{
title: data.title,
description: data.explanation,
fields: [
{
name: `Due to Discord not allowing to display videos in rich embeds, you'll have to visit the webpage`,
value: `https://apod.nasa.gov/apod/astropix.html`,
},
],
color: 0x0165b3,
},
],
});
// checks if the post type is an image and not copyrighted
} else if (data.media_type === 'image') {
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: process.env.NASAAPOD_CHANNEL_ID,
content: `π **Astronomy Picture of the Day** π`,
embeds: [
{
title: data.title,
description: data.explanation,
image: {url: data.url},
color: 0x0165b3,
},
],
});
}