Adding some modifications to increase reuseability
This commit is contained in:
parent
7ebdb84c98
commit
24b97bc5b0
@ -1,8 +1,5 @@
|
|||||||
const { SlashCommandBuilder } = require('discord.js');
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
const { google } = require('googleapis');
|
const { addCalendar } = require('../../utilities/googlecalendar.js');
|
||||||
const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../../config.json');
|
|
||||||
const { stdout } = require('node:process');
|
|
||||||
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@ -22,34 +19,13 @@ module.exports = {
|
|||||||
|
|
||||||
const name = interaction.options.getString('name');
|
const name = interaction.options.getString('name');
|
||||||
|
|
||||||
const jwtClient = new google.auth.JWT(
|
|
||||||
googleClientEmail,
|
|
||||||
'./keyfile.json',
|
|
||||||
googlePrivateKey,
|
|
||||||
SCOPES,
|
|
||||||
);
|
|
||||||
|
|
||||||
const calendar = new google.calendar({
|
|
||||||
version: 'v3',
|
|
||||||
project: googleProjectNumber,
|
|
||||||
auth: jwtClient,
|
|
||||||
});
|
|
||||||
|
|
||||||
calendar.calendars.insert({
|
|
||||||
resource: {
|
|
||||||
summary: name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
async (err, res) => {
|
addCalendar(name, async (success, message, extra) => {
|
||||||
if (err) {
|
const embedResponse = new EmbedBuilder()
|
||||||
await interaction.editReply('Failed to create calendar ' + name + '\nAsk Bradley to check Breadbot console');
|
.setColor(success ? 0x00FF00 : 0xFF0000)
|
||||||
stdout.write('[ERROR]: ');
|
.setTitle(message);
|
||||||
console.log(err.errors);
|
|
||||||
return;
|
await interaction.editReply({ embeds: [ embedResponse ] });
|
||||||
}
|
});
|
||||||
await interaction.editReply('New Calendar ' + name + ' Created');
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
@ -0,0 +1,27 @@
|
|||||||
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
|
const { deleteCalendar } = require('../../utilities/googlecalendar.js');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
data: new SlashCommandBuilder()
|
||||||
|
.setName('deletecalendar')
|
||||||
|
.setDescription('Permanently deletes a calendar and it\'s associated events')
|
||||||
|
.addStringOption(option =>
|
||||||
|
option
|
||||||
|
.setName('name')
|
||||||
|
.setDescription('The name of the calendar you want to delete')
|
||||||
|
.setRequired(true)),
|
||||||
|
async execute(interaction) {
|
||||||
|
await interaction.deferReply({ ephemeral: true });
|
||||||
|
|
||||||
|
const name = interaction.options.getString('name');
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
deleteCalendar(name, async (success, message, extra) => {
|
||||||
|
const embedResponse = new EmbedBuilder()
|
||||||
|
.setColor(success ? 0x0FF00 : 0xFF0000)
|
||||||
|
.setTitle(message);
|
||||||
|
|
||||||
|
await interaction.editReply({ embeds: [ embedResponse ] });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
@ -1,8 +1,6 @@
|
|||||||
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
||||||
const { google } = require('googleapis');
|
const { getListOfCalendars } = require('../../utilities/googlecalendar');
|
||||||
const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../../config.json');
|
|
||||||
const { stdout } = require('node:process');
|
const { stdout } = require('node:process');
|
||||||
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
data: new SlashCommandBuilder()
|
data: new SlashCommandBuilder()
|
||||||
@ -11,20 +9,7 @@ module.exports = {
|
|||||||
async execute(interaction) {
|
async execute(interaction) {
|
||||||
await interaction.deferReply({ ephemeral: true });
|
await interaction.deferReply({ ephemeral: true });
|
||||||
|
|
||||||
const jwtClient = new google.auth.JWT(
|
getListOfCalendars({}, async (err, res) => {
|
||||||
googleClientEmail,
|
|
||||||
'./keyfile.json',
|
|
||||||
googlePrivateKey,
|
|
||||||
SCOPES,
|
|
||||||
);
|
|
||||||
|
|
||||||
const calendar = new google.calendar({
|
|
||||||
version: 'v3',
|
|
||||||
project: googleProjectNumber,
|
|
||||||
auth: jwtClient,
|
|
||||||
});
|
|
||||||
|
|
||||||
calendar.calendarList.list({}, async (err, res) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
const errorEmbed = new EmbedBuilder()
|
const errorEmbed = new EmbedBuilder()
|
||||||
.setColor(0xFF0000)
|
.setColor(0xFF0000)
|
||||||
|
99
utilities/googlecalendar.js
Normal file
99
utilities/googlecalendar.js
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
const { google } = require('googleapis');
|
||||||
|
const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../config.json');
|
||||||
|
const { stdout } = require('node:process');
|
||||||
|
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
|
||||||
|
|
||||||
|
async function getCalendarReference() {
|
||||||
|
const jwtClient = new google.auth.JWT(
|
||||||
|
googleClientEmail,
|
||||||
|
'../keyfile.json',
|
||||||
|
googlePrivateKey,
|
||||||
|
SCOPES,
|
||||||
|
);
|
||||||
|
|
||||||
|
return new google.calendar({
|
||||||
|
version: 'v3',
|
||||||
|
project: googleProjectNumber,
|
||||||
|
auth: jwtClient,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function doesCalendarExist(calendarName) {
|
||||||
|
const calendarReference = await getCalendarReference();
|
||||||
|
const listResults = await calendarReference.calendarList.list({});
|
||||||
|
console.log(listResults);
|
||||||
|
console.log(listResults.data.items);
|
||||||
|
|
||||||
|
for (const item of listResults.data.items) {
|
||||||
|
console.log(item);
|
||||||
|
console.log('[DEBUG]: Calendar Item Summary: ' + item.summary);
|
||||||
|
if (item.summary === calendarName) {
|
||||||
|
console.log('[DEBUG]: The previous item is causing doesCalendarExist to return true');
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addCalendar(calendarName, callback) {
|
||||||
|
const calendarReference = await getCalendarReference();
|
||||||
|
calendarReference.calendars.insert({
|
||||||
|
resource: {
|
||||||
|
summary: calendarName,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
async (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
callback(false, 'Failed to create new calendar ' + calendarName + '\nAsk Bradley to check Breadbat console', err);
|
||||||
|
stdout.write('[ERROR]: ');
|
||||||
|
console.log(err.errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(true, 'Successfully created new calendar ' + calendarName, null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteCalendar(calendarName, callback) {
|
||||||
|
const exists = await doesCalendarExist(calendarName);
|
||||||
|
|
||||||
|
if (exists) {
|
||||||
|
const calendarReference = await getCalendarReference();
|
||||||
|
calendarReference.calendars.delete({
|
||||||
|
resource: {
|
||||||
|
calendarId: exists.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
async (err, res) => {
|
||||||
|
if (err) {
|
||||||
|
callback(false, 'Failed to delete ' + calendarName + '\nAsk Bradley to check Breadbot console', err);
|
||||||
|
stdout.write('[ERROR]: ');
|
||||||
|
console.log(err.errors);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(true, 'Successfully deleted ' + calendarName, null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callback(false, 'The calendar name specified doesn\'t exist', null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getCalendarReference,
|
||||||
|
getListOfCalendars,
|
||||||
|
doesCalendarExist,
|
||||||
|
deleteCalendar,
|
||||||
|
addCalendar,
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user