Exemple #1
0
        private async void OnChatMessage(ChatMessage message)
        {
            using IDisposable logScope = message.BeginLogScope(_log);

            // run only in prod, test group or owner PM
            if (!_environment.IsProduction() &&
                !((message.IsGroupMessage && message.RecipientID == _botOptions.CurrentValue.TestGroupID) ||
                  (message.IsPrivateMessage && message.SenderID == _botOptions.CurrentValue.OwnerID)))
            {
                return;
            }

            try
            {
                // only work in group text messages
                if (!message.IsText || !message.IsGroupMessage)
                {
                    return;
                }

                // quit early if group or user is ignored
                if (_mentionsOptions.CurrentValue.IgnoredGroups?.Contains(message.RecipientID) == true)
                {
                    return;
                }
                if (_mentionsOptions.CurrentValue.IgnoredUsers?.Contains(message.SenderID.Value) == true)
                {
                    return;
                }

                CancellationToken           cancellationToken = _cts?.Token ?? default;
                IEnumerable <MentionConfig> allMentions       = await _mentionConfigStore.GetAllAsync(cancellationToken).ConfigureAwait(false);

                if (allMentions?.Any() == true)
                {
                    foreach (MentionConfig mentionConfig in allMentions)
                    {
                        if (!mentionConfig.Patterns.Any(pattern => pattern.Regex.IsMatch(message.Text)))
                        {
                            continue;
                        }
                        if (mentionConfig.IgnoreSelf && mentionConfig.ID == message.SenderID.Value)
                        {
                            continue;
                        }

                        string text = await BuildMentionMessage(message, mentionConfig, cancellationToken).ConfigureAwait(false);

                        await _client.SendPrivateTextMessageAsync(mentionConfig.ID, text, cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (TaskCanceledException) { }
            catch (MessageSendingException ex) when(ex.SentMessage is ChatMessage && ex.Response is WolfResponse response && response.ErrorCode == WolfErrorCode.LoginIncorrectOrCannotSendToGroup)
            {
            }
            catch (Exception ex) when(ex.LogAsError(_log, "Error occured when processing message"))
            {
            }
        }