Deviantart command, some stuff

This commit is contained in:
Daniel Odendahl Jr
2017-11-11 23:21:33 +00:00
parent 9a1d3c4ff6
commit 35b61e1105
6 changed files with 84 additions and 10 deletions
+2 -1
View File
@@ -8,13 +8,14 @@ module.exports = class DanbooruCommand extends Command {
aliases: ['danbooru-image'],
group: 'search',
memberName: 'danbooru',
description: 'Searches Danbooru for your query.',
description: 'Responds with an image from Danbooru, with optional query.',
nsfw: true,
args: [
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string',
default: '',
validate: query => {
if (!query.includes(' ')) return true;
return 'Invalid query, please only search for one tag at a time.';
+68
View File
@@ -0,0 +1,68 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const { list } = require('../../util/Util');
const { DEVIANTART_ID, DEVIANTART_SECRET } = process.env;
const sections = ['dailydeviations', 'hot', 'newest', 'popular', 'undiscovered'];
module.exports = class DeviantartCommand extends Command {
constructor(client) {
super(client, {
name: 'deviantart',
group: 'search',
memberName: 'deviantart',
description: 'Responds with an image from a DeviantArt section, with optional query.',
args: [
{
key: 'section',
prompt: `What section would you like to search? Either ${list(sections, 'or')}.`,
type: 'string',
validate: section => {
if (sections.includes(section.toLowerCase())) return true;
return `Invalid section, please enter either ${list(sections), 'or'}.`;
},
parse: section => section.toLowerCase()
},
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string',
default: ''
}
]
});
this.token = null;
}
async run(msg, { section, query }) {
try {
if (!this.token) await this.fetchToken();
const { body } = await snekfetch
.get(`https://www.deviantart.com/api/v1/oauth2/browse/${section}`)
.query({
q: query,
limit: 120,
access_token: this.token,
mature_content: msg.channel.nsfw || false
});
const images = body.results.filter(image => image.content && image.content.src);
if (!images.length) return msg.say('Could not find any results.');
return msg.say(images[Math.floor(Math.random() * images.length)].content.src);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
async fetchToken() {
const { body } = await snekfetch
.get('https://www.deviantart.com/oauth2/token')
.query({
grant_type: 'client_credentials',
client_id: DEVIANTART_ID,
client_secret: DEVIANTART_SECRET
});
this.token = body.access_token;
this.client.setTimeout(() => { this.token = null; }, body.expires_in * 1000);
return body;
}
};
+5 -3
View File
@@ -11,13 +11,14 @@ module.exports = class GelbooruCommand extends Command {
aliases: ['gelbooru-image'],
group: 'search',
memberName: 'gelbooru',
description: 'Searches Gelbooru for your query.',
description: 'Responds with an image from Gelbooru, with optional query.',
nsfw: true,
args: [
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string'
type: 'string',
default: ''
}
]
});
@@ -31,7 +32,8 @@ module.exports = class GelbooruCommand extends Command {
page: 'dapi',
s: 'post',
q: 'index',
tags: query
tags: query,
limit: 200
});
const body = await xml(text);
const data = body.posts.post;
+3 -2
View File
@@ -8,13 +8,14 @@ module.exports = class KonachanCommand extends Command {
aliases: ['konachan-image'],
group: 'search',
memberName: 'konachan',
description: 'Searches Konachan for your query.',
description: 'Responds with an image from Konachan, with optional query.',
nsfw: true,
args: [
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string'
type: 'string',
default: ''
}
]
});
+5 -3
View File
@@ -11,12 +11,13 @@ module.exports = class SafebooruCommand extends Command {
aliases: ['safebooru-image'],
group: 'search',
memberName: 'safebooru',
description: 'Searches Safebooru for your query.',
description: 'Responds with an image from Safebooru, with optional query.',
args: [
{
key: 'query',
prompt: 'What image would you like to search for?',
type: 'string'
type: 'string',
default: ''
}
]
});
@@ -30,7 +31,8 @@ module.exports = class SafebooruCommand extends Command {
page: 'dapi',
s: 'post',
q: 'index',
tags: query
tags: query,
limit: 200
});
const body = await xml(text);
const data = body.posts.post;
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "54.1.1",
"version": "54.2.0",
"description": "Your personal server companion.",
"main": "XiaoBot.js",
"scripts": {