Ejemplo n.º 1
0
        /// <summary>
        /// Handles a message
        /// </summary>
        /// <param name="receivedMessage"></param>
        /// <returns></returns>
        public async Task HandleMessageAsync(ApiWrapper.ReceivedMessage receivedMessage)
        {
            try
            {
                var configContext = new Config.IConfigurationDependency[] { receivedMessage.Channel.ParentServer, receivedMessage.Channel };

                // Iterate through all commands and check if one of them gets triggered
                var commandExecuted = false;
                foreach (var command in Commands)
                {
                    foreach (var handler in Handlers.Where(x => ConfigStore.IsCommandActive(command, BotFramework.Settings.CommandsEnabledByDefault, configContext) &&
                                                           command.GetType().GetInterfaces().Contains(x.CommandType) &&
                                                           receivedMessage.IsBotMentioned))
                    {
                        // If command should not execute, ignore command and continue to next
                        var shouldExecute = handler.ShouldExecute(receivedMessage, command);
                        if (!shouldExecute.shouldExecute)
                        {
                            continue;
                        }

                        commandExecuted = await handler.ExecuteCommandAsync(receivedMessage, command, shouldExecute.parameter).ConfigureAwait(false);

                        if (commandExecuted)
                        {
                            break;
                        }
                    }

                    if (commandExecuted)
                    {
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                BotFramework.LoggerFactory.CreateLogger(GetType().Name).LogError($"Error processing message {receivedMessage.Content}: {ex}");
                await receivedMessage.Channel.SendMessageAsync($"Oh no! Something caused me to crash: {ex.Message}");
            }
        }