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 2 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
35 changes: 25 additions & 10 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,10 +80,26 @@ public async Task ExecuteAsync(CommandContext context, string[] args)

return;
}

IExecutor<CommandContext> executor = default!;

var success = false;
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 = executors.First();
return true;
}

foreach (var exec in executors)
{
executor = exec;
Expand All @@ -96,7 +113,8 @@ public async Task ExecuteAsync(CommandContext context, string[] args)
if (!CommandHandler.IsValidArgumentType(param.ParameterType))
{
success = false;
break;
executor = null;
return false;
}

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

success = false;
break;
return false;
}

if (success)
break;
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