Files
xiao/commands/search/wikihow.js
T
Daniel Odendahl Jr 45c8d62dd5 Credit Command
2019-04-14 00:03:38 +00:00

53 lines
1.3 KiB
JavaScript

const Command = require('../../structures/Command');
const request = require('node-superfetch');
const { stripIndents } = require('common-tags');
module.exports = class WikihowCommand extends Command {
constructor(client) {
super(client, {
name: 'wikihow',
aliases: ['how-to', 'how'],
group: 'search',
memberName: 'wikihow',
description: 'Searches Wikihow for your query.',
credit: [
{
name: 'wikiHow',
url: 'https://www.wikihow.com/Main-Page'
}
],
args: [
{
key: 'query',
prompt: 'What article would you like to search for?',
type: 'string',
parse: query => query.replace(/^((how )?to )/i, '')
}
]
});
}
async run(msg, { query }) {
try {
const { body } = await request
.get('https://www.wikihow.com/api.php')
.query({
action: 'query',
prop: 'info',
format: 'json',
titles: query,
inprop: 'url',
redirects: ''
});
const data = body.query.pages[Object.keys(body.query.pages)[0]];
if (data.missing === '') return msg.say('Could not find any results.');
return msg.say(stripIndents`
How to ${data.title}
${data.fullurl}
`);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};