Based on your query, this comand displays a randomly chosen public domain image from the Met Open Access API
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// Command to explore met
await lib.discord.commands['@0.0.0'].create({
name: 'explore-met',
description:
'Explore the Met Open Access collection by getting a random image based on a search term',
options: [
{
type: 3,
name: 'query',
description: 'Keywords to search for',
required: true,
},
],
});
// Set query variable from command
let searchQ = context.params.event.data.options[0].value;
console.log('Search query: ' + searchQ);
// Helper function to return random item from array
function rand(items) {
// "|" for a kinda "int div"
return items[(items.length * Math.random()) | 0];
}
// Create ephemeral response message for no results behavior
async function createEphemMessage(token, string) {
let result = await lib.discord.interactions[
'@1.0.1'
].responses.ephemeral.create({
token: token,
content: string,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
});
return result;
}
// Send a request to the Met Open Access API to get an array of usable objectIDs matching the query
async function searchMet(searchQ, titleOnly) {
// Send a request to the API
let result = await lib.http.request['@1.1.7'].get({
url: `https://collectionapi.metmuseum.org/public/collection/v1/search`,
queryParams: {
hasImages: true,
title: titleOnly,
q: searchQ.replace(/ /g, '%20'),
},
});
// If it's only searching titles and nothing comes up, try again searching outside titles
if (result.data.total == 0 && titleOnly == true) {
console.log('No results in title, trying general search');
result = await searchMet(searchQ, false);
// If searching everything and no objectIDs returned, return false
} else if (result.data.total == 0 && titleOnly == false) {
console.log('Could not find query anywhere!');
return false;
} else {
// Do nothing
}
console.log(result);
return result;
}
// Chooses an objectID randomly from results and gets the object data
async function getMetObject(objectIDs) {
// Make sure there are actually objects in the arguement passed
if (objectIDs.length !== 0) {
// Pick a random object ID
let selectedID = rand(objectIDs);
// Send request to the met API
let metObject = await lib.http.request['@1.1.7'].get({
url:
'https://collectionapi.metmuseum.org/public/collection/v1/objects/' +
selectedID,
});
// Check if the returned object is in the public domain, if it's not, remove it from the object array and try again
if (metObject.data.isPublicDomain == false) {
let filteredObjArray = objectIDs.filter(function (e) {
return e !== selectedID;
});
return getMetObject(filteredObjArray);
} else {
return metObject;
}
} else {
return false;
}
}
// Display the valid results retrieved from the Met API
async function displayMetResult(objectIDs) {
let imageResult = await getMetObject(objectIDs);
console.log(imageResult);
// Make sure you actually got something
if (imageResult) {
// Get the username of the person using the command
let searchingUser = context.params.event.member.user.id;
// Set the data for the embed
let resultDepartment = imageResult.data.department || 'not found';
let resultClassification = imageResult.data.classification || 'not found';
let resultArtistName = imageResult.data.artistDisplayName || 'not found';
let resultYear = imageResult.data.objectDate || 'year not found';
// Create a response with results showing image and data
let result = await lib.discord.interactions['@1.0.1'].responses.create({
token: `${context.params.event.token}`, // required
content:
`<@!${context.params.event.member.user.id}>, here's your random image from Met Open Access based on your search term "` +
searchQ +
`"`,
embeds: [
{
type: 'rich',
title:
imageResult.data.title +
', ' +
resultArtistName +
', ' +
resultYear,
description: '',
color: 0x8e3384,
fields: [
{
name: `Department`,
value: resultDepartment,
inline: true
},
{
name: `Classification`,
value: resultClassification,
inline: true
},
{
name: `Object ID`,
value: imageResult.data.objectID,
inline: true
},
],
image: {
url: imageResult.data.primaryImage,
height: 0,
width: 0,
},
author: {
name: `Met Open Access`,
},
url: imageResult.data.objectURL,
},
],
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
});
} else {
// If there are no results with images let the user know (this should basically never happen at this point)
console.log('Results but none with available images');
let ephemString =
`No results in the Met Open Access for your search term "` +
searchQ +
`"`;
return await createEphemMessage(context.params.event.token, ephemString);
}
}
// make API request to get list of museum objects matching query
let metResults = await searchMet(searchQ, true);
// Check for valid results
if (metResults) {
// Display the result
let result = await displayMetResult(metResults.data.objectIDs);
} else {
// Send user message that the query returned no results
let ephemString =
`No results in the Met Open Access for your search term "` + searchQ + `"`;
let result = await createEphemMessage(
context.params.event.token,
ephemString
);
}