From d2625ac26e744c33b12cb0f3550ab969fbf5e7c3 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 18 Nov 2017 15:56:36 +0000 Subject: [PATCH] Book Command --- commands/search/google-books.js | 60 +++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 commands/search/google-books.js diff --git a/commands/search/google-books.js b/commands/search/google-books.js new file mode 100644 index 00000000..e151df21 --- /dev/null +++ b/commands/search/google-books.js @@ -0,0 +1,60 @@ +const { Command } = require('discord.js-commando'); +const { MessageEmbed } = require('discord.js'); +const snekfetch = require('snekfetch'); +const { shorten } = require('../../util/Util'); +const { GOOGLE_KEY } = process.env; + +module.exports = class GoogleBookCommand extends Command { + constructor(client) { + super(client, { + name: 'google-book', + aliases: ['google-books', 'book'], + group: 'search', + memberName: 'google-book', + description: 'Searches Google Books for a book.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What book would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const { body } = await snekfetch + .get('https://www.googleapis.com/books/v1/volumes') + .query({ + apiKey: GOOGLE_KEY, + q: query, + maxResults: 1, + printType: 'books' + }); + if (!body.items) return msg.say('Could not find any results.'); + const data = body.items[0].volumeInfo; + const embed = new MessageEmbed() + .setColor(0x4285F4) + .setTitle(data.title) + .setURL(data.previewLink) + .setAuthor('Google Books', 'https://i.imgur.com/N3oHABo.png') + .setDescription(data.description ? shorten(data.description) : 'No description available.') + .setThumbnail(data.imageLinks ? data.imageLinks.thumbnail : null) + .addField('❯ Authors', + data.authors.length ? data.authors.join(', ') : '???') + .addField('❯ Publisher', + data.publisher || '???', true) + .addField('❯ Publish Date', + data.publishedDate || '???', true) + .addField('❯ Page Count', + data.pageCount || '???', true) + .addField('❯ Genres', + data.categories.length ? data.categories.join(', ') : '???'); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 44f8d69b..9304a79e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "55.0.1", + "version": "55.1.0", "description": "Your personal server companion.", "main": "XiaoBot.js", "scripts": {