This snippet is an example of how you can make a request to an external API like Google Books API and send retrieved data into Discord. For a complete explanation check out the guide: https://autocode.com/autocode/threads/how-to-make-a-request-to-an-external-api-using-autocode-tutorial-c05d083a/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let search = context.params.event.content.replace('!search', '').split('author:')[0].trim();
let author = context.params.event.content.replace('!search', '').split('author:')[1].trim();
search = search.replace('&', '');
author = author.replace('&', '');
// make API request
let result = await lib.http.request['@1.1.6'].get({
url: `https://www.googleapis.com/books/v1/volumes`,
headers: {
key: ``,
},
queryParams: {
q: `${search}`,
inauthor: `${author}`,
},
});
console.log(result.data.items);
let books = result.data.items.slice(0, 5);
for (let i = 0; i < books.length; i++) {
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: 'Here are your search results!',
tts: false,
embeds: [
{
type: 'rich',
title: `${books[i].volumeInfo.title} by ${books[i].volumeInfo.authors}`,
description: result.data.items[i].volumeInfo.description,
color: 0x00c3ff,
fields: [
{
name: 'Published Date:',
value: `${books[i].volumeInfo.publishedDate}`,
inline: true,
},
{
name: 'Page Count:',
value: `${books[i].volumeInfo.pageCount}`,
inline: true,
},
{
name: `Rating:`,
value: `${books[i].volumeInfo.averageRating}`,
inline: true,
},
],
image: {
url: `${books[i].volumeInfo.imageLinks.thumbnail}`,
height: 0,
width: 0,
},
url: `${books[i].volumeInfo.previewLink}`,
},
],
});
}