Ejemplo n.º 1
0
        async Task HandleMudaeMessageAsync(IUserMessage message)
        {
            if (!message.Embeds.Any())
            {
                return;
            }

            var guild = ((IGuildChannel)message.Channel).Guild;
            var embed = message.Embeds.First();

            if (embed.Footer.HasValue)
            {
                // character must not belong to another user
                if (embed.Footer.Value.Text.StartsWith("Belongs to", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                // message must not be $im
                if (_imFooterRegex.IsMatch(embed.Footer.Value.Text))
                {
                    return;
                }
            }

            //
            if (!embed.Author.HasValue || embed.Author.Value.IconUrl != null)
            {
                return;
            }


            var description = embed.Description.Split('\n');
            var character   = embed.Author.Value.Name.Trim().ToLowerInvariant();
            var anime       = description[0].Trim().ToLowerInvariant();
            var KakeraValue = 0;

            //if length >= 2 then description includes additional info
            if (description.Length >= 2)
            {
                //46983... is emoji symbol
                if (description[description.Length - 1].Contains("469835869059153940"))
                {
                    String KakeraValueString = new String(description[description.Length - 1].SkipWhile(c => !Char.IsDigit(c)).TakeWhile(Char.IsDigit).ToArray());
                    KakeraValue = Convert.ToInt32(KakeraValueString);
                }
            }

            // matching by character and name
            var matched         = false;
            var kakeraThreshold = _config.KakeraThreshold;

            matched |= _config.WishedCharacterRegex?.IsMatch(character) ?? false;
            matched |= _config.WishedAnimeRegex?.IsMatch(anime) ?? false;

            // matching by wishlist
            if (message.Content.StartsWith("Wished by"))
            {
                matched |= message.GetUserIds().Any(_config.ClaimWishlistUserIds.Contains);
            }

            if (matched)
            {
                var state = _state.Get(guild.Id);

                // ensure we can claim right now
                if (!state.CanClaim && DateTime.Now < state.ClaimReset)
                {
                    Log.Warning($"{guild} {message.Channel}: Found character '{character}' but cannot claim it due to cooldown.");
                    return;
                }
                //check and see if character's kakera value is at or above threshold
                if (KakeraValue >= kakeraThreshold)
                {
                    Log.Warning($"{guild} {message.Channel}: Found character '{character}', trying marriage.");
                }
                else
                {
                    Log.Warning($"{guild} {message.Channel}: Found character '{character}' but cannot claim it due to character's kakera value '{KakeraValue}' not greater than or equal to threshold. '{kakeraThreshold}'");
                    return;
                }
                // reactions may not have been attached when we received this message
                // remember this message so we can attach an appropriate reaction later when we receive it
                _claimQueue[message.Id] = new ClaimQueueItem
                {
                    Message   = message,
                    Character = new CharacterInfo(character, anime),
                    Measure   = new MeasureContext()
                };
            }
            else
            {
                Log.Info($"{guild} #{message.Channel}: Ignored character '{character}', not wished.");
            }
        }