Files
xiao/commands/search/wattpad.js
T
2018-07-02 01:42:04 +00:00

56 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const request = require('node-superfetch');
const { shorten } = require('../../util/Util');
const { WATTPAD_KEY } = process.env;
module.exports = class WattpadCommand extends Command {
constructor(client) {
super(client, {
name: 'wattpad',
aliases: ['wattpad-book'],
group: 'search',
memberName: 'wattpad',
description: 'Searches Wattpad for your query.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'query',
prompt: 'What book would you like to search for?',
type: 'string'
}
]
});
}
async run(msg, { query }) {
try {
const { body } = await request
.get('https://api.wattpad.com/v4/stories')
.query({
query,
limit: 1
})
.set({ Authorization: `Basic ${WATTPAD_KEY}` });
if (!body.stories.length) return msg.say('Could not find any results.');
const data = body.stories[0];
const embed = new MessageEmbed()
.setColor(0xF89C34)
.setAuthor('Wattpad', 'https://i.imgur.com/lFTXnlz.png', 'https://www.wattpad.com/')
.setURL(data.url)
.setTitle(data.title)
.setDescription(shorten(data.description))
.setThumbnail(data.cover)
.addField(' Creation Date', new Date(data.createDate).toDateString(), true)
.addField(' Author', data.user.name, true)
.addField(' Chapters', data.numParts, true)
.addField(' Reads', data.readCount, true)
.addField(' Votes', data.voteCount, true)
.addField(' Comments', data.commentCount, true);
return msg.embed(embed);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};