From dfee509e757250e81751f5010df880e9b990c83b Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 15 May 2017 17:28:44 +0000 Subject: [PATCH] XKCD Command --- commands/randomimg/xkcd.js | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 commands/randomimg/xkcd.js diff --git a/commands/randomimg/xkcd.js b/commands/randomimg/xkcd.js new file mode 100644 index 00000000..c2c997a4 --- /dev/null +++ b/commands/randomimg/xkcd.js @@ -0,0 +1,47 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); + +module.exports = class XKCDCommand extends Command { + constructor(client) { + super(client, { + name: 'xkcd', + group: 'randomimg', + memberName: 'xkcd', + description: 'Gets an XKCD Comic, optionally opting for today\'s.', + args: [ + { + key: 'type', + prompt: 'Would you like to get the comic for today or random?', + type: 'string', + validate: type => { + if (['today', 'random'].includes(type.toLowerCase())) return true; + return 'Please enter either `today` or `random`'; + }, + default: 'random' + } + ] + }); + } + + async run(msg, args) { + if (msg.channel.type !== 'dm') + if (!msg.channel.permissionsFor(this.client.user).has('ATTACH_FILES')) + return msg.say('This Command requires the `Attach Files` Permission.'); + const { type } = args; + try { + const current = await snekfetch + .get('https://xkcd.com/info.0.json'); + if (type === 'today') return msg.channel.send({ files: [current.body.img] }) + .catch(err => msg.say(err)); + else { + const random = Math.floor(Math.random() * current.body.num) + 1; + const { body } = await snekfetch + .get(`https://xkcd.com/${random}/info.0.json`); + return msg.channel.send({ files: [body.img] }) + .catch(err => msg.say(err)); + } + } catch (err) { + return msg.say(err); + } + } +};