Cleaned up permission checks, added Permissions.Has(enum) methods
This commit is contained in:
@@ -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}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands.Attributes.Preconditions
|
||||
@@ -8,42 +6,44 @@ namespace Discord.Commands.Attributes.Preconditions
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||
public class RequirePermission : PreconditionAttribute
|
||||
{
|
||||
public GuildPermission? GuildPermission { get; set; }
|
||||
public ChannelPermission? ChannelPermission { get; set; }
|
||||
public GuildPermission? GuildPermission { get; }
|
||||
public ChannelPermission? ChannelPermission { get; }
|
||||
|
||||
public RequirePermission(GuildPermission permission)
|
||||
{
|
||||
GuildPermission = permission;
|
||||
ChannelPermission = null;
|
||||
}
|
||||
|
||||
public RequirePermission(ChannelPermission permission)
|
||||
{
|
||||
ChannelPermission = permission;
|
||||
GuildPermission = null;
|
||||
}
|
||||
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissions(IMessage context, Command executingCommand, object moduleInstance)
|
||||
{
|
||||
if (!(context.Channel is IGuildChannel))
|
||||
return Task.FromResult(PreconditionResult.FromError("Command must be used in a guild channel"));
|
||||
|
||||
var author = context.Author as IGuildUser;
|
||||
var guildUser = context.Author as IGuildUser;
|
||||
|
||||
if (GuildPermission.HasValue)
|
||||
{
|
||||
var guildPerms = author.GuildPermissions.ToList();
|
||||
if (!guildPerms.Contains(GuildPermission.Value))
|
||||
return Task.FromResult(PreconditionResult.FromError($"User is missing guild permission {GuildPermission.Value}"));
|
||||
if (guildUser == null)
|
||||
return Task.FromResult(PreconditionResult.FromError("Command must be used in a guild channel"));
|
||||
if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
|
||||
return Task.FromResult(PreconditionResult.FromError($"Command requires guild permission {GuildPermission.Value}"));
|
||||
}
|
||||
|
||||
if (ChannelPermission.HasValue)
|
||||
{
|
||||
var channel = context.Channel as IGuildChannel;
|
||||
var channelPerms = author.GetPermissions(channel).ToList();
|
||||
var guildChannel = context.Channel as IGuildChannel;
|
||||
|
||||
if (!channelPerms.Contains(ChannelPermission.Value))
|
||||
return Task.FromResult(PreconditionResult.FromError($"User is missing channel permission {ChannelPermission.Value}"));
|
||||
ChannelPermissions perms;
|
||||
if (guildChannel != null)
|
||||
perms = guildUser.GetPermissions(guildChannel);
|
||||
else
|
||||
perms = ChannelPermissions.All(guildChannel);
|
||||
|
||||
if (!perms.Has(ChannelPermission.Value))
|
||||
return Task.FromResult(PreconditionResult.FromError($"Command requires channel permission {ChannelPermission.Value}"));
|
||||
}
|
||||
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
Reference in New Issue
Block a user