From f3aa13e4c6807dbf01a20687f6202c5b4258f8ac Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 16 Sep 2017 17:02:22 +0000 Subject: [PATCH] New York Times Command --- commands/random-res/new-york-times.js | 51 +++++++++++++++++++++++++++ commands/random/new-york-times.js | 0 package.json | 2 +- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 commands/random-res/new-york-times.js create mode 100644 commands/random/new-york-times.js diff --git a/commands/random-res/new-york-times.js b/commands/random-res/new-york-times.js new file mode 100644 index 00000000..0b28c7f7 --- /dev/null +++ b/commands/random-res/new-york-times.js @@ -0,0 +1,51 @@ +const Command = require('../../structures/Command'); +const { MessageEmbed } = require('discord.js'); +const snekfetch = require('snekfetch'); +const { NYTIMES_KEY } = process.env; + +module.exports = class NewYorkTimesCommand extends Command { + constructor(client) { + super(client, { + name: 'new-york-times', + aliases: ['ny-times'], + group: 'random-res', + memberName: 'new-york-times', + description: 'Responds with an article from the New York Times.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What article do you want to search for?', + type: 'string', + default: '' + } + ] + }); + } + + async run(msg, { query }) { + try { + const fetch = snekfetch + .get(`https://api.nytimes.com/svc/search/v2/articlesearch.json`) + .query({ + 'api-key': NYTIMES_KEY, + sort: 'newest' + }); + if (query) fetch.query({ q: query }); + const { body } = await fetch; + if (!body.response.docs.length) return msg.say('Could not find any results'); + const data = body.response.docs[Math.floor(Math.random() * body.response.docs.length)]; + const embed = new MessageEmbed() + .setColor(0xF6F6F6) + .setAuthor('New York Times', 'https://i.imgur.com/trXljtY.png') + .setURL(data.web_url) + .setTitle(data.headline.main) + .setDescription(data.snippet) + .addField('❯ Publish Date', + new Date(data.pub_date).toDateString(), true); + return msg.embed(embed); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/random/new-york-times.js b/commands/random/new-york-times.js new file mode 100644 index 00000000..e69de29b diff --git a/package.json b/package.json index fff49457..e2d101ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "40.1.0", + "version": "40.2.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {