From badca78816e644cdc031b2c66ac08b8b56657a0a Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 18 Oct 2017 22:45:01 +0000 Subject: [PATCH] Revenge of the Sith --- commands/search/danbooru.js | 45 ++++++++++++++++++++++++++++++++++++ commands/search/gelbooru.js | 45 ++++++++++++++++++++++++++++++++++++ commands/search/konachan.js | 41 ++++++++++++++++++++++++++++++++ commands/search/safebooru.js | 2 +- package.json | 2 +- 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 commands/search/danbooru.js create mode 100644 commands/search/gelbooru.js create mode 100644 commands/search/konachan.js diff --git a/commands/search/danbooru.js b/commands/search/danbooru.js new file mode 100644 index 00000000..cf541a99 --- /dev/null +++ b/commands/search/danbooru.js @@ -0,0 +1,45 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { stripIndents } = require('common-tags'); + +module.exports = class DanbooruCommand extends Command { + constructor(client) { + super(client, { + name: 'danbooru', + aliases: ['danbooru-image'], + group: 'search', + memberName: 'danbooru', + description: 'Searches Danbooru for your query.', + args: [ + { + key: 'query', + prompt: 'What image would you like to search for?', + type: 'string', + validate: query => { + if (!query.includes(' ')) return true; + return 'Invalid query, please only search for one tag at a time.'; + } + } + ] + }); + } + + async run(msg, { query }) { + if (!msg.channel.nsfw) return msg.say('This command can only be used in NSFW channels.'); + try { + const { body } = await snekfetch + .get('https://danbooru.donmai.us/posts.json') + .query({ + tags: `${query} order:random`, + limit: 1 + }); + if (!body.length || !body[0].file_url) return msg.say('Could not find any results.'); + return msg.say(stripIndents` + Result for ${query}: + https://danbooru.donmai.us${body[0].file_url} + `); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/gelbooru.js b/commands/search/gelbooru.js new file mode 100644 index 00000000..3152ca23 --- /dev/null +++ b/commands/search/gelbooru.js @@ -0,0 +1,45 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { xml2js } = require('xml-js'); +const { stripIndents } = require('common-tags'); + +module.exports = class GelbooruCommand extends Command { + constructor(client) { + super(client, { + name: 'gelbooru', + aliases: ['gelbooru-image'], + group: 'search', + memberName: 'gelbooru', + description: 'Searches Gelbooru for your query.', + args: [ + { + key: 'query', + prompt: 'What image would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + if (!msg.channel.nsfw) return msg.say('This command can only be used in NSFW channels.'); + try { + const { text } = await snekfetch + .get('https://gelbooru.com/index.php') + .query({ + page: 'dapi', + s: 'post', + q: 'index', + tags: query + }); + const parsed = xml2js(text, { compact: true }).posts; + if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.'); + return msg.say(stripIndents` + Result for ${query}: + https:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url} + `); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/konachan.js b/commands/search/konachan.js new file mode 100644 index 00000000..98baf2e8 --- /dev/null +++ b/commands/search/konachan.js @@ -0,0 +1,41 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { stripIndents } = require('common-tags'); + +module.exports = class KonachanCommand extends Command { + constructor(client) { + super(client, { + name: 'konachan', + aliases: ['konachan-image'], + group: 'search', + memberName: 'konachan', + description: 'Searches Konachan for your query.', + args: [ + { + key: 'query', + prompt: 'What image would you like to search for?', + type: 'string' + } + ] + }); + } + + async run(msg, { query }) { + if (!msg.channel.nsfw) return msg.say('This command can only be used in NSFW channels.'); + try { + const { body } = await snekfetch + .get('https://konachan.net/post.json') + .query({ + tags: `${query} order:random`, + limit: 1 + }); + if (!body.length || !body[0].file_url) return msg.say('Could not find any results.'); + return msg.say(stripIndents` + Result for ${query}: + https:${body[0].file_url} + `); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/safebooru.js b/commands/search/safebooru.js index 8af26bbb..78175df0 100644 --- a/commands/search/safebooru.js +++ b/commands/search/safebooru.js @@ -35,7 +35,7 @@ module.exports = class SafebooruCommand extends Command { if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.'); return msg.say(stripIndents` Result for ${query}: - http:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url} + https:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url} `); } catch (err) { return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); diff --git a/package.json b/package.json index e7dc3f27..e1ff7e7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "47.7.0", + "version": "47.8.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {