Files
xiao/commands/search/wattpad.js
T
Daniel Odendahl Jr 6b4beb3bbe better errors
2017-05-16 17:35:53 +00:00

61 lines
2.2 KiB
JavaScript

const { Command } = require('discord.js-commando');
const { RichEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { WATTPAD_KEY } = process.env;
module.exports = class WattpadCommand extends Command {
constructor(client) {
super(client, {
name: 'wattpad',
group: 'search',
memberName: 'wattpad',
description: 'Searches Wattpad for a book.',
args: [
{
key: 'query',
prompt: 'What book would you like to search for?',
type: 'string'
}
]
});
}
async run(msg, args) {
if (msg.channel.type !== 'dm')
if (!msg.channel.permissionsFor(this.client.user).has('EMBED_LINKS'))
return msg.say('This Command requires the `Embed Links` Permission.');
const { query } = args;
try {
const { body } = await snekfetch
.get('https://api.wattpad.com:443/v4/stories')
.query({
query,
limit: 1
})
.set({ 'Authorization': `Basic ${WATTPAD_KEY}` });
if (!body.stories.length) throw new Error('No Results.');
const embed = new RichEmbed()
.setColor(0xF89C34)
.setAuthor('Wattpad', 'https://i.imgur.com/Rw9vRQB.png')
.setURL(body.stories[0].url)
.setTitle(body.stories[0].title)
.setDescription(body.stories[0].description.substr(0, 2000))
.addField('Author',
body.stories[0].user, true)
.addField('Parts',
body.stories[0].numParts, true)
.addField('Created On',
body.stories[0].createDate, true)
.addField('Votes',
body.stories[0].voteCount, true)
.addField('Reads',
body.stories[0].readCount, true)
.addField('Comments',
body.stories[0].commentCount, true);
return msg.embed(embed);
} catch (err) {
return msg.say(`${err.name}: ${err.message}`);
}
}
};