Files
xiao/commands/search/rule34.js
T
Daniel Odendahl Jr d26af498ad snekfetch 4.0.0
2018-04-24 21:27:16 +00:00

46 lines
1.1 KiB
JavaScript

const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
module.exports = class Rule34Command extends Command {
constructor(client) {
super(client, {
name: 'rule34',
aliases: ['rule34-image'],
group: 'search',
memberName: 'rule34',
description: 'Responds with an image from Rule34, with optional query.',
nsfw: true,
args: [
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string',
default: ''
}
]
});
}
async run(msg, { query }) {
try {
const { raw } = await snekfetch
.get('https://rule34.xxx/index.php')
.query({
page: 'dapi',
s: 'post',
q: 'index',
json: 1,
tags: query,
limit: 200
});
const text = raw.toString();
if (!text) return msg.say('Could not find any results.');
const body = JSON.parse(text);
const data = body[Math.floor(Math.random() * body.length)];
return msg.say(`https://rule34.xxx/images/${data.directory}/${data.image}`);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};