From e68272236f250cee456a30cad95a05fe80aa1813 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Sat, 11 Nov 2023 12:29:37 -0500 Subject: [PATCH] Pushing up changes to main before I branch --- README.md | 1 + calendarpulltest.js | 2 +- commands/googlecalendar/addcalendar.js | 12 ++++++-- commands/googlecalendar/listcalendar.js | 25 ++++------------ datetesting.js | 35 +++++++++++++++++++++++ package-lock.json | 11 +++++++ package.json | 1 + utilities/googlecalendar.js | 38 ++++++++++++++++++++++--- 8 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 datetesting.js diff --git a/README.md b/README.md index 3d15d48..6877fdc 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ I have a subset of features that I want it to have before I call it "production "Production Ready" Features - [ ] Google Calendar Integration for Event Management - [ ] Create/Manage/Remove Events and Calendars + - [ ] Add Autocomplete for common elements like Calendar Names, Event Names, Timezones, etc. - [ ] Calendar Announcements for Upcoming Events - [ ] Poll Creation and Results Announcements - [ ] Conversation Archiving (May be Removed) diff --git a/calendarpulltest.js b/calendarpulltest.js index 9d0b3c4..b9cc2cc 100644 --- a/calendarpulltest.js +++ b/calendarpulltest.js @@ -23,7 +23,7 @@ async function main() { console.log(err.errors); return; } - console.log(res.data.items.map(x => x.summary)); + console.log(res.data.items.map(x => x.summary + '---' + x.timeZone)); }); } diff --git a/commands/googlecalendar/addcalendar.js b/commands/googlecalendar/addcalendar.js index e3b70db..b4458a7 100644 --- a/commands/googlecalendar/addcalendar.js +++ b/commands/googlecalendar/addcalendar.js @@ -1,5 +1,6 @@ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { addCalendar } = require('../../utilities/googlecalendar.js'); +// const { getTimeZones } = require('@vvo/tzdb'); module.exports = { data: new SlashCommandBuilder() @@ -12,15 +13,20 @@ module.exports = { .setRequired(true)) .addStringOption(option => option - .setName('description') - .setDescription('The description of this new calendar')), + .setName('timezone') + .setDescription('The Time Zone of this new calendar, must be in IANA format') + .setRequired(true)), + // .addChoices(getTimeZones().map(tz => { + // return { name: tz.name, value: tz.name }; + // }))), async execute(interaction) { await interaction.deferReply({ ephemeral: true }); const name = interaction.options.getString('name'); + const timezone = interaction.options.getString('timezone'); // eslint-disable-next-line no-unused-vars - addCalendar(name, async (success, message, extra) => { + addCalendar(name, timezone, async (success, message, extra) => { const embedResponse = new EmbedBuilder() .setColor(success ? 0x00FF00 : 0xFF0000) .setTitle(message); diff --git a/commands/googlecalendar/listcalendar.js b/commands/googlecalendar/listcalendar.js index c5d37fe..b005e9c 100644 --- a/commands/googlecalendar/listcalendar.js +++ b/commands/googlecalendar/listcalendar.js @@ -1,6 +1,5 @@ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { getListOfCalendars } = require('../../utilities/googlecalendar'); -const { stdout } = require('node:process'); module.exports = { data: new SlashCommandBuilder() @@ -9,25 +8,13 @@ module.exports = { async execute(interaction) { await interaction.deferReply({ ephemeral: true }); - getListOfCalendars({}, async (err, res) => { - if (err) { - const errorEmbed = new EmbedBuilder() - .setColor(0xFF0000) - .setTitle('Failed to get a list of calendars') - .setDescription('Ask Bradley to check Breadbot console'); + getListOfCalendars({}, async (success, message, extra) => { + const embedResponse = new EmbedBuilder() + .setColor(success ? 0x00FF00 : 0xFF0000) + .setTitle(message) + .setDescription(extra.map(x => x.summary + ' --- ' + x.timeZone).join('\n\n')); - await interaction.editReply({ embeds: errorEmbed }); - stdout.write('[ERROR]: '); - console.log(err.errors); - return; - } - - const successEmbed = new EmbedBuilder() - .setColor(0x00FF00) - .setTitle('Calendar List') - .setDescription(res.data.items.map((x) => x.summary).join('\n')); - - await interaction.editReply({ embeds: [ successEmbed ] }); + await interaction.editReply({ embeds: [ embedResponse ] }); }); }, }; \ No newline at end of file diff --git a/datetesting.js b/datetesting.js new file mode 100644 index 0000000..5b754ef --- /dev/null +++ b/datetesting.js @@ -0,0 +1,35 @@ +const readline = require('readline'); + +const r1 = readline.createInterface({ + input: process.stdin, + output: process.stdout, + terminal: false, +}); + +let date1 = null; +let date2 = null; +let result = null; + +r1.on('line', line => { + if (line.startsWith('date1')) { + date1 = new Date(line.split(':')[1]); + } + else if (line.startsWith('date2')) { + date2 = new Date(line.split(':')[1]); + } + else if (line.startsWith('result')) { + if (date1 !== null && date2 !== null) { + result = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate(), + date2.getHours(), date2.getMinutes(), date2.getSeconds()); + + console.log(result); + } + } + else { + console.log('Bad command'); + } +}); + +r1.once('close', () => { + console.log('Closing'); +}); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1cef511..d197ecb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@vvo/tzdb": "^6.77.0", "discord.js": "^14.6.0", "googleapis": "^109.0.1" }, @@ -206,6 +207,11 @@ "@types/node": "*" } }, + "node_modules/@vvo/tzdb": { + "version": "6.77.0", + "resolved": "https://registry.npmjs.org/@vvo/tzdb/-/tzdb-6.77.0.tgz", + "integrity": "sha512-t7aN3GAznzt8fQ5enJiM3C7HKPEDBoqKExp2W7nYu2AgS0J9FfMk6rwWhL2jjTe0+27REmO9C+TL3XM2evileQ==" + }, "node_modules/acorn": { "version": "8.8.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.1.tgz", @@ -1975,6 +1981,11 @@ "@types/node": "*" } }, + "@vvo/tzdb": { + "version": "6.77.0", + "resolved": "https://registry.npmjs.org/@vvo/tzdb/-/tzdb-6.77.0.tgz", + "integrity": "sha512-t7aN3GAznzt8fQ5enJiM3C7HKPEDBoqKExp2W7nYu2AgS0J9FfMk6rwWhL2jjTe0+27REmO9C+TL3XM2evileQ==" + }, "acorn": { "version": "8.8.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.1.tgz", diff --git a/package.json b/package.json index 9379d10..5fc120d 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Bradley Bickford", "license": "ISC", "dependencies": { + "@vvo/tzdb": "^6.77.0", "discord.js": "^14.6.0", "googleapis": "^109.0.1" }, diff --git a/utilities/googlecalendar.js b/utilities/googlecalendar.js index 8440abd..3ff32e1 100644 --- a/utilities/googlecalendar.js +++ b/utilities/googlecalendar.js @@ -31,18 +31,26 @@ async function doesCalendarExist(calendarName) { return null; } -// TODO This needs to be changed so that it uses the common callback -// format that I've created async function getListOfCalendars(options, callback) { const calendarReference = await getCalendarReference(); - calendarReference.calendarList.list(options, callback); + calendarReference.calendarList.list(options, async (err, res) => { + if (err) { + callback(false, 'Failed to retrieve the list of calendars\nAsk Bradley to check Breadbot console'); + stdout.write('[ERROR]:'); + console.log(err.errors); + return; + } + + callback(true, 'Calendar List', res.data.items); + }); } -async function addCalendar(calendarName, callback) { +async function addCalendar(calendarName, timezone, callback) { const calendarReference = await getCalendarReference(); calendarReference.calendars.insert({ resource: { summary: calendarName, + timeZone: timezone, }, }, // eslint-disable-next-line no-unused-vars @@ -83,6 +91,28 @@ async function deleteCalendar(calendarName, callback) { } } +async function addEvent(calendarName, eventName, location, description, startDate, startTime, endDate, endTime) { + const exists = await doesCalendarExist(calendarName); + + if (exists) { + const calendarReference = await getCalendarReference(); + calendarReference.events.insert({ + calendarId: exists.id, + resource: { + summary: eventName, + location: location, + description: description, + start: { + + } + }, + }) + } + else { + callback(false, 'The calendar name specified doesn\'t exist', null); + } +} + module.exports = { getCalendarReference, getListOfCalendars,