Refactor some of the code and naming conventions
This commit is contained in:
parent
3b76d06619
commit
d6d8aa7e8a
@ -5,7 +5,7 @@ using ChaosBot.Attribute;
|
|||||||
namespace ChaosBot.Database.Entity
|
namespace ChaosBot.Database.Entity
|
||||||
{
|
{
|
||||||
[DBEntity("CommandPermissions")]
|
[DBEntity("CommandPermissions")]
|
||||||
public class CommandPermissions : BaseEntity
|
public class CommandPermission : BaseEntity
|
||||||
{
|
{
|
||||||
[DBPrimaryKey]
|
[DBPrimaryKey]
|
||||||
[DBAutoIncrement]
|
[DBAutoIncrement]
|
||||||
@ -17,9 +17,9 @@ namespace ChaosBot.Database.Entity
|
|||||||
public string cmd { get; private set; }
|
public string cmd { get; private set; }
|
||||||
public long guildId { 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.id = id;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@ -28,7 +28,7 @@ namespace ChaosBot.Database.Entity
|
|||||||
this.guildId = guildId;
|
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.type = type;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@ -36,9 +36,9 @@ namespace ChaosBot.Database.Entity
|
|||||||
this.guildId = guildId;
|
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)
|
public override void SetFromRow(DataRow row)
|
||||||
@ -16,9 +16,9 @@ namespace ChaosBot.Database.Repository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="guildId"></param>
|
/// <param name="guildId"></param>
|
||||||
/// <returns>List of Commands and Permissions</returns>
|
/// <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();
|
return cmds.ToArray();
|
||||||
}
|
}
|
||||||
@ -29,9 +29,9 @@ namespace ChaosBot.Database.Repository
|
|||||||
/// <param name="cmd"></param>
|
/// <param name="cmd"></param>
|
||||||
/// <param name="guildId"></param>
|
/// <param name="guildId"></param>
|
||||||
/// <returns>List of raffles</returns>
|
/// <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();
|
if(cmds.Count != 0) return cmds.ToArray();
|
||||||
|
|
||||||
|
|||||||
@ -11,36 +11,46 @@ namespace ChaosBot.Discord.PreConditions
|
|||||||
{
|
{
|
||||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
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 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));
|
// Loop through all permissions
|
||||||
|
foreach (var perm in commandPermissions)
|
||||||
Program._logger.Info(command.Name);
|
|
||||||
|
|
||||||
if(results != null)
|
|
||||||
{
|
{
|
||||||
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")
|
// 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;
|
requiredGroup = ConfigurationRepository.GetValue<string>($"Role:{perm.value}", context.Guild.Id) ?? perm.value;
|
||||||
else
|
}
|
||||||
requiredGroup = perm.value;
|
else
|
||||||
|
{
|
||||||
if (gUser.Roles.Any(r => r.Name == requiredGroup))
|
// Return the permission value
|
||||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
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
|
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."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user