diff --git a/commands/random-img/safebooru.js b/commands/random-img/safebooru.js new file mode 100644 index 00000000..0355913c --- /dev/null +++ b/commands/random-img/safebooru.js @@ -0,0 +1,44 @@ +const Command = require('../../structures/Command'); +const { stripIndents } = require('common-tags'); +const snekfetch = require('snekfetch'); +const { promisifyAll } = require('tsubaki'); +const xml = promisifyAll(require('xml2js')); + +module.exports = class SafebooruCommand extends Command { + constructor(client) { + super(client, { + name: 'safebooru', + group: 'random-img', + memberName: 'safebooru', + description: 'Searches Safebooru with optional query.', + nsfw: true, + args: [ + { + key: 'query', + prompt: 'What would you like to search for?', + type: 'string', + default: '' + } + ] + }); + } + + async run(msg, args) { + const { query } = args; + const { text } = await snekfetch + .get('http://safebooru.org/index.php') + .query({ + page: 'dapi', + s: 'post', + q: 'index', + tags: query, + limit: 200 + }); + const { posts } = await xml.parseStringAsync(text); + if (posts.$.count === '0') return msg.say('No Results.'); + return msg.say(stripIndents` + ${query ? `Result for ${query}:` : 'Random Image:'} + http:${posts.post[Math.floor(Math.random() * posts.post.length)].$.file_url} + `); + } +}; diff --git a/package.json b/package.json index 74de2467..5637383b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "29.1.7", + "version": "29.2.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {