Skip to content

Commit

Permalink
Fix commands not finding valid executor (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Mar 6, 2024
1 parent 4ce9755 commit 31544a3
Showing 1 changed file with 26 additions and 12 deletions.
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

0 comments on commit 31544a3

Please sign in to comment.