diff --git a/commands/events/holidays.js b/commands/events/holidays.js index 818b6f22..6f663418 100644 --- a/commands/events/holidays.js +++ b/commands/events/holidays.js @@ -1,13 +1,14 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); -const { today, tomorrow } = require('../../util/Util'); -const { GOOGLE_KEY, GOOGLE_CALENDAR_ID } = process.env; +const { stripIndents } = require('common-tags'); +const { list, today, tomorrow } = require('../../util/Util'); +const { GOOGLE_KEY, GOOGLE_CALENDAR_ID, PERSONAL_GOOGLE_CALENDAR_ID } = process.env; module.exports = class HolidaysCommand extends Command { constructor(client) { super(client, { name: 'holidays', - aliases: ['google-calendar', 'holiday'], + aliases: ['google-calendar', 'holiday', 'calendar'], group: 'events', memberName: 'holidays', description: 'Responds with today\'s holidays.' @@ -15,21 +16,39 @@ module.exports = class HolidaysCommand extends Command { } async run(msg) { + try { + const events = []; + const standardEvents = await this.fetchHolidays(GOOGLE_CALENDAR_ID); + if (standardEvents) events.push(...standardEvents); + const personalEvents = await this.fetchHolidays(PERSONAL_GOOGLE_CALENDAR_ID); + if (personalEvents) events.push(...personalEvents); + if (!events.length) return msg.say('There are no holidays today...'); + return msg.say(stripIndents` + There are **${events.length}** holidays today: + ${list(events)}. + `); + } catch (err) { + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async fetchHolidays(id) { try { const { body } = await request - .get(`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(GOOGLE_CALENDAR_ID)}/events`) + .get(`https://www.googleapis.com/calendar/v3/calendars/${encodeURIComponent(id)}/events`) .query({ - maxResults: 10, + maxResults: 20, orderBy: 'startTime', singleEvents: true, timeMax: tomorrow().toISOString(), timeMin: today().toISOString(), key: GOOGLE_KEY }); - if (!body.items.length) return msg.say('There are no holidays today...'); - return msg.say(body.items.map(holiday => holiday.summary).join('\n')); + if (!body.items.length) return null; + return body.items.map(holiday => holiday.summary); } catch (err) { - return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + if (err.status === 404) return null; + throw err; } } }; diff --git a/package.json b/package.json index f0c5aa0f..d747db60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "81.0.4", + "version": "81.0.5", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {