Files
xiao/commands/search/google.js
T
Daniel Odendahl Jr 8074fe51bd Move to snekfetch 🐍
2017-04-16 01:12:22 +00:00

44 lines
1.5 KiB
JavaScript

const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const cheerio = require('cheerio');
const querystring = require('querystring');
module.exports = class GoogleCommand extends Command {
constructor(client) {
super(client, {
name: 'google',
aliases: [
'search'
],
group: 'search',
memberName: 'google',
description: 'Searches Google. (;google Cat)',
examples: [';google Cat'],
args: [{
key: 'query',
prompt: 'What would you like to search for?',
type: 'string'
}]
});
}
async run(message, args) {
if (message.channel.type !== 'dm') {
if (!message.channel.permissionsFor(this.client.user).hasPermission(['SEND_MESSAGES', 'READ_MESSAGES'])) return;
}
const query = encodeURIComponent(args.query);
const msg = await message.say('Searching...');
try {
const response = await snekfetch
.get(`https://www.google.com/search?q=${query}`);
const $ = cheerio.load(response.text);
let href = $('.r').first().find('a').first().attr('href');
href = querystring.parse(href.replace('/url?', ''));
return msg.edit(href.q);
}
catch (err) {
return msg.edit(':x: Error! No Results Found!');
}
}
};