Beispiel #1
0
        public async void Analyze(Message message)
        {
            if (!ShouldMonitorChannel(message.Channel))
            {
                Log.Verbose("Skipping message from channel {channel}", message.Channel.Name);
                return;
            }

            float spamValue = Heuristics.CalculateSpamValue(message);

            Log.Logger.Information("Message from {user} had a spam rating of : {rating} ({heuristictype})",
                                   message.User.Name, spamValue, Heuristics.AggregateMethod);

            List <string> heuristicLog = new List <string>();

            foreach (SpamHeuristic heuristic in Heuristics.Heuristics.Keys)
            {
                string heuristicName       = heuristic.GetType().Name;
                float  individualSpamValue = heuristic.CalculateSpamValue(message, false);
                float  weight = Heuristics.Heuristics[heuristic];

                Log.Logger.Information("\t {heuristic}: {rating} ({weight}wt)", heuristicName, individualSpamValue, weight);

                heuristicLog.Add($"{heuristic}: {individualSpamValue} ({weight}wt)");
            }

            var toLog = new MessageLog
            {
                SenderName = message.User.Name,
                Message    = message.Text,
                SentTime   = message.Timestamp,
                SpamValue  = spamValue,
                Channel    = message.Channel.Name,
                Server     = new DiscordServerInfo
                {
                    Name      = message.Server.Name,
                    Icon      = message.Server.IconUrl,
                    Id        = message.Server.Id.ToString(),
                    UserCount = message.Server.UserCount
                },
                Details = heuristicLog,
                Formula = Heuristics.AggregateMethod.ToString()
            };

            foreach (SpamTrigger trigger in Triggers.OrderByDescending(d => d.TriggerScore))
            {
                if (!trigger.ShouldTrigger(spamValue))
                {
                    continue;
                }

                Log.Logger.Information("Message from {user} caused a trigger: {trigger} ({messagescore}/{triggerscore})",
                                       message.User.Name, trigger.GetType().Name, spamValue, trigger.TriggerScore);

                toLog.Trigger = $"{trigger.GetType().Name}: out of {trigger.TriggerScore}";

                try
                {
                    trigger.Trigger(_client, message);
                }
                catch (AggregateException ex)
                {
                    Log.Logger.Error(ex.InnerExceptions.FirstOrDefault(), "Tried to perform trigger {trigger}, but got an error", trigger.GetType().Name);
                }
                catch (Exception ex)
                {
                    Log.Logger.Error(ex, "Tried to perform trigger {trigger}, but got an error", trigger.GetType().Name);
                }

                if (!trigger.PassThrough)
                {
                    break;
                }
            }

            await StoreMessage(toLog);
        }