mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-26 14:19:11 +02:00
Mayo Clinic Command
This commit is contained in:
@@ -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()
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user