From a24a616459e0379aa996b7733e2f920c93e8029f Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Tue, 23 Jan 2024 13:59:11 -0800 Subject: [PATCH] clean my code + fix permissions error? --- commands/checkStatus.js | 2 +- commands/firearmSearch.js | 2 +- commands/nameSearch.js | 2 +- commands/panic.js | 2 +- commands/plateSearch.js | 2 +- commands/updateLicense.js | 2 +- commands/updateStatus.js | 2 +- structures/LinesPoliceCadBot.js | 29 +++++++++++------------------ 8 files changed, 18 insertions(+), 25 deletions(-) diff --git a/commands/checkStatus.js b/commands/checkStatus.js index 56b2666..cd0d8ec 100644 --- a/commands/checkStatus.js +++ b/commands/checkStatus.js @@ -30,7 +30,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/commands/firearmSearch.js b/commands/firearmSearch.js index d3053f9..0e6c11d 100644 --- a/commands/firearmSearch.js +++ b/commands/firearmSearch.js @@ -31,7 +31,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/commands/nameSearch.js b/commands/nameSearch.js index ccfa069..3f47b63 100644 --- a/commands/nameSearch.js +++ b/commands/nameSearch.js @@ -45,7 +45,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/commands/panic.js b/commands/panic.js index 4ab2cd9..f368eb6 100644 --- a/commands/panic.js +++ b/commands/panic.js @@ -22,7 +22,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.`, flags: (1 << 6) }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command", flags: (1 << 6) }); let user = await client.dbo.collection("users").findOne({"user.discord.id": interaction.member.user.id}).then(user => user); diff --git a/commands/plateSearch.js b/commands/plateSearch.js index c24a17d..d662f9a 100644 --- a/commands/plateSearch.js +++ b/commands/plateSearch.js @@ -31,7 +31,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/commands/updateLicense.js b/commands/updateLicense.js index 8835592..b06cb2c 100644 --- a/commands/updateLicense.js +++ b/commands/updateLicense.js @@ -45,7 +45,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/commands/updateStatus.js b/commands/updateStatus.js index 2c1dc6c..5c86c90 100644 --- a/commands/updateStatus.js +++ b/commands/updateStatus.js @@ -37,7 +37,7 @@ module.exports = { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } - let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles, true); + let useCommand = await client.verifyUseCommand(GuildDB.serverID, interaction.member.roles); if (!useCommand) return interaction.send({ content: "You don't have permission to use this command" }); let user = await client.dbo.collection("users").findOne({"user.discord.id":interaction.member.user.id}).then(user => user); diff --git a/structures/LinesPoliceCadBot.js b/structures/LinesPoliceCadBot.js index 6f43ea0..c7bf973 100644 --- a/structures/LinesPoliceCadBot.js +++ b/structures/LinesPoliceCadBot.js @@ -216,25 +216,16 @@ class LinesPoliceCadBot extends Client { }); } - async checkRoleStatus(rolesCache, serverID, isList) { - let hasRole = false; - let guild = await this.dbo.collection("prefixes").findOne({"server.serverID": serverID}).then(guild => guild); + async checkRoleStatus(rolesCache, serverID) { + let guild = await this.dbo.collection("prefixes").findOne({ "server.serverID": serverID }).then(guild => guild); + + if (guild.server.allowedRoles.length == 0) return true; // If we are in this function, and there are no roles to compare to, return true as if we had the role anyway - // If user has one of any in the list of allowed roles, hasRole is true for (let i = 0; i < guild.server.allowedRoles.length; i++) { - if (!isList) { - if (rolesCache.some(role => role.id == guild.server.allowedRoles[i])) { - hasRole = true - break - } - } else { - if (rolesCache.includes(guild.server.allowedRoles[i])) { - hasRole = true - break - } - } + if (rolesCache.some(role => role.id == guild.server.allowedRoles[i])) return true; // they have at least one matching role } - return hasRole; + + return false; // they have no matching roles. } async GetGuild(GuildId) { @@ -266,7 +257,7 @@ class LinesPoliceCadBot extends Client { command ie. has cop role to use name-search */ - async verifyUseCommand(serverID, rolesCache, isList) { + async verifyUseCommand(serverID, rolesCache) { let { customRoleStatus } = await this.GetGuild(serverID) if (!customRoleStatus) return false; // There is no role limits @@ -281,7 +272,9 @@ class LinesPoliceCadBot extends Client { if (!role) update = true; } - if (update) { + // If some roles no longer exists, update + // or if customRoleStatus is true but no roles exist, update + if (update || (filteredRoles == 0 && customRoleStatus)) { let newHasCustomRoles = filteredRoles > 0; await this.dbo.collection("prefixes").updateOne({ 'server.serverID': serverID }, { $set: {