diff --git a/commands/search/gelbooru.js b/commands/search/gelbooru.js index 67421e05..2db5c8f7 100644 --- a/commands/search/gelbooru.js +++ b/commands/search/gelbooru.js @@ -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!`); } diff --git a/commands/search/safebooru.js b/commands/search/safebooru.js index b80cf96d..2a06fc51 100644 --- a/commands/search/safebooru.js +++ b/commands/search/safebooru.js @@ -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!`); }