Cleaned up permission checks, added Permissions.Has(enum) methods

This commit is contained in:
RogueException
2016-08-09 18:18:50 -03:00
parent e452aa9662
commit dcb603acd7
4 changed files with 40 additions and 35 deletions

View File

@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Discord.Commands
@@ -8,35 +6,36 @@ namespace Discord.Commands
[Flags]
public enum ContextType
{
Guild = 1, // 01
DM = 2 // 10
Guild = 0x01,
DM = 0x02,
Group = 0x04
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class RequireContextAttribute : PreconditionAttribute
{
public ContextType Context { get; set; }
public ContextType Contexts { get; }
public RequireContextAttribute(ContextType context)
public RequireContextAttribute(ContextType contexts)
{
Context = context;
Contexts = contexts;
}
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
{
var validContext = false;
bool isValid = false;
if (Context.HasFlag(ContextType.Guild))
validContext = validContext || context.Channel is IGuildChannel;
if ((Contexts & ContextType.Guild) != 0)
isValid = isValid || context.Channel is IGuildChannel;
if ((Contexts & ContextType.DM) != 0)
isValid = isValid || context.Channel is IDMChannel;
if ((Contexts & ContextType.Group) != 0)
isValid = isValid || context.Channel is IGroupChannel;
if (Context.HasFlag(ContextType.DM))
validContext = validContext || context.Channel is IDMChannel;
if (validContext)
if (isValid)
return Task.FromResult(PreconditionResult.FromSuccess());
else
return Task.FromResult(PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Context}"));
return Task.FromResult(PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Contexts}"));
}
}
}