@@ -5,8 +5,8 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
public abstract class PermissionAttribute : Attribute | |||||
public abstract class PreconditionAttribute : Attribute | |||||
{ | { | ||||
public abstract void CheckPermissions(PermissionsContext context); | |||||
public abstract void CheckPermissions(PreconditionContext context); | |||||
} | } | ||||
} | } |
@@ -0,0 +1,16 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Commands | |||||
{ | |||||
public class RequireDMAttribute : PreconditionAttribute | |||||
{ | |||||
public override void CheckPermissions(PreconditionContext context) | |||||
{ | |||||
if (context.Message.Channel is IGuildChannel) | |||||
context.Handled = true; | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,16 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Commands | |||||
{ | |||||
public class RequireGuildAttribute : PreconditionAttribute | |||||
{ | |||||
public override void CheckPermissions(PreconditionContext context) | |||||
{ | |||||
if (!(context.Message.Channel is IGuildChannel)) | |||||
context.Handled = true; | |||||
} | |||||
} | |||||
} |
@@ -19,7 +19,7 @@ namespace Discord.Commands | |||||
public string Text { get; } | public string Text { get; } | ||||
public Module Module { get; } | public Module Module { get; } | ||||
public IReadOnlyList<CommandParameter> Parameters { get; } | public IReadOnlyList<CommandParameter> Parameters { get; } | ||||
public IReadOnlyList<PermissionAttribute> Permissions { get; } | |||||
public IReadOnlyList<PreconditionAttribute> Permissions { get; } | |||||
internal Command(Module module, object instance, CommandAttribute attribute, MethodInfo methodInfo, string groupPrefix) | internal Command(Module module, object instance, CommandAttribute attribute, MethodInfo methodInfo, string groupPrefix) | ||||
{ | { | ||||
@@ -42,11 +42,11 @@ namespace Discord.Commands | |||||
_action = BuildAction(methodInfo); | _action = BuildAction(methodInfo); | ||||
} | } | ||||
public bool CanExecute(IMessage message) | |||||
public bool MeetsPreconditions(IMessage message) | |||||
{ | { | ||||
var context = new PermissionsContext(this, message); | |||||
var context = new PreconditionContext(this, message); | |||||
foreach (PermissionAttribute permission in Permissions) | |||||
foreach (PreconditionAttribute permission in Permissions) | |||||
{ | { | ||||
permission.CheckPermissions(context); | permission.CheckPermissions(context); | ||||
if (context.Handled) | if (context.Handled) | ||||
@@ -68,8 +68,8 @@ namespace Discord.Commands | |||||
if (!parseResult.IsSuccess) | if (!parseResult.IsSuccess) | ||||
return ExecuteResult.FromError(parseResult); | return ExecuteResult.FromError(parseResult); | ||||
if (!CanExecute(msg)) // TODO: should we have to check this here, or leave it entirely to the bot dev? | |||||
return ExecuteResult.FromError(CommandError.InvalidPermissions, "Permissions check failed"); | |||||
if (!MeetsPreconditions(msg)) // TODO: should we have to check this here, or leave it entirely to the bot dev? | |||||
return ExecuteResult.FromError(CommandError.UnmetPrecondition, "Permissions check failed"); | |||||
try | try | ||||
{ | { | ||||
@@ -82,9 +82,9 @@ namespace Discord.Commands | |||||
} | } | ||||
} | } | ||||
private IReadOnlyList<PermissionAttribute> BuildPermissions(MethodInfo methodInfo) | |||||
private IReadOnlyList<PreconditionAttribute> BuildPermissions(MethodInfo methodInfo) | |||||
{ | { | ||||
return methodInfo.GetCustomAttributes<PermissionAttribute>().ToImmutableArray(); | |||||
return methodInfo.GetCustomAttributes<PreconditionAttribute>().ToImmutableArray(); | |||||
} | } | ||||
private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo) | private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo) | ||||
@@ -16,6 +16,6 @@ | |||||
//Execute | //Execute | ||||
Exception, | Exception, | ||||
InvalidPermissions | |||||
UnmetPrecondition | |||||
} | } | ||||
} | } |
@@ -211,10 +211,10 @@ namespace Discord.Commands | |||||
// TODO: this logic is for users who don't manually search/execute: should we keep it? | // TODO: this logic is for users who don't manually search/execute: should we keep it? | ||||
IReadOnlyList<Command> commands = searchResult.Commands | IReadOnlyList<Command> commands = searchResult.Commands | ||||
.Where(x => x.CanExecute(message)).ToImmutableArray(); | |||||
.Where(x => x.MeetsPreconditions(message)).ToImmutableArray(); | |||||
if (commands.Count == 0 && searchResult.Commands.Count > 0) | if (commands.Count == 0 && searchResult.Commands.Count > 0) | ||||
return ParseResult.FromError(CommandError.InvalidPermissions, "Invalid permissions"); | |||||
return ParseResult.FromError(CommandError.UnmetPrecondition, "Unmet precondition"); | |||||
for (int i = commands.Count - 1; i >= 0; i--) | for (int i = commands.Count - 1; i >= 0; i--) | ||||
{ | { | ||||
@@ -5,16 +5,16 @@ using System.Threading.Tasks; | |||||
namespace Discord.Commands | namespace Discord.Commands | ||||
{ | { | ||||
public class PermissionsContext | |||||
public class PreconditionContext | |||||
{ | { | ||||
public Command ExecutingCommand { get; internal set; } | |||||
public Command Command { get; internal set; } | |||||
public IMessage Message { get; internal set; } | public IMessage Message { get; internal set; } | ||||
public bool Handled { get; set; } | public bool Handled { get; set; } | ||||
internal PermissionsContext(Command command, IMessage message) | |||||
internal PreconditionContext(Command command, IMessage message) | |||||
{ | { | ||||
ExecutingCommand = command; | |||||
Command = command; | |||||
Message = message; | Message = message; | ||||
Handled = false; | Handled = false; |