Beispiel #1
0
        public async Task CheckMessage(SocketMessage message)
        {
            try {
                if (message.Author.IsBot || !(message.Channel is SocketGuildChannel) || !(message is SocketUserMessage))
                {
                    return; //Makes sure it's not logging a message from a bot and that it's in a discord server
                }
                SocketCommandContext context = new SocketCommandContext(client, message as SocketUserMessage);
                SocketGuildChannel   chnl    = message.Channel as SocketGuildChannel;

                if (chnl?.Guild == null)
                {
                    return;
                }
                var Guild = chnl.Guild;
                ModerationSettings modSettings = Guild.LoadFromFile <ModerationSettings>();
                SocketGuildUser    gUser       = message.Author as SocketGuildUser;
                List <BadWord>     badWords    = Guild.LoadFromFile <BadWordList>()?.badWords;

                if (modSettings != null)
                {
                    if (modSettings.channelsWithoutAutoMod != null && modSettings.channelsWithoutAutoMod.Contains(chnl.Id) || (message.Author as SocketGuildUser).CantBeWarned())
                    {
                        return; //Returns if channel is set as not using automod
                    }
                    //Checks if a message contains an invite
                    if (!modSettings.invitesAllowed && message.Content.ToLower().Contains("discord.gg/") || message.Content.ToLower().Contains("discordapp.com/invite/"))
                    {
                        await context.Punish("Posted Invite");

                        return;
                    }

                    //Checks for links
                    if ((modSettings.allowedLinks != null && modSettings.allowedLinks.Count > 0) && (modSettings.allowedToLink == null || !gUser.RoleIDs().Intersect(modSettings.allowedToLink).Any()))
                    {
                        const string    linkRegex = @"^((?:https?|steam):\/\/[^\s<]+[^<.,:;" + "\"\'\\]\\s])";
                        MatchCollection matches   = Regex.Matches(message.Content, linkRegex, RegexOptions.IgnoreCase);
                        //if (matches != null && matches.Count > 0) await new LogMessage(LogSeverity.Info, "Filter", "Link detected").Log();
                        foreach (Match match in matches)
                        {
                            if (!modSettings.allowedLinks.Any(s => match.ToString().ToLower().Contains(s.ToLower())))
                            {
                                await context.Punish("Using unauthorized links", 1);

                                return;
                            }
                        }
                    }

                    //Check for emojis
                    if (modSettings.badUEmojis.NotEmpty() && modSettings.badUEmojis.Any(s => message.Content.Contains(s)))
                    {
                        await context.Punish("Bad emoji used", 0.8f);

                        return;
                    }

                    if (modSettings.allowedCaps > 0 && message.Content.Length > 5)
                    {
                        uint amountCaps = 0;
                        foreach (char c in message.Content)
                        {
                            if (char.IsUpper(c))
                            {
                                amountCaps++;
                            }
                        }
                        if (((amountCaps / (float)message.Content.Length) * 100) >= modSettings.allowedCaps)
                        {
                            await context.Punish("Excessive caps", 0.3f);

                            return;
                        }
                    }
                } //End of stuff from mod settings

                //Checks for bad words
                if (badWords != null)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (char c in message.Content)
                    {
                        if (sb.ToString().Length + 1 == message.Content.Length)
                        {
                            sb.Append(c);
                            break;
                        }
                        switch (c)
                        {
                        case '1':
                        case '!':
                            sb.Append('i');
                            break;

                        case '0':
                            sb.Append('o');
                            break;

                        case '8':
                            sb.Append('b');
                            break;

                        case '3':
                            sb.Append('e');
                            break;

                        case '$':
                            sb.Append('s');
                            break;

                        case '@':
                        case '4':
                            sb.Append('a');
                            break;

                        default:
                            if (!char.IsPunctuation(c) && !char.IsSymbol(c))
                            {
                                sb.Append(c);
                            }
                            break;
                        }
                    }

                    string strippedMessage = sb.ToString().ToLower();

                    foreach (BadWord badWord in badWords)
                    {
                        if (badWord.partOfWord)
                        {
                            if (strippedMessage.Contains(badWord.word.ToLower()))
                            {
                                if (badWord.euphemism != null && badWord.euphemism != "")
                                {
                                    await context.Punish("Bad word used (" + badWord.euphemism + ")");
                                }
                                else
                                {
                                    await context.Punish("Bad word used");
                                }

                                return;
                            }
                        }
                        else
                        {
                            string[] messageParts = strippedMessage.Split(' ');
                            foreach (string word in messageParts)
                            {
                                if (word == badWord.word.ToLower())
                                {
                                    if (badWord.euphemism != null && badWord.euphemism != "")
                                    {
                                        await context.Punish("Bad word used (" + badWord.euphemism + ")", badWord.size);
                                    }
                                    else
                                    {
                                        await context.Punish("Bad word used", badWord.size);
                                    }
                                    return;
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                await new LogMessage(LogSeverity.Error, "Filter", "Something went wrong with the message filter", e).Log();
            }
        }