Rename and move things about
This commit is contained in:
@@ -5,8 +5,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public abstract class FilterAttribute : Attribute
|
||||
public abstract class PermissionAttribute : Attribute
|
||||
{
|
||||
public abstract void OnCommandExecuting(CommandExecutionContext context);
|
||||
public abstract void CheckPermissions(PermissionsContext context);
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ namespace Discord.Commands
|
||||
public string Text { get; }
|
||||
public Module Module { get; }
|
||||
public IReadOnlyList<CommandParameter> Parameters { get; }
|
||||
public IReadOnlyList<FilterAttribute> Filters { get; }
|
||||
public IReadOnlyList<PermissionAttribute> Permissions { get; }
|
||||
|
||||
internal Command(Module module, object instance, CommandAttribute attribute, MethodInfo methodInfo, string groupPrefix)
|
||||
{
|
||||
@@ -38,10 +38,24 @@ namespace Discord.Commands
|
||||
Synopsis = synopsis.Text;
|
||||
|
||||
Parameters = BuildParameters(methodInfo);
|
||||
Filters = BuildFilters(methodInfo);
|
||||
Permissions = BuildPermissions(methodInfo);
|
||||
_action = BuildAction(methodInfo);
|
||||
}
|
||||
|
||||
public bool CanExecute(IMessage message)
|
||||
{
|
||||
var context = new PermissionsContext(this, message);
|
||||
|
||||
foreach (PermissionAttribute permission in Permissions)
|
||||
{
|
||||
permission.CheckPermissions(context);
|
||||
if (context.Handled)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<ParseResult> Parse(IMessage msg, SearchResult searchResult)
|
||||
{
|
||||
if (!searchResult.IsSuccess)
|
||||
@@ -54,13 +68,8 @@ namespace Discord.Commands
|
||||
if (!parseResult.IsSuccess)
|
||||
return ExecuteResult.FromError(parseResult);
|
||||
|
||||
var context = new CommandExecutionContext(this, parseResult, msg);
|
||||
foreach (FilterAttribute filter in Filters)
|
||||
{
|
||||
filter.OnCommandExecuting(context);
|
||||
if (context.Handled)
|
||||
return ExecuteResult.FromError(CommandError.InvalidPermissions, $"Permission check for {filter.GetType().FullName} failed");
|
||||
}
|
||||
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");
|
||||
|
||||
try
|
||||
{
|
||||
@@ -73,9 +82,9 @@ namespace Discord.Commands
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<FilterAttribute> BuildFilters(MethodInfo methodInfo)
|
||||
private IReadOnlyList<PermissionAttribute> BuildPermissions(MethodInfo methodInfo)
|
||||
{
|
||||
return methodInfo.GetCustomAttributes<FilterAttribute>().ToImmutableArray();
|
||||
return methodInfo.GetCustomAttributes<PermissionAttribute>().ToImmutableArray();
|
||||
}
|
||||
|
||||
private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo)
|
||||
|
||||
@@ -5,18 +5,16 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public class CommandExecutionContext
|
||||
public class PermissionsContext
|
||||
{
|
||||
public Command ExecutingCommand { get; internal set; }
|
||||
public ParseResult ParseResult { get; internal set; }
|
||||
public IMessage Message { get; internal set; }
|
||||
|
||||
public bool Handled { get; set; }
|
||||
|
||||
internal CommandExecutionContext(Command command, ParseResult parseResult, IMessage message)
|
||||
internal PermissionsContext(Command command, IMessage message)
|
||||
{
|
||||
ExecutingCommand = command;
|
||||
ParseResult = parseResult;
|
||||
Message = message;
|
||||
|
||||
Handled = false;
|
||||
Reference in New Issue
Block a user