From 0aa7d3edc05c40007ed88aeb0f600e48cd89459b Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 25 Feb 2023 04:10:40 -0500 Subject: [PATCH] Improved Discord Auth System - Used on all commands - Moved to middleware dir --- middleware/discordAuthorization.js | 23 +++++++++++++++++++++++ utilities/discordAuthorization.js | 16 ---------------- 2 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 middleware/discordAuthorization.js delete mode 100644 utilities/discordAuthorization.js diff --git a/middleware/discordAuthorization.js b/middleware/discordAuthorization.js new file mode 100644 index 0000000..cb18522 --- /dev/null +++ b/middleware/discordAuthorization.js @@ -0,0 +1,23 @@ +// To authorize a message sender for admin level commands +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "discordAuthorization"); + +const botAdmins = process.env.BOT_ADMINS; + +exports.authorizeCommand = async (interaction, command, next) => { + log.DEBUG("Command is privileged? ", command.isPrivileged) + + // If the command is not privileged, run the command + if (!command.isPrivileged) return next(true); + + log.DEBUG(`${interaction.member.user} is attempting to run the privileged command '${command}'`); + + // Check to see if the user has the role specified in the config + if (!interaction.member.roles.cache.has(`${botAdmins}`)) { + log.DEBUG(`Unauthorized - ${interaction.member.user} does not have the privilege to run '${command}'`); + return await interaction.reply({ content: `Sorry ${interaction.member.user}, you are not permitted to run that command`, ephemeral: true }); + } else { + log.DEBUG(`Authorized - ${interaction.member.user} can run '${command}'`); + return next(true); + } +} \ No newline at end of file diff --git a/utilities/discordAuthorization.js b/utilities/discordAuthorization.js deleted file mode 100644 index 2e6c035..0000000 --- a/utilities/discordAuthorization.js +++ /dev/null @@ -1,16 +0,0 @@ -// To authorize a message sender for admin level commands - -const { DebugBuilder } = require("./debugBuilder"); -const log = new DebugBuilder("server", "discordAuthorization"); - -const botAdmins = process.env.BOT_ADMINS; - -exports.authorizeCommand = async (interaction, command, next) => { - log.DEBUG(`${interaction.member.user} is attempting to run the privileged command '${command}'`); - if (!interaction.member.roles.cache.has(`${botAdmins}`)) { - log.DEBUG(`Unauthorized - ${interaction.member.user} does not have the privilege to run '${command}'`); - return await interaction.reply(`Sorry ${interaction.member.tag}, you are not permitted to run that command`); - } - log.DEBUG(`Authorized - ${interaction.member.user} can run '${command}'`); - next(true); -} \ No newline at end of file