From 7ebdb84c98c4484d3131ed49c8a30269903b4c06 Mon Sep 17 00:00:00 2001 From: Bradley Bickford Date: Fri, 25 Nov 2022 19:41:08 -0500 Subject: [PATCH] Some mild changes, added list calendar --- commands/googlecalendar/addcalendar.js | 2 +- commands/googlecalendar/listcalendar.js | 48 +++++++++++++++++++++++++ deploy-commands.js | 7 +++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/commands/googlecalendar/addcalendar.js b/commands/googlecalendar/addcalendar.js index a39ec3f..e6f0db0 100644 --- a/commands/googlecalendar/addcalendar.js +++ b/commands/googlecalendar/addcalendar.js @@ -43,7 +43,7 @@ module.exports = { // eslint-disable-next-line no-unused-vars async (err, res) => { if (err) { - await interaction.editReply('Failed to create calendar ' + name + '\nAsk an Admin to check Breadbot console'); + await interaction.editReply('Failed to create calendar ' + name + '\nAsk Bradley to check Breadbot console'); stdout.write('[ERROR]: '); console.log(err.errors); return; diff --git a/commands/googlecalendar/listcalendar.js b/commands/googlecalendar/listcalendar.js index e69de29..8f616d2 100644 --- a/commands/googlecalendar/listcalendar.js +++ b/commands/googlecalendar/listcalendar.js @@ -0,0 +1,48 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const { google } = require('googleapis'); +const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../../config.json'); +const { stdout } = require('node:process'); +const SCOPES = ['https://www.googleapis.com/auth/calendar']; + +module.exports = { + data: new SlashCommandBuilder() + .setName('listcalendars') + .setDescription('Lists the currently available calendars'), + async execute(interaction) { + await interaction.deferReply({ ephemeral: true }); + + const jwtClient = new google.auth.JWT( + googleClientEmail, + './keyfile.json', + googlePrivateKey, + SCOPES, + ); + + const calendar = new google.calendar({ + version: 'v3', + project: googleProjectNumber, + auth: jwtClient, + }); + + calendar.calendarList.list({}, 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'); + + 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 ] }); + }); + }, +}; \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js index 0dac376..ed0d7b5 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -32,7 +32,12 @@ const commandFiles = allFiles.filter(file => file.endsWith('.js')); // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment for (const file of commandFiles) { const command = require(`${file}`); - commands.push(command.data.toJSON()); + if ('data' in command && 'execute' in command) { + commands.push(command.data.toJSON()); + } + else { + console.log(`Skipping ${file} as it is missing one or more required exported fields`); + } } // Construct and prepare an instance of the REST module