From 6b96f048fdf4366d79ebbf0ccfcec65bdf3fb2b7 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 25 Feb 2023 03:25:54 -0500 Subject: [PATCH] Mutiple Changes - Added events loader and directory --- deploy-commands.js | 11 ---------- events/interactionCreate.js | 28 ++++++++++++++++++++++++ index.js | 43 ++++++++++++++----------------------- libCore.js | 2 +- 4 files changed, 45 insertions(+), 39 deletions(-) delete mode 100644 deploy-commands.js create mode 100644 events/interactionCreate.js diff --git a/deploy-commands.js b/deploy-commands.js deleted file mode 100644 index 87c08d8..0000000 --- a/deploy-commands.js +++ /dev/null @@ -1,11 +0,0 @@ -const { REST } = require('@discordjs/rest'); -const { Routes } = require('discord-api-types/v9'); -//const { clientId, guildId, token } = require('./config.json'); - -const commands = []; - -const rest = new REST({ version: '9' }).setToken(token); - -rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands }) - .then(() => console.log('Successfully registered application commands.')) - .catch(console.error); \ No newline at end of file diff --git a/events/interactionCreate.js b/events/interactionCreate.js new file mode 100644 index 0000000..13132c3 --- /dev/null +++ b/events/interactionCreate.js @@ -0,0 +1,28 @@ +const { Events } = require('discord.js'); + +module.exports = { + name: Events.InteractionCreate, + async execute(interaction) { + if (!interaction.isChatInputCommand()) return; + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + console.error(`No command matching ${interaction.commandName} was found.`); + return; + } + + log.DEBUG(`${interaction.member.user} is running '${interaction.commandName}'`); + + try { + await command.execute(interaction); + } catch (error) { + console.error(error); + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); + } else { + await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + } + } + }, +}; \ No newline at end of file diff --git a/index.js b/index.js index 312b2fe..cf35bfe 100644 --- a/index.js +++ b/index.js @@ -91,7 +91,7 @@ function runHTTPServer() { server.on('error', libUtils.onError); server.on('listening', () => { - log.INFO("Local HTTP Server Running"); + log.INFO("HTTP server started!"); try { libCore.feedArray = libCore.getFeeds(); } catch (error) { @@ -115,43 +115,32 @@ for (const file of commandFiles) { client.commands.set(command.data.name, command); } +// Run when the bot is ready client.on('ready', () => { log.DEBUG(`Discord server up and running with client: ${client.user.tag}`); + log.INFO(`Logged in as ${client.user.tag}!`); - // Deploy commands - log.DEBUG("Deploying commands"); + // Deploy slash commands + log.DEBUG("Deploying slash commands"); deployCommands.deploy(client.guilds.cache.map(guild => guild.id)); log.DEBUG(`Starting HTTP Server`); runHTTPServer(); - - log.INFO(`Logged in as ${client.user.tag}!`); - log.INFO("HTTP server started!"); }); -client.on(Events.InteractionCreate, async interaction => { - if (!interaction.isChatInputCommand()) return; +// Setup any additional event handlers +const eventsPath = path.join(__dirname, 'events'); +const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js')); - const command = interaction.client.commands.get(interaction.commandName); - - if (!command) { - console.error(`No command matching ${interaction.commandName} was found.`); - return; +for (const file of eventFiles) { + const filePath = path.join(eventsPath, file); + const event = require(filePath); + if (event.once) { + client.once(event.name, (...args) => event.execute(...args)); + } else { + client.on(event.name, (...args) => event.execute(...args)); } - - log.DEBUG(`${interaction.member.user} is running '${command}'`); - - try { - await command.execute(interaction); - } catch (error) { - console.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); - } else { - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); - } - } -}); +} client.login(discordToken); //Load Client Discord Token try { diff --git a/libCore.js b/libCore.js index 5622552..b7b8ece 100644 --- a/libCore.js +++ b/libCore.js @@ -103,7 +103,7 @@ exports.deleteSource = function (title, callback) { */ exports.getFeeds = function (feedType) { var linkFlayerFilteredMap = []; - if (feedType == null || feedType == undefined || feedType == "") { + if (feedType == null || feedType == undefined || feedType == "" || feedType.toLowerCase() == "all") { return rssFeedMap; } else {