From 08007fa4e7d72721cf8b79269ef59e3c7546959c Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sat, 21 Oct 2017 01:54:40 +0000 Subject: [PATCH] Google Doodle Command --- commands/other/google-doodle.js | 46 +++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 commands/other/google-doodle.js diff --git a/commands/other/google-doodle.js b/commands/other/google-doodle.js new file mode 100644 index 00000000..2c9463a2 --- /dev/null +++ b/commands/other/google-doodle.js @@ -0,0 +1,46 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); + +module.exports = class GoogleDoodleCommand extends Command { + constructor(client) { + super(client, { + name: 'google-doodle', + group: 'other', + memberName: 'google-doodle', + description: 'Responds with a Google doodle, either the latest or a random one from a specific month/year.', + clientPermissions: ['ATTACH_FILES'], + args: [ + { + key: 'month', + prompt: 'What month would you like to get doodles for?', + type: 'integer', + default: 'latest', + validate: month => { + if (month < 13 && month > 0) return true; + return 'Invalid month, please enter a number from 1-12.'; + } + }, + { + key: 'year', + prompt: 'What year would you like to get doodles for?', + type: 'integer', + default: '' + } + ] + }); + } + + async run(msg, { month, year }) { + const latest = month === 'latest'; + if (latest) month = new Date().getMonth() + 1; + if (!year) year = new Date().getFullYear(); + try { + const { body } = await snekfetch.get(`https://www.google.com/doodles/json/${year}/${month}`); + if (!body.length) return msg.say('Could not find any results.'); + const data = body[latest ? 0 : Math.floor(Math.random() * body.length)]; + return msg.say(data.share_text, { files: [`https:${data.url}`] }); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/package.json b/package.json index 4c9f2cd4..32f24fdd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "49.0.0", + "version": "49.1.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {