From aabc1cda5129d5fb3daae398401b7daaab5b5fef Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Sun, 18 Mar 2018 14:37:33 +0000 Subject: [PATCH] Neko Atsume Password Command --- commands/events/holidays.js | 20 ++------ commands/events/neko-atsume-password.js | 68 +++++++++++++++++++++++++ package.json | 2 +- util/Util.js | 16 ++++++ 4 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 commands/events/neko-atsume-password.js diff --git a/commands/events/holidays.js b/commands/events/holidays.js index 944a09a2..9d3e292b 100644 --- a/commands/events/holidays.js +++ b/commands/events/holidays.js @@ -1,5 +1,6 @@ const { Command } = require('discord.js-commando'); const snekfetch = require('snekfetch'); +const { today, tomorrow } = require('../../util/Util'); const { GOOGLE_KEY, GOOGLE_CALENDAR_ID } = process.env; module.exports = class HolidaysCommand extends Command { @@ -21,8 +22,8 @@ module.exports = class HolidaysCommand extends Command { maxResults: 10, orderBy: 'startTime', singleEvents: true, - timeMax: this.tomorrow().toISOString(), - timeMin: this.today().toISOString(), + timeMax: tomorrow().toISOString(), + timeMin: today().toISOString(), key: GOOGLE_KEY }); if (!body.items.length) return msg.say('There are no holidays today...'); @@ -31,19 +32,4 @@ module.exports = class HolidaysCommand extends Command { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } - - today() { - const now = new Date(); - now.setHours(0); - now.setMinutes(0); - now.setSeconds(0); - now.setMilliseconds(0); - return now; - } - - tomorrow() { - const today = this.today(); - today.setDate(today.getDate() + 1); - return today; - } }; diff --git a/commands/events/neko-atsume-password.js b/commands/events/neko-atsume-password.js new file mode 100644 index 00000000..d24c9f5f --- /dev/null +++ b/commands/events/neko-atsume-password.js @@ -0,0 +1,68 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { stripIndents } = require('common-tags'); +const { list, duration, tomorrow } = require('../../util/Util'); +const { GOLD_FISH_EMOJI_ID, SILVER_FISH_EMOJI_ID } = process.env; +const locales = ['en', 'jp']; + +module.exports = class NekoAtsumePasswordCommand extends Command { + constructor(client) { + super(client, { + name: 'neko-atsume-password', + group: 'events', + memberName: 'neko-atsume-password', + description: 'Responds with today\'s Neko Atsume password.', + args: [ + { + key: 'locale', + prompt: `What locale do you want to use? Either ${list(locales, 'or')}.`, + type: 'string', + default: 'en', + validate: locale => { + if (locales.includes(locale.toLowerCase())) return true; + return `Invalid locale, please enter either ${list(locales, 'or')}.`; + }, + parse: locale => locale.toLowerCase() + } + ] + }); + } + + async run(msg, { locale }) { + try { + const data = await this.fetchPassword(locale); + return msg.say(stripIndents` + The current Neko Atsume password is **${data.password}**. + It will expire in **${duration(data.expires - data.date)}**. + + ${data.gold} ${this.goldFish} + ${data.silver} ${this.silverFish} + `); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async fetchPassword(locale) { + const { text } = await snekfetch + .get(`http://hpmobile.jp/app/nekoatsume/neko_daily${locale !== 'jp' ? `_${locale}` : ''}.php`); + const data = text.split(','); + const date = new Date(); + date.setUTCHours(date.getUTCHours() + 9); + return { + password: data[1], + silver: data[2], + gold: data[3], + date, + expires: tomorrow(9) + }; + } + + get goldFish() { + return GOLD_FISH_EMOJI_ID ? `` : 'Gold Fish'; + } + + get silverFish() { + return SILVER_FISH_EMOJI_ID ? `` : 'Silver Fish'; + } +}; diff --git a/package.json b/package.json index 0ae3c88c..1b7275e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "69.1.0", + "version": "69.2.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index bbd9a41d..bb9d0b42 100644 --- a/util/Util.js +++ b/util/Util.js @@ -72,6 +72,22 @@ class Util { return body.data.images[Math.floor(Math.random() * body.data.images.length)].link; } + today(timeZone) { + const now = new Date(); + if (timeZone) now.setUTCHours(now.getUTCHours() + timeZone); + now.setHours(0); + now.setMinutes(0); + now.setSeconds(0); + now.setMilliseconds(0); + return now; + } + + tomorrow(timeZone) { + const today = Util.today(timeZone); + today.setDate(today.getDate() + 1); + return today; + } + static cleanXML(text) { return text .replace(/
/g, '')