From d6d8aa7e8a18833348884c1922806a2b9a1e72b0 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Wed, 17 Jun 2020 13:43:51 +0200 Subject: [PATCH] Refactor some of the code and naming conventions --- ...andPermissions.cs => CommandPermission.cs} | 12 ++--- .../Repository/CommandPermissionRepository.cs | 8 +-- .../Discord/PreConditions/CheckCommandPerm.cs | 54 +++++++++++-------- 3 files changed, 42 insertions(+), 32 deletions(-) rename ChaosBot/Database/Entity/{CommandPermissions.cs => CommandPermission.cs} (73%) diff --git a/ChaosBot/Database/Entity/CommandPermissions.cs b/ChaosBot/Database/Entity/CommandPermission.cs similarity index 73% rename from ChaosBot/Database/Entity/CommandPermissions.cs rename to ChaosBot/Database/Entity/CommandPermission.cs index 67ed04e..a85eb65 100644 --- a/ChaosBot/Database/Entity/CommandPermissions.cs +++ b/ChaosBot/Database/Entity/CommandPermission.cs @@ -5,7 +5,7 @@ using ChaosBot.Attribute; namespace ChaosBot.Database.Entity { [DBEntity("CommandPermissions")] - public class CommandPermissions : BaseEntity + public class CommandPermission : BaseEntity { [DBPrimaryKey] [DBAutoIncrement] @@ -17,9 +17,9 @@ namespace ChaosBot.Database.Entity public string cmd { get; private set; } public long guildId { get; private set; } - public CommandPermissions() {} + public CommandPermission() {} - public CommandPermissions(int id, string type, string value, string cmd, long guildId) + public CommandPermission(int id, string type, string value, string cmd, long guildId) { this.id = id; this.type = type; @@ -28,7 +28,7 @@ namespace ChaosBot.Database.Entity this.guildId = guildId; } - public CommandPermissions(string type, string value, string cmd, long guildId) + public CommandPermission(string type, string value, string cmd, long guildId) { this.type = type; this.value = value; @@ -36,9 +36,9 @@ namespace ChaosBot.Database.Entity this.guildId = guildId; } - public static QueryBuilder Query() + public static QueryBuilder Query() { - return BaseEntity.Query(); + return BaseEntity.Query(); } public override void SetFromRow(DataRow row) diff --git a/ChaosBot/Database/Repository/CommandPermissionRepository.cs b/ChaosBot/Database/Repository/CommandPermissionRepository.cs index 2dd1cea..59f68e9 100644 --- a/ChaosBot/Database/Repository/CommandPermissionRepository.cs +++ b/ChaosBot/Database/Repository/CommandPermissionRepository.cs @@ -16,9 +16,9 @@ namespace ChaosBot.Database.Repository /// /// /// List of Commands and Permissions - public static CommandPermissions[] All(long guildId) + public static CommandPermission[] All(long guildId) { - var cmds = CommandPermissions.Query().Where("guildId", guildId).All(); + var cmds = CommandPermission.Query().Where("guildId", guildId).All(); return cmds.ToArray(); } @@ -29,9 +29,9 @@ namespace ChaosBot.Database.Repository /// /// /// List of raffles - public static CommandPermissions[] getPerm(string cmd, long guildId) + public static CommandPermission[] getPerm(string cmd, long guildId) { - List cmds = CommandPermissions.Query().Where("cmd", cmd).Where("guildId", guildId).Get(); + List cmds = CommandPermission.Query().Where("cmd", cmd).Where("guildId", guildId).Get(); if(cmds.Count != 0) return cmds.ToArray(); diff --git a/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs b/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs index 5503263..061c72b 100644 --- a/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs +++ b/ChaosBot/Discord/PreConditions/CheckCommandPerm.cs @@ -11,36 +11,46 @@ namespace ChaosBot.Discord.PreConditions { public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { - string requiredGroup = null; + // Debug information + Program._logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}"); + + // If user is not SocketGuildUser, then return error + if (!(context.User is SocketGuildUser gUser)) return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command.")); + + // Get the possible permissions + var commandPermissions = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id)); - if (context.User is SocketGuildUser gUser) + // If we can find a permission + if(commandPermissions != null) { - var results = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id)); - - Program._logger.Info(command.Name); - - if(results != null) + // Loop through all permissions + foreach (var perm in commandPermissions) { - foreach (var perm in results) + string requiredGroup; + // Check if it's a role or group permission and fetch the right type + if (perm.type.ToLower() == "role") { - if (perm.type == "role") - requiredGroup = ConfigurationRepository.GetValue($"Role:{perm.value}", context.Guild.Id) ?? perm.value; - else - requiredGroup = perm.value; - - if (gUser.Roles.Any(r => r.Name == requiredGroup)) - return Task.FromResult(PreconditionResult.FromSuccess()); + // If it's a role, check the configuration for the role otherwise return the permission value + requiredGroup = ConfigurationRepository.GetValue($"Role:{perm.value}", context.Guild.Id) ?? perm.value; + } + else + { + // Return the permission value + requiredGroup = perm.value; } - } - else - { - Program._logger.Info($"Value: Null Value"); - } - return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command.")); + // Check if any of the users roles are of the required group, if so, permission granted + if (gUser.Roles.Any(r => r.Name == requiredGroup)) + return Task.FromResult(PreconditionResult.FromSuccess()); + } } else - return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command.")); + { + Program._logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value"); + } + + // Permission denied + return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command.")); } } } \ No newline at end of file