This commit is contained in:
Daniel Odendahl Jr
2017-12-04 17:15:55 +00:00
parent cf1f6739f7
commit bd8a0235f6
2 changed files with 9 additions and 16 deletions
+4 -8
View File
@@ -1,8 +1,5 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
module.exports = class GelbooruCommand extends Command {
constructor(client) {
@@ -26,19 +23,18 @@ module.exports = class GelbooruCommand extends Command {
async run(msg, { query }) {
try {
const { text } = await snekfetch
const { body } = await snekfetch
.get('https://gelbooru.com/index.php')
.query({
page: 'dapi',
s: 'post',
q: 'index',
json: 1,
tags: query,
limit: 200
});
const body = await xml(text);
const data = body.posts.post;
if (!data || !data.length) return msg.say('Could not find any results.');
return msg.say(data[Math.floor(Math.random() * data.length)].$.file_url);
if (!body) return msg.say('Could not find any results.');
return msg.say(body[Math.floor(Math.random() * body.length)].file_url);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
+5 -8
View File
@@ -1,8 +1,5 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
module.exports = class SafebooruCommand extends Command {
constructor(client) {
@@ -25,19 +22,19 @@ module.exports = class SafebooruCommand extends Command {
async run(msg, { query }) {
try {
const { text } = await snekfetch
const { body } = await snekfetch
.get('https://safebooru.org/index.php')
.query({
page: 'dapi',
s: 'post',
q: 'index',
json: 1,
tags: query,
limit: 200
});
const body = await xml(text);
const data = body.posts.post;
if (!data || !data.length) return msg.say('Could not find any results.');
return msg.say(`https:${data[Math.floor(Math.random() * data.length)].$.file_url}`);
if (!body) return msg.say('Could not find any results.');
const data = body[Math.floor(Math.random() * body.length)];
return msg.say(`https://safebooru.org/images/${data.directory}/${data.image}`);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}