diff --git a/breadbot.js b/breadbot.js index 3fedd18..271c95c 100644 --- a/breadbot.js +++ b/breadbot.js @@ -3,22 +3,41 @@ const path = require('node:path'); const { Client, Events, GatewayIntentBits, Collection } = require('discord.js'); const { token } = require('./config.json'); +const getAllFiles = function(directoryPath, arrayOfFiles) { + const files = fs.readdirSync(directoryPath); + + arrayOfFiles = arrayOfFiles || []; + + files.forEach(file => { + if (fs.statSync(directoryPath + path.sep + file).isDirectory()) { + arrayOfFiles = getAllFiles(directoryPath + path.sep + file, arrayOfFiles); + } + else { + arrayOfFiles.push(path.join(__dirname, directoryPath, path.sep, file)); + } + }); + + return arrayOfFiles; +}; + +const allFiles = []; +getAllFiles('.' + path.sep + 'commands', allFiles); + const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.commands = new Collection(); -const commandsPath = path.join(__dirname, 'commands'); -const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); +const commandFiles = allFiles.filter(file => file.endsWith('.js')); for (const file of commandFiles) { - const filePath = path.join(commandsPath, file); - const command = require(filePath); + const command = require(file); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); + console.log(`[INFO] Loaded command at ${file}`); } else { - console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); + console.log(`[WARNING] The command at ${file} is missing a required "data" or "execute" property.`); } } diff --git a/commands/googlecalendar/addcalendar.js b/commands/googlecalendar/addcalendar.js new file mode 100644 index 0000000..f2baad9 --- /dev/null +++ b/commands/googlecalendar/addcalendar.js @@ -0,0 +1,45 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { google } = require('googleapis'); +const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../../config.json'); +const SCOPES = ['https://www.googleapis.com/auth/calendar']; + +module.exports = { + data: new SlashCommandBuilder() + .setName('addcalendar') + .setDescription('Creates a new Google Calendar') + .addStringOption(option => + option + .setName('name') + .setDescription('The new name for the calendar') + .setRequired(true)) + .addStringOption(option => + option + .setName('description') + .setDescription('The description of this new calendar')), + async execute(interaction) { + await interaction.deferReply({ ephemeral: true }); + + const name = interaction.options.getString('name'); + const description = interaction.options.getString('description'); + + const jwtClient = new google.auth.JWT( + googleClientEmail, + null, + googlePrivateKey, + SCOPES, + ); + + const calendar = new google.calendar({ + version: 'v3', + project: googleProjectNumber, + auth: jwtClient, + }); + + calendar.calendarList.insert({ + 'summary': name, + 'description': description, + }); + + await interaction.editReply('New Calendar ' + name + ' Created'); + }, +}; \ No newline at end of file diff --git a/deploy-commands.js b/deploy-commands.js index 940f1ea..0dac376 100644 --- a/deploy-commands.js +++ b/deploy-commands.js @@ -1,14 +1,37 @@ const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('./config.json'); const fs = require('node:fs'); +const path = require('node:path'); + +const getAllFiles = function(directoryPath, arrayOfFiles) { + const files = fs.readdirSync(directoryPath); + + arrayOfFiles = arrayOfFiles || []; + + files.forEach(file => { + if (fs.statSync(directoryPath + path.sep + file).isDirectory()) { + arrayOfFiles = getAllFiles(directoryPath + path.sep + file, arrayOfFiles); + } + else { + arrayOfFiles.push(path.join(__dirname, directoryPath, path.sep, file)); + } + }); + + return arrayOfFiles; +}; const commands = []; +const allFiles = []; +getAllFiles('.' + path.sep + 'commands', allFiles); +allFiles.forEach(file => { + console.log(file); +}); // Grab all the command files from the commands directory you created earlier -const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); +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(`./commands/${file}`); + const command = require(`${file}`); commands.push(command.data.toJSON()); }