This commit is contained in:
Daniel Odendahl Jr
2017-09-27 16:49:37 +00:00
parent 9f1b9688ae
commit 8a70e44902
16 changed files with 88 additions and 18 deletions
+20 -5
View File
@@ -1,7 +1,12 @@
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { xml2js } = require('xml-js');
const { stripIndents } = require('common-tags');
const { shorten } = require('../../structures/Util');
const ratings = {
s: 'Safe',
q: 'Questionable'
};
module.exports = class SafebooruCommand extends Command {
constructor(client) {
@@ -11,6 +16,7 @@ module.exports = class SafebooruCommand extends Command {
group: 'search',
memberName: 'safebooru',
description: 'Searches Safebooru for your query.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'query',
@@ -36,10 +42,19 @@ module.exports = class SafebooruCommand extends Command {
if (parsed._attributes.count === '0' || !parsed.post.length) return msg.say('Could not find any results.');
const posts = msg.channel.nsfw ? parsed.post : parsed.post.filter(post => post._attributes.rating === 's');
if (!posts.length) return msg.say('Could not find any results.');
return msg.say(stripIndents`
${query ? `Results for ${query}:` : 'Random Image:'}
https:${posts[Math.floor(Math.random() * posts.length)]._attributes.file_url}
`);
const data = posts[Math.floor(Math.random() * posts.length)]._attributes;
const embed = new MessageEmbed()
.setAuthor('Safebooru', 'https://i.imgur.com/iGMNwhf.jpg')
.setColor(0xC6D2E1)
.setURL(`http://safebooru.org/index.php?page=post&s=view&id=${data.id}`)
.setImage(`https:${data.file_url}`)
.addField(' Upload Date',
new Date(data.created_at).toDateString(), true)
.addField(' Rating',
ratings[data.rating], true)
.addField(' Tags',
shorten(data.tags, 1000));
return msg.embed(embed);
} catch (err) {
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}