From 3d5c83ad632eea6ae496175140452fae6e0be4f0 Mon Sep 17 00:00:00 2001 From: Taken Date: Sat, 18 Nov 2023 19:05:26 +0100 Subject: [PATCH] Added interaction event handler --- events/interactions/logBtnsCmds.js | 21 +++++++++++++++++++++ index.js | 18 ++---------------- utils/eventHandler.js | 4 ++-- utils/eventHandlers/interaction.js | 23 +++++++++++++++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 events/interactions/logBtnsCmds.js create mode 100644 utils/eventHandlers/interaction.js diff --git a/events/interactions/logBtnsCmds.js b/events/interactions/logBtnsCmds.js new file mode 100644 index 0000000..fa946d0 --- /dev/null +++ b/events/interactions/logBtnsCmds.js @@ -0,0 +1,21 @@ +module.exports = { + name: "logBtnsCmds", + description: "Logs all button and command interactions", + type: "interaction", + + /** @param { import('discord.js').ChatInputCommandInteraction } interaction */ + + execute(interaction) { + if (interaction.isCommand()) { + console.log(interaction.user.username + "#" + + interaction.user.discriminator + " ran " + + interaction.commandName + ); + } else if (interaction.isButton()) { + console.log(interaction.user.username + "#" + + interaction.user.discriminator + " clicked " + + interaction.customId + ); + } + } +} diff --git a/index.js b/index.js index 1a9b596..e8958bb 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const { Client, GatewayIntentBits, Partials, ActivityType, Events, Collection } = require('discord.js'); -const { botLogChannel, color } = require('./config/options.json'); -const { loadSlashCommands, loadMessageEvents, loadContextMenu, loadModalEvents, loadButtonEvents, loadReadyEvents } = require('./utils/eventHandler.js') +const { loadSlashCommands, loadMessageEvents, loadContextMenu, loadModalEvents, loadButtonEvents, loadReadyEvents, loadInteractionEvents } = require('./utils/eventHandler.js') require('dotenv').config(); const mongoURI = process.env.MONGOURI; const { connect } = require('mongoose'); @@ -39,20 +38,7 @@ loadButtonEvents(client); loadModalEvents(client); loadMessageEvents(client); loadReadyEvents(client) - -client.on(Events.InteractionCreate, async interaction => { - if (interaction.isCommand()) { - console.log(interaction.user.username + "#" + - interaction.user.discriminator + " ran " + - interaction.commandName - ); - } else if (interaction.isButton()) { - console.log(interaction.user.username + "#" + - interaction.user.discriminator + " clicked " + - interaction.customId - ); - } -}); +loadInteractionEvents(client); client.login(token); diff --git a/utils/eventHandler.js b/utils/eventHandler.js index 66ff9f0..990ebc7 100644 --- a/utils/eventHandler.js +++ b/utils/eventHandler.js @@ -4,6 +4,6 @@ const { loadContextMenu } = require('./eventHandlers/contextmenu.js') const { loadMessageEvents } = require('./eventHandlers/message.js') const { loadModalEvents } = require('./eventHandlers/modal.js') const { loadReadyEvents } = require('./eventHandlers/ready.js') +const { loadInteractionEvents } = require('./eventHandlers/interaction.js') - -module.exports = { loadSlashCommands, loadButtonEvents, loadContextMenu, loadMessageEvents, loadModalEvents, loadReadyEvents } +module.exports = { loadSlashCommands, loadButtonEvents, loadContextMenu, loadMessageEvents, loadModalEvents, loadReadyEvents, loadInteractionEvents } diff --git a/utils/eventHandlers/interaction.js b/utils/eventHandlers/interaction.js new file mode 100644 index 0000000..1af8b5b --- /dev/null +++ b/utils/eventHandlers/interaction.js @@ -0,0 +1,23 @@ +const { Events } = require('discord.js') +const path = require('path'); +const fs = require('fs'); + +/** @param { import('discord.js').Client } client */ + +function loadInteractionEvents(client) { + const interactionsPath = path.join(__dirname, '..', '..', 'events', 'interactions') + const interactionsFiles = fs.readdirSync(interactionsPath).filter(file => file.endsWith('.js')); + + for (const file of interactionsFiles) { + const filePath = path.join(interactionsPath, file); + const interactionFile = require(filePath); + + if ('name' in interactionFile && 'execute' in interactionFile && interactionFile.type === 'interaction') { + client.on(Events.InteractionCreate, interactionFile.execute) + } else { + console.log(`[WARNING] The interactions event at ${filePath} is missing a required "name", "execute" or "type" property.`); + } + } +} + +module.exports = { loadInteractionEvents }