mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-05 13:53:12 +02:00
Allow usage of a personal event calendar alongside the holidays one
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "81.0.4",
|
||||
"version": "81.0.5",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user