Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commands not finding valid executor #429

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions Obsidian/Commands/Framework/Entities/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Obsidian.Commands.Framework.Exceptions;
using Obsidian.Plugins;
using Obsidian.Utilities.Interfaces;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Obsidian.Commands.Framework.Entities;
Expand Down Expand Up @@ -79,14 +80,28 @@ public async Task ExecuteAsync(CommandContext context, string[] args)

return;
}

IExecutor<CommandContext> executor = default!;

var success = false;
foreach (var exec in executors)
if (!this.TryFindExecutor(executors, args, context, out var executor))
throw new InvalidOperationException($"Failed to find valid executor for /{this.Name}");

await this.ExecuteAsync(executor, context, args);
}

private bool TryFindExecutor(IEnumerable<IExecutor<CommandContext>> executors, string[] args, CommandContext context,
[NotNullWhen(true)] out IExecutor<CommandContext>? executor)
{
executor = null;

var success = args.Length == 0;

if (success)
{
executor = exec;
executor = executors.First();
return true;
}

foreach (var exec in executors)
{
var methodParams = exec.GetParameters();
for (int i = 0; i < args.Length; i++)
{
Expand All @@ -96,7 +111,7 @@ public async Task ExecuteAsync(CommandContext context, string[] args)
if (!CommandHandler.IsValidArgumentType(param.ParameterType))
{
success = false;
break;
continue;
}

var parser = CommandHandler.GetArgumentParser(param.ParameterType);
Expand All @@ -107,17 +122,16 @@ public async Task ExecuteAsync(CommandContext context, string[] args)
}

success = false;
break;
}

if (success)
break;
{
executor = exec;
return true;
}
}

if (!success)
throw new InvalidOperationException($"Failed to find valid executor for /{this.Name}");

await this.ExecuteAsync(executor, context, args);
return false;
}

private async Task ExecuteAsync(IExecutor<CommandContext> commandExecutor, CommandContext context, string[] args)
Expand Down
Loading