|
@@ -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<FilterAttribute> Filters { get; } |
|
|
|
|
|
|
|
|
public IReadOnlyList<PermissionAttribute> 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) |
|
|
{ |
|
|
{ |
|
@@ -38,10 +38,24 @@ namespace Discord.Commands |
|
|
Synopsis = synopsis.Text; |
|
|
Synopsis = synopsis.Text; |
|
|
|
|
|
|
|
|
Parameters = BuildParameters(methodInfo); |
|
|
Parameters = BuildParameters(methodInfo); |
|
|
Filters = BuildFilters(methodInfo); |
|
|
|
|
|
|
|
|
Permissions = BuildPermissions(methodInfo); |
|
|
_action = BuildAction(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) |
|
|
public async Task<ParseResult> Parse(IMessage msg, SearchResult searchResult) |
|
|
{ |
|
|
{ |
|
|
if (!searchResult.IsSuccess) |
|
|
if (!searchResult.IsSuccess) |
|
@@ -54,13 +68,8 @@ namespace Discord.Commands |
|
|
if (!parseResult.IsSuccess) |
|
|
if (!parseResult.IsSuccess) |
|
|
return ExecuteResult.FromError(parseResult); |
|
|
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 |
|
|
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) |
|
|
private IReadOnlyList<CommandParameter> BuildParameters(MethodInfo methodInfo) |
|
|