Exemplo n.º 1
0
 public MessageContentPatternDto(string pattern, MessageContentPatternType patternType)
 {
     Pattern = pattern;
     Type    = patternType;
 }
        public async Task <ServiceResponse> AddPattern(ulong guildId, string regexPattern, MessageContentPatternType patternType)
        {
            if (!_authorizationService.HasClaim(AuthorizationClaim.ManageMessageContentPatterns))
            {
                return(ServiceResponse.Fail("User does not have claim to manage patterns!"));
            }

            if (!IsValidRegex(regexPattern))
            {
                return(ServiceResponse.Fail("Pattern is not a valid Regex!"));
            }

            if (await DoesPatternExist(guildId, regexPattern))
            {
                return(ServiceResponse.Fail("Pattern already exists!"));
            }

            var entity = new MessageContentPatternEntity
            {
                GuildId     = guildId,
                Pattern     = regexPattern,
                PatternType = patternType,
            };

            _db.Add(entity);

            await _db.SaveChangesAsync();

            ClearCacheForGuild(guildId);

            return(ServiceResponse.Ok());
        }