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

59 lines
2.4 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { RichEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
module.exports = class WattpadCommand extends Command {
constructor(client) {
super(client, {
name: 'wattpad',
group: 'search',
memberName: 'wattpad',
description: 'Searches Wattpad for a specified book. (;wattpad Heroes of Dreamland)',
examples: [';wattpad Heroes of Dreamland'],
args: [{
key: 'book',
prompt: 'What book 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;
if (!message.channel.permissionsFor(this.client.user).hasPermission('EMBED_LINKS')) return message.say(':x: Error! I don\'t have the Embed Links Permission!');
}
const book = encodeURIComponent(args.book);
try {
const response = await snekfetch
.get(`https://api.wattpad.com:443/v4/stories?query=${book}&limit=1`)
.set({
'Authorization': `Basic ${process.env.WATTPAD_KEY}`
});
const data = response.body.stories[0];
const embed = new RichEmbed()
.setColor(0xF89C34)
.setAuthor('Wattpad', 'http://www.selfpubtoolbox.com/wp-content/uploads/2015/05/a6044fd3a88acd5043860484db972ca6.png')
.setURL(data.url)
.setTitle(data.title)
.setDescription(data.description.substr(0, 1500))
.addField('**Author:**',
data.user, true)
.addField('**Parts:**',
data.numParts, true)
.addField('**Created On:**',
data.createDate, true)
.addField('**Votes:**',
data.voteCount, true)
.addField('**Reads:**',
data.readCount, true)
.addField('**Comments:**',
data.commentCount, true);
return message.embed(embed);
}
catch (err) {
return message.say(':x: Error! Book not Found!');
}
}
};