Example #1
0
        public static async Task CheckMessageWithAutoMod(SocketCommandContext context)
        {
            var config = DataManager.AllGuildData[context.Guild.Id].Moderator;

            UserRecord record;

            try
            {
                record = GetUserRecord(config, context.User.Id);
                record.Messages.Add(context.Message);
            }
            catch (ArgumentException)
            {
                // Message was from bot.
                return;
            }

            // Conserve memory.
            record.Messages.Truncate(15);

            if (!config.IsAutoModOn)
            {
                return;
            }

            string dueTo;
            string warningMessage;

            if (AutoModMethods.CheckMessagesForSpam(record.Messages))
            {
                warningMessage = "Recent messages were automatically deemed to be spam.";
                dueTo          = "spam";
            }
            else if (AutoModMethods.CheckMessagesForRepeatedContent(record.Messages))
            {
                warningMessage = "Recent messages contained repeated content.";
                dueTo          = "repeated messages";
            }
            else
            {
                // No need to act on the messages.
                return;
            }

            // Don't auto warn the user too many times in a short time period.
            if (record.Warnings.Count > 0)
            {
                var lastWarningTime = DateTime.FromBinary(record.Warnings.Last().DateTimeBinary);
                if (DateTime.Now - lastWarningTime < TimeSpan.FromSeconds(20))
                {
                    return;
                }
            }

            await WarnOrBanUserAsync(
                config : config,
                victim : (SocketGuildUser)context.User,
                requestedBy : await BotService.GetClientGuildUserAsync(context.Channel),
                message : warningMessage);

            await new InfoMessage($"{context.User.Mention} has been warned due to {dueTo}.")
            .SendAsync(context.Channel);
        }