コード例 #1
0
        /// <inheritdoc cref="ICommandInterceptor"/>
        public Task <CommandHandlingResult> InterceptAsync(ICommandInterceptionContext context)
        {
            if (_customLoggingActionsMap.TryGetValue(context.Command.GetType(), out var customLoggingAction))
            {
                customLoggingAction?.Invoke(_defaultLogger, context.HandlerObject, context.Command);
            }
            else
            {
                _defaultLogger.Log(context.HandlerObject, context.Command);
            }

            return(context.InvokeNextAsync());
        }
コード例 #2
0
        public async Task <CommandResult?> Process(string commandName, IImmutableList <string> args, Message message)
        {
            if (!_commands.TryGetValue(commandName.ToLower(), out Command command))
            {
                _logger.LogDebug("unknown command '{Command}'", commandName);
                return(null);
            }
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            CommandResult result;

            try
            {
                result = await command.Execution(new CommandContext(message, args, _argsParser));

                await _commandLogger.Log(message.User.Id, commandName, args, result.Response);
            }
            catch (ArgsParseFailure ex)
            {
                result = new CommandResult {
                    Response = ex.Message
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex,
                                 "An exception occured while executing command '{Command}'. User: {User}, Original text: {MessageText}",
                                 command.Name, message.User, message.MessageText);
                result = new CommandResult {
                    Response = "An error occurred."
                };
            }
            stopwatch.Stop();
            if (stopwatch.Elapsed >= CommandWarnTimeLimit)
            {
                _logger.LogWarning(
                    "Command '{Command}' took unusually long ({Duration}ms) to finish! " +
                    "User: {User:l}, Original text: {MessageText}",
                    command.Name, stopwatch.ElapsedMilliseconds, message.User, message.MessageText);
            }
            return(result);
        }
コード例 #3
0
        /// <inheritdoc cref="ICommandInterceptor"/>
        public Task <CommandHandlingResult> InterceptAsync(ICommandInterceptionContext context)
        {
            _commandLogger.Log(context.HandlerObject, context.Command);

            return(context.InvokeNextAsync());
        }