public DetermineConditionResult IsEntityNameValid
        (
            [NotNull] ModuleInfo commandModule,
            [CanBeNull] string entityName
        )
        {
            if (entityName.IsNullOrWhitespace())
            {
                return(DetermineConditionResult.FromError(CommandError.ObjectNotFound, "Names cannot be empty."));
            }

            if (entityName.Any(c => this.ReservedNameCharacters.Contains(c)))
            {
                return(DetermineConditionResult.FromError
                       (
                           CommandError.UnmetPrecondition,
                           $"Names may not contain any of the following characters: {this.ReservedNameCharacters.Humanize()}"
                       ));
            }

            if (this.ReservedNames.Any(n => string.Equals(n, entityName, StringComparison.OrdinalIgnoreCase)))
            {
                return(DetermineConditionResult.FromError
                       (
                           CommandError.UnmetPrecondition,
                           "That is a reserved name."
                       ));
            }

            var submodules = commandModule.Submodules;

            var commandNames = commandModule.Commands.SelectMany(c => c.Aliases);

            commandNames = commandNames.Union(commandModule.Commands.Select(c => c.Name));

            var submoduleCommandNames = submodules.SelectMany(s => s.Commands.SelectMany(c => c.Aliases));

            submoduleCommandNames = submoduleCommandNames.Union(submodules.SelectMany(s => s.Commands.Select(c => c.Name)));

            commandNames = commandNames.Union(submoduleCommandNames);

            if (commandNames.Any(entityName.Contains))
            {
                return(DetermineConditionResult.FromError(CommandError.UnmetPrecondition, "Names may not be the same as a command."));
            }

            return(DetermineConditionResult.FromSuccess());
        }
예제 #2
0
        public async Task <DetermineConditionResult> CanUserTransformUserAsync
        (
            [NotNull] GlobalInfoContext db,
            [NotNull] IGuild discordServer,
            [NotNull] IUser invokingUser,
            [NotNull] IUser targetUser
        )
        {
            var localProtection = await GetOrCreateServerUserProtectionAsync(db, targetUser, discordServer);

            if (!localProtection.HasOptedIn)
            {
                return(DetermineConditionResult.FromError("The target hasn't opted into transformations."));
            }

            var globalProtection = await GetOrCreateGlobalUserProtectionAsync(db, targetUser);

            switch (localProtection.Type)
            {
            case ProtectionType.Blacklist:
            {
                return(globalProtection.Blacklist.All(u => u.DiscordID != (long)invokingUser.Id)
                                                ? DetermineConditionResult.FromSuccess()
                                                : DetermineConditionResult.FromError("You're on that user's blacklist."));
            }

            case ProtectionType.Whitelist:
            {
                return(globalProtection.Whitelist.Any(u => u.DiscordID == (long)invokingUser.Id)
                                                ? DetermineConditionResult.FromSuccess()
                                                : DetermineConditionResult.FromError("You're not on that user's whitelist."));
            }

            default:
            {
                throw new ArgumentOutOfRangeException();
            }
            }
        }