/// <inheritdoc />
        public sealed override async Task <PreconditionResult> CheckPermissionsAsync
        (
            [NotNull] ICommandContext context,
            [NotNull] CommandInfo command,
            [NotNull] IServiceProvider services
        )
        {
            var prioPreconditions = command.Preconditions.Where
                                    (
                a =>
                a is PrioritizedPreconditionAttribute
                                    ).Cast <PrioritizedPreconditionAttribute>();

            foreach (var prioConditionGroup in prioPreconditions.GroupBy(p => p.Group, StringComparer.Ordinal))
            {
                if (prioConditionGroup.Key is null)
                {
                    // Just check the permissions as normal
                    foreach (var prioPrecondition in prioConditionGroup.OrderBy(p => p.Priority))
                    {
                        var checkResult = await prioPrecondition.CheckPrioritizedPermissions
                                          (
                            context,
                            command,
                            services
                                          );

                        if (!checkResult.IsSuccess)
                        {
                            return(checkResult);
                        }
                    }
                }
                else
                {
                    var results = new List <PreconditionResult>();
                    foreach (var prioPrecondition in prioConditionGroup.OrderBy(p => p.Priority))
                    {
                        results.Add(await prioPrecondition.CheckPrioritizedPermissions(context, command, services));
                    }

                    if (!results.Any(p => p.IsSuccess))
                    {
                        return(PreconditionGroupResult.FromError
                               (
                                   $"Prioritized precondition group \"{prioConditionGroup.Key}\" failed.", results
                               ));
                    }
                }
            }

            return(PreconditionResult.FromSuccess());
        }
Ejemplo n.º 2
0
        public async Task <PreconditionResult> CheckPreconditionsAsync(string commandString, IServiceProvider services = null)
        {
            services = services ?? EmptyServiceProvider.Instance;

            async Task <PreconditionResult> CheckGroups(IEnumerable <PreconditionAttribute> preconditions, string type)
            {
                foreach (IGrouping <string, PreconditionAttribute> preconditionGroup in preconditions.GroupBy(p => p.Group, StringComparer.Ordinal))
                {
                    if (preconditionGroup.Key == null)
                    {
                        foreach (PreconditionAttribute precondition in preconditionGroup)
                        {
                            var result = await precondition.CheckPermissionsAsync(commandString, this, services).ConfigureAwait(false);

                            if (!result.IsSuccess)
                            {
                                return(result);
                            }
                        }
                    }
                    else
                    {
                        var results = new List <PreconditionResult>();
                        foreach (PreconditionAttribute precondition in preconditionGroup)
                        {
                            results.Add(await precondition.CheckPermissionsAsync(commandString, this, services).ConfigureAwait(false));
                        }

                        if (!results.Any(p => p.IsSuccess))
                        {
                            return(PreconditionGroupResult.FromError($"{type} precondition group {preconditionGroup.Key} failed.", results));
                        }
                    }
                }
                return(PreconditionGroupResult.FromSuccess());
            }

            var moduleResult = await CheckGroups(Module.Preconditions, "Module").ConfigureAwait(false);

            if (!moduleResult.IsSuccess)
            {
                return(moduleResult);
            }

            var commandResult = await CheckGroups(Preconditions, "Command").ConfigureAwait(false);

            if (!commandResult.IsSuccess)
            {
                return(commandResult);
            }

            return(PreconditionResult.FromSuccess());
        }