mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-26 22:32:52 +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 Command = require('../../structures/Command');
|
||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const { today, tomorrow } = require('../../util/Util');
|
const { stripIndents } = require('common-tags');
|
||||||
const { GOOGLE_KEY, GOOGLE_CALENDAR_ID } = process.env;
|
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 {
|
module.exports = class HolidaysCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client, {
|
super(client, {
|
||||||
name: 'holidays',
|
name: 'holidays',
|
||||||
aliases: ['google-calendar', 'holiday'],
|
aliases: ['google-calendar', 'holiday', 'calendar'],
|
||||||
group: 'events',
|
group: 'events',
|
||||||
memberName: 'holidays',
|
memberName: 'holidays',
|
||||||
description: 'Responds with today\'s holidays.'
|
description: 'Responds with today\'s holidays.'
|
||||||
@@ -15,21 +16,39 @@ module.exports = class HolidaysCommand extends Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async run(msg) {
|
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 {
|
try {
|
||||||
const { body } = await request
|
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({
|
.query({
|
||||||
maxResults: 10,
|
maxResults: 20,
|
||||||
orderBy: 'startTime',
|
orderBy: 'startTime',
|
||||||
singleEvents: true,
|
singleEvents: true,
|
||||||
timeMax: tomorrow().toISOString(),
|
timeMax: tomorrow().toISOString(),
|
||||||
timeMin: today().toISOString(),
|
timeMin: today().toISOString(),
|
||||||
key: GOOGLE_KEY
|
key: GOOGLE_KEY
|
||||||
});
|
});
|
||||||
if (!body.items.length) return msg.say('There are no holidays today...');
|
if (!body.items.length) return null;
|
||||||
return msg.say(body.items.map(holiday => holiday.summary).join('\n'));
|
return body.items.map(holiday => holiday.summary);
|
||||||
} catch (err) {
|
} 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",
|
"name": "xiao",
|
||||||
"version": "81.0.4",
|
"version": "81.0.5",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user