From b32cf4c2d90fc96f878d7fa7b4c1182df35d0845 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 16 Sep 2017 02:20:13 +0000 Subject: [PATCH] Flickr Command --- commands/search/flickr.js | 40 +++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 commands/search/flickr.js diff --git a/commands/search/flickr.js b/commands/search/flickr.js new file mode 100644 index 00000000..ef0a63b4 --- /dev/null +++ b/commands/search/flickr.js @@ -0,0 +1,40 @@ +const Command = require('../../structures/Command'); +const snekfetch = require('snekfetch'); +const { FLICKR_KEY } = process.env; + +module.exports = class FlickrCommand extends Command { + constructor(client) { + super(client, { + name: 'flickr', + group: 'search', + memberName: 'flickr', + description: 'Searches Flickr for your query.', + args: [ + { + key: 'query', + prompt: 'What photo would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const { body } = await snekfetch + .get('https://api.flickr.com/services/rest/') + .query({ + api_key: FLICKR_KEY, + format: 'json', + method: 'flickr.photos.search', + text: query, + nojsoncallback: true + }); + if (!body.photos.photo.length) return msg.say('Could not find any results.'); + const photo = body.photos.photo[Math.floor(Math.random() * body.photos.photo.length)]; + return msg.say(`https://farm${photo.farm}.staticflickr.com/${photo.server}/${photo.id}_${photo.secret}.jpg`); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index b544e609..331fabcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "39.0.1", + "version": "39.1.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {