Refactor some of the code and naming conventions

This commit is contained in:
Daniel_I_Am 2020-06-17 13:43:51 +02:00
parent 3b76d06619
commit d6d8aa7e8a
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 42 additions and 32 deletions

View File

@ -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<CommandPermissions> Query()
public static QueryBuilder<CommandPermission> Query()
{
return BaseEntity.Query<CommandPermissions>();
return BaseEntity.Query<CommandPermission>();
}
public override void SetFromRow(DataRow row)

View File

@ -16,9 +16,9 @@ namespace ChaosBot.Database.Repository
/// </summary>
/// <param name="guildId"></param>
/// <returns>List of Commands and Permissions</returns>
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
/// <param name="cmd"></param>
/// <param name="guildId"></param>
/// <returns>List of raffles</returns>
public static CommandPermissions[] getPerm(string cmd, long guildId)
public static CommandPermission[] getPerm(string cmd, long guildId)
{
List<CommandPermissions> cmds = CommandPermissions.Query().Where("cmd", cmd).Where("guildId", guildId).Get();
List<CommandPermission> cmds = CommandPermission.Query().Where("cmd", cmd).Where("guildId", guildId).Get();
if(cmds.Count != 0) return cmds.ToArray();

View File

@ -11,36 +11,46 @@ namespace ChaosBot.Discord.PreConditions
{
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
string requiredGroup = null;
// Debug information
Program._logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
if (context.User is SocketGuildUser gUser)
{
var results = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id));
// 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."));
Program._logger.Info(command.Name);
// Get the possible permissions
var commandPermissions = CommandPermissionRepository.getPerm(command.Name, Convert.ToInt64(context.Guild.Id));
if(results != null)
// If we can find a permission
if(commandPermissions != null)
{
foreach (var perm in results)
// Loop through all permissions
foreach (var perm in commandPermissions)
{
if (perm.type == "role")
string requiredGroup;
// Check if it's a role or group permission and fetch the right type
if (perm.type.ToLower() == "role")
{
// If it's a role, check the configuration for the role otherwise return the permission value
requiredGroup = ConfigurationRepository.GetValue<string>($"Role:{perm.value}", context.Guild.Id) ?? perm.value;
}
else
{
// Return the permission value
requiredGroup = perm.value;
}
// 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
{
Program._logger.Info($"Value: Null Value");
Program._logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value");
}
// Permission denied
return Task.FromResult(PreconditionResult.FromError($"You do not have access to this command."));
}
else
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
}
}
}