Ejemplo n.º 1
0
        public async Task <IActionResult> SlashCommandHook(SlashCommand slashCommand)
        {
            if (slashCommand == null)
            {
                _logger.LogError("Null slash command");
                return(Ok(ErrorStrings.General(), ResponseType.User));
            }

            if (!_messageVerifier.IsValid(slashCommand))
            {
                _logger.LogError("Bad verification token");
                return(Ok(ErrorStrings.General(), ResponseType.User));
            }

            _logger.LogInformation("Received slash command {Text} {@Command}", slashCommand.Text, slashCommand);

            try
            {
                var result = await _commandRunner.RunAsync(slashCommand.Text, new Dictionary <string, object>
                {
                    { Constants.SlashCommand, slashCommand }
                }).ConfigureAwait(false);

                Response.ContentType = "application/json";
                return(new CommandRouterResult(result));
            }
            catch (CommandNotFoundException)
            {
                return(Ok(ErrorStrings.CommandUnknown(slashCommand.Text), ResponseType.User));
            }
            catch (RunStartException e)
            {
                switch (e.Reason)
                {
                case RunStartException.RunStartExceptionReason.ExistingActiveRun:
                    return(Ok(ErrorStrings.StartRun_RunAlreadyStarted(), ResponseType.User));

                case RunStartException.RunStartExceptionReason.Unspecified:
                default:
                    break;
                }

                _logger.LogError(e, "Failed to start run");
            }
            catch (RunEndException e)
            {
                switch (e.Reason)
                {
                case RunEndException.RunEndExceptionReason.NoActiveRun:
                    return(Ok(ErrorStrings.EndRun_RunNotStarted(), ResponseType.User));

                case RunEndException.RunEndExceptionReason.NoOrders:
                    return(Ok(ErrorStrings.EndRun_NoOrders(), ResponseType.User));

                case RunEndException.RunEndExceptionReason.NotJoined:
                    return(Ok(ErrorStrings.EndRun_NotJoined(), ResponseType.User));

                case RunEndException.RunEndExceptionReason.Unspecified:
                default:
                    break;
                }

                _logger.LogError(e, "Failed to end run");
            }
            catch (PermissionException e)
            {
                return(Ok(e.Message, ResponseType.User));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to run command");
            }

            return(Ok(ErrorStrings.CommandFailed(), ResponseType.User));
        }