This repository was archived by the owner on May 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommandHandler.cs
More file actions
95 lines (78 loc) · 3.28 KB
/
CommandHandler.cs
File metadata and controls
95 lines (78 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using FluentResults;
using Kysect.BotFramework.Core.BotMessages;
using Kysect.BotFramework.Core.Commands;
using Kysect.BotFramework.DefaultCommands;
using Microsoft.Extensions.DependencyInjection;
namespace Kysect.BotFramework.Core.CommandInvoking
{
public class CommandHandler
{
private readonly CommandHolder _commands = new CommandHolder();
private readonly ServiceProvider _serviceProvider;
public CommandHandler(ServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public Result CheckArgsCount(CommandContainer args)
{
Result<BotCommandDescriptor> commandTask = _commands.GetCommand(args.CommandName);
if (commandTask.IsFailed)
{
return commandTask.ToResult<CommandContainer>();
}
return (commandTask.Value.Args.Count == args.Arguments.Count) || (args.CommandName == "Poll") // Better way to check for Poll?
? Result.Ok()
: Result.Fail<CommandContainer>(
"Cannot execute command. Argument count miss matched with command signature");
}
public Result CanCommandBeExecuted(CommandContainer args)
{
Result<BotCommandDescriptor> commandTask = _commands.GetCommand(args.CommandName);
if (commandTask.IsFailed)
{
return commandTask.ToResult<CommandContainer>();
}
IBotCommand command = commandTask.Value.ResolveCommand(_serviceProvider);
Result canExecute = command.CanExecute(args);
return canExecute.IsSuccess
? Result.Ok()
: Result.Fail<CommandContainer>(
$"Command [{commandTask.Value.CommandName}] cannot be executed: {canExecute}");
}
public CommandHandler SetCaseSensitive(bool caseSensitive)
{
_commands.SetCaseSensitive(caseSensitive);
return this;
}
public void RegisterCommand(BotCommandDescriptor descriptor)
{
_commands.AddCommand(descriptor);
}
public Result<IBotMessage> ExecuteCommand(CommandContainer args)
{
Result<BotCommandDescriptor> commandDescriptor = _commands.GetCommand(args.CommandName);
if (!commandDescriptor.IsSuccess)
{
return commandDescriptor.ToResult<IBotMessage>();
}
try
{
IBotCommand command = commandDescriptor.Value.ResolveCommand(_serviceProvider);
return command switch
{
IBotAsyncCommand asyncCommand => asyncCommand.Execute(args).Result,
IBotSyncCommand syncCommand => syncCommand.Execute(args),
_ => Result.Fail(new Error("Command execution failed. Wrong command inheritance."))
};
}
catch (Exception e)
{
string errorMessage =
$"Command execution failed. Command: {args.CommandName}; arguments: {string.Join(", ", args.Arguments)}";
return Result.Fail(new Error(errorMessage).CausedBy(e));
}
}
public CommandHolder GetCommands() => _commands;
}
}