From 72b5a8955031c3ac4a4b761b22b4ac375823a0b2 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Fri, 12 Oct 2018 00:40:11 +0000 Subject: [PATCH] Mayo Clinic Command --- README.md | 3 +- commands/search/mayo-clinic.js | 62 ++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 commands/search/mayo-clinic.js diff --git a/README.md b/README.md index bc9fab1f..3c8dda3f 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Xiao is a Discord bot coded in JavaScript with Xiao is no longer available for invite. You can self-host the bot, or use her on the [home server](https://discord.gg/sbMe32W). -## Commands (327) +## Commands (328) ### Utility: * **eval:** Executes JavaScript code. @@ -152,6 +152,7 @@ the [home server](https://discord.gg/sbMe32W). * **kitsu-manga:** Searches Kitsu.io for your query, getting manga results. * **know-your-meme:** Searches Know Your Meme for your query. * **league-of-legends-champion:** Responds with information on a League of Legends champion. +* **mayo-clinic:** Searches Mayo Clinic for your query. * **mdn:** Searches MDN for your query. * **nasa:** Searches NASA's image archive for your query. * **neopet:** Responds with the image of a specific Neopet. diff --git a/commands/search/mayo-clinic.js b/commands/search/mayo-clinic.js new file mode 100644 index 00000000..c9476755 --- /dev/null +++ b/commands/search/mayo-clinic.js @@ -0,0 +1,62 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const cheerio = require('cheerio'); +const { MessageEmbed } = require('discord.js'); +const { shorten } = require('../../util/Util'); + +module.exports = class MayoClinicCommand extends Command { + constructor(client) { + super(client, { + name: 'mayo-clinic', + aliases: ['disease', 'diagnose', 'diagnosis'], + group: 'search', + memberName: 'mayo-clinic', + description: 'Searches Mayo Clinic for your query.', + clientPermissions: ['EMBED_LINKS'], + args: [ + { + key: 'query', + prompt: 'What disease would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + try { + const location = await this.search(query); + if (!location) return msg.say('Could not find any results.'); + const data = await this.fetchDisease(location); + const embed = new MessageEmbed() + .setColor(0x0044B3) + .setAuthor('Mayo Clinic', 'https://i.imgur.com/9zdulOS.jpg', 'https://www.mayoclinic.org/') + .setTitle(data.name) + .setDescription(shorten(data.description || 'No description available.')) + .setURL(data.url); + return msg.embed(embed); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async search(query) { + const { text } = await request + .get('https://www.mayoclinic.org/search/search-results') + .query({ q: query }); + const $ = cheerio.load(text); + const location = $('ol.navlist').find('li.noimg').first().children().find('a').attr('href'); + if (!location) return null; + return location; + } + + async fetchDisease(location) { + const { text } = await request.get(location); + const $ = cheerio.load(text); + return { + name: $('h1').first().text().trim(), + url: location, + description: $('h2').first().next().text() + }; + } +}; diff --git a/package.json b/package.json index 4dd3aff4..19a01308 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "94.0.3", + "version": "94.1.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {