Exemple #1
0
        public async Task <bool> AntiIp(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck)
        {
            var guild    = context.Server;
            var BypassIP = exemptcheck.Any(x => x.Privacy);

            if (!BypassIP)
            {
                if (Regex.IsMatch(context.Message.Content, @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
                {
                    await context.Message?.DeleteAsync();

                    var emb = new EmbedBuilder
                    {
                        Title = $"{context.User} - This server does not allow you to post IP addresses"
                    };
                    await context.Channel.SendMessageAsync("", false, emb.Build());

                    if (guild.Antispam.Privacy.WarnOnDetection)
                    {
                        await guild.AddWarn("AutoMod - Anti IP", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                        guild.Save();
                    }

                    return(true);
                }
            }

            return(false);
        }
Exemple #2
0
        public bool CheckHidden(LithiumContext context)
        {
            if (context.Guild == null)
            {
                return(false);
            }
            var guild = context.Server;

            if (guild.Settings.DisabledParts.BlacklistedCommands.Any() || guild.Settings.DisabledParts.BlacklistedModules.Any())
            {
                CommandInfo CMDCheck  = null;
                var         argPos    = 0;
                var         cmdSearch = _commands.Search(context, argPos);
                if (cmdSearch.IsSuccess)
                {
                    CMDCheck = cmdSearch.Commands.FirstOrDefault().Command;
                }

                if (CMDCheck != null)
                {
                    var guser = (IGuildUser)context.User;
                    if (!guser.GuildPermissions.Administrator && !guild.ModerationSetup.AdminRoles.Any(x => guser.RoleIds.Contains(x)))
                    {
                        if (guild.Settings.DisabledParts.BlacklistedCommands.Any(x => string.Equals(x, CMDCheck.Name, StringComparison.CurrentCultureIgnoreCase)) ||
                            guild.Settings.DisabledParts.BlacklistedModules.Any(x => string.Equals(x, CMDCheck.Module.Name, StringComparison.CurrentCultureIgnoreCase)))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #3
0
        public static string DoReplacements(string input, LithiumContext context)
        {
            var result = input;

            if (!string.IsNullOrEmpty(input))
            {
                result = Regex.Replace(input, "{user}", context.User.Username, RegexOptions.IgnoreCase);
                result = Regex.Replace(result, "{user.mention}", context.User.Mention, RegexOptions.IgnoreCase);
                result = Regex.Replace(result, "{guild}", context.Guild.Name, RegexOptions.IgnoreCase);
                result = Regex.Replace(result, "{channel}", context.Channel.Name, RegexOptions.IgnoreCase);
                result = Regex.Replace(result, "{channel.mention}", ((SocketTextChannel)context.Channel).Mention, RegexOptions.IgnoreCase);
            }
            return(result);
        }
Exemple #4
0
        public async Task <bool> CheckToxicity(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck, CommandInfo CMDCheck)
        {
            var guild = context.Server;

            if (guild.Antispam.Toxicity.UsePerspective)
            {
                var BypassToxicity = exemptcheck.Any(x => x.Toxicity);

                if (!BypassToxicity)
                {
                    var CheckUsingToxicity = CMDCheck == null;

                    if (ToxicityAPI != null && CheckUsingToxicity && !string.IsNullOrWhiteSpace(context.Message.Content))
                    {
                        try
                        {
                            var res = ToxicityAPI.QueryToxicity(context.Message.Content);
                            if (res.attributeScores.TOXICITY.summaryScore.value * 100 > guild.Antispam.Toxicity.ToxicityThreshHold)
                            {
                                await context.Message?.DeleteAsync();

                                var emb = new EmbedBuilder
                                {
                                    Title       = "Toxicity Threshhold Breached",
                                    Description = $"{context.User.Mention}"
                                };
                                await context.Channel.SendMessageAsync("", false, emb.Build());

                                if (guild.Antispam.Blacklist.WarnOnDetection)
                                {
                                    await guild.AddWarn("AutoMod - Toxicity", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                                    guild.Save();
                                }

                                return(true);
                            }
                        }
                        catch
                        {
                            //
                        }
                    }
                }
            }

            return(false);
        }
Exemple #5
0
        public async Task <bool> CheckBlacklist(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck, CommandInfo CMDCheck)
        {
            var guild = context.Server;

            if (guild.Antispam.Blacklist.BlacklistWordSet.Any())
            {
                if (CMDCheck == null)
                {
                    var BypassBlacklist = exemptcheck.Any(x => x.Blacklist);

                    if (!BypassBlacklist)
                    {
                        var blacklistdetected       = false;
                        var blacklistmessage        = guild.Antispam.Blacklist.DefaultBlacklistMessage;
                        var detectedblacklistmodule = guild.Antispam.Blacklist.BlacklistWordSet.FirstOrDefault(blist => blist.WordList.Any(x => context.Message.Content.ToLower().Contains(x.ToLower())));
                        if (detectedblacklistmodule != null)
                        {
                            blacklistdetected = true;
                            blacklistmessage  = detectedblacklistmodule.BlacklistResponse ?? guild.Antispam.Blacklist.DefaultBlacklistMessage;
                        }

                        if (blacklistdetected)
                        {
                            await context.Message?.DeleteAsync();

                            if (!string.IsNullOrEmpty(blacklistmessage))
                            {
                                var result = Discord.Extensions.Formatting.DoReplacements(blacklistmessage, context);
                                await context.Channel.SendMessageAsync(result);
                            }

                            if (guild.Antispam.Blacklist.WarnOnDetection)
                            {
                                await guild.AddWarn("AutoMod - Blacklist", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                                guild.Save();
                            }

                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #6
0
        public async Task <bool> AntiInvite(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck)
        {
            var guild         = context.Server;
            var bypass_invite = exemptcheck.Any(x => x.Advertising);

            if (!bypass_invite)
            {
                if (Regex.Match(context.Message.Content, @"(http|https)?(:)?(\/\/)?(discordapp|discord).(gg|io|me|com)\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!-/]))?").Success)
                {
                    await context.Message?.DeleteAsync();

                    var emb = new EmbedBuilder();
                    if (guild.Antispam.Advertising.NoInviteMessage != null)
                    {
                        emb.Description = Formatting.DoReplacements(guild.Antispam.Advertising.NoInviteMessage, context);
                    }
                    else
                    {
                        emb.Description = $"{context.User} - This server does not allow you to send invite links in chat";
                    }

                    // Description = guild.Antispam.Advertising.NoInviteMessage ?? $"{context.User?.Mention} - no sending invite links... the admins might get angry"
                    await context.Channel.SendMessageAsync(string.Empty, false, emb.Build());

                    if (guild.Antispam.Advertising.WarnOnDetection)
                    {
                        await guild.AddWarn("AutoMod - Anti Advertising", context.User as IGuildUser, context.Client.CurrentUser, context.Channel, context.Message.Content);

                        guild.Save();
                    }

                    return(true);
                }
            }

            return(false);
        }
Exemple #7
0
        public async Task <bool> AntiInvite(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck)
        {
            var guild        = context.Server;
            var BypassInvite = exemptcheck.Any(x => x.Advertising);

            if (!BypassInvite)
            {
                if (Regex.Match(context.Message.Content, @"(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?(d+i+s+c+o+r+d+|a+p+p)+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$").Success)
                {
                    await context.Message?.DeleteAsync();

                    var emb = new EmbedBuilder();
                    if (guild.Antispam.Advertising.NoInviteMessage != null)
                    {
                        emb.Description = Formatting.DoReplacements(guild.Antispam.Advertising.NoInviteMessage, context);
                    }
                    else
                    {
                        emb.Description = $"{context.User} - This server does not allow you to send invite links in chat";
                    }
                    //    Description = guild.Antispam.Advertising.NoInviteMessage ?? $"{context.User?.Mention} - no sending invite links... the admins might get angry"
                    await context.Channel.SendMessageAsync("", false, emb.Build());

                    if (guild.Antispam.Advertising.WarnOnDetection)
                    {
                        await guild.AddWarn("AutoMod - Anti Advertising", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                        guild.Save();
                    }

                    return(true);
                }
            }

            return(false);
        }
Exemple #8
0
        public async Task DoCommand(SocketMessage parameterMessage)
        {
            try
            {
                if (!(parameterMessage is SocketUserMessage message))
                {
                    return;
                }
                var argPos  = 0;
                var context = new LithiumContext(_client, message, Provider);

                //Do not react to commands initiated by a bot
                if (context.User.IsBot)
                {
                    return;
                }

                if (await RunSpamChecks(context))
                {
                    return;
                }

                //Ensure that commands are only executed if they start with the bot's prefix
                if (!(message.HasMentionPrefix(_client.CurrentUser, ref argPos) ||
                      message.HasStringPrefix(Config.Load().DefaultPrefix, ref argPos) ||
                      (context.Server?.Settings.Prefix != null && message.HasStringPrefix(context.Server.Settings.Prefix, ref argPos))))
                {
                    return;
                }

                //Ensure that the message passes all checks before running as a command
                if (CheckHidden(context))
                {
                    return;
                }

                var result = await _commands.ExecuteAsync(context, argPos, Provider);

                var commandsuccess = result.IsSuccess;

                if (!commandsuccess)
                {
                    var embed = new EmbedBuilder
                    {
                        Title       = $"ERROR: {result.Error.ToString().ToUpper()}",
                        Description = $"Command: {context.Message}\n" +
                                      $"Error: {result.ErrorReason}"
                    };
                    await context.Channel.SendMessageAsync("", false, embed.Build());

                    Logger.LogMessage($"{message.Content} || {message.Author}", LogSeverity.Error);
                }
                else
                {
                    Logger.LogMessage($"{message.Content} || {message.Author}");
                }
            }
            catch (Exception e)
            {
                Logger.LogMessage(e.ToString(), LogSeverity.Error);
            }
        }
Exemple #9
0
        public async Task <bool> RunSpamChecks(LithiumContext context)
        {
            if (context.Guild == null)
            {
                return(false);
            }
            if (context.Channel is IDMChannel)
            {
                return(false);
            }
            if (context.Server == null)
            {
                return(false);
            }
            try
            {
                var guild = context.Server;

                var exemptcheck = new List <GuildModel.Guild.antispams.IgnoreRole>();
                if (guild.Antispam.IgnoreRoles.Any())
                {
                    exemptcheck = guild.Antispam.IgnoreRoles.Where(x => ((IGuildUser)context.User).RoleIds.Contains(x.RoleID)).ToList();
                }

                if (guild.Antispam.Antispam.NoSpam)
                {
                    if (await AntiSpam(context, exemptcheck))
                    {
                        return(true);
                    }
                }

                if (guild.Antispam.Advertising.Invite)
                {
                    if (await AntiInvite(context, exemptcheck))
                    {
                        return(true);
                    }
                }

                if (guild.Antispam.Mention.RemoveMassMention || guild.Antispam.Mention.MentionAll)
                {
                    if (await AntiMention(context, exemptcheck))
                    {
                        return(true);
                    }
                }


                if (guild.Antispam.Privacy.RemoveIPs)
                {
                    if (await AntiIp(context, exemptcheck))
                    {
                        return(true);
                    }
                }


                if (guild.Antispam.Blacklist.BlacklistWordSet.Any() || guild.Antispam.Toxicity.UsePerspective)
                {
                    CommandInfo CMDCheck  = null;
                    var         argPos    = 0;
                    var         cmdSearch = _commands.Search(context, argPos);
                    if (cmdSearch.IsSuccess)
                    {
                        CMDCheck = cmdSearch.Commands.FirstOrDefault().Command;
                    }

                    if (await CheckBlacklist(context, exemptcheck, CMDCheck))
                    {
                        return(true);
                    }
                    if (await CheckToxicity(context, exemptcheck, CMDCheck))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.LogMessage($"AntiSpam Error G:[{context.Guild.Id}] GN:{context.Guild.Name} C:{context.Channel.Name} U:{context.User.Username}\n" +
                                  $"{e}", LogSeverity.Error);
            }


            return(false);
        }
Exemple #10
0
        public async Task <bool> AntiMention(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck)
        {
            var guild         = context.Server;
            var BypassMention = exemptcheck.Any(x => x.Mention);

            if (!BypassMention)
            {
                if (guild.Antispam.Mention.RemoveMassMention)
                {
                    if (context.Message.MentionedRoleIds.Count + context.Message.MentionedUserIds.Count >= 5)
                    {
                        await context.Message?.DeleteAsync();

                        var emb = new EmbedBuilder
                        {
                            Description = $"{context.User} - This server does not allow you to mention 5+ roles or uses at once"
                        };
                        await context.Channel.SendMessageAsync("", false, emb.Build());

                        if (guild.Antispam.Mention.WarnOnDetection)
                        {
                            await guild.AddWarn("AutoMod - Mass Mention", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                            guild.Save();
                        }

                        return(true);
                    }
                }

                if (guild.Antispam.Mention.MentionAll)
                {
                    if (context.Message.Content.Contains("@everyone") || context.Message.Content.Contains("@here"))
                    {
                        await context.Message?.DeleteAsync();

                        var emb = new EmbedBuilder();
                        if (guild.Antispam.Mention.MentionAllMessage != null)
                        {
                            emb.Description = guild.Antispam.Mention.MentionAllMessage;
                        }
                        else
                        {
                            emb.Title = $"{context.User} - This server has disabled the ability for you to mention @everyone and @here";
                        }

                        await context.Channel.SendMessageAsync("", false, emb.Build());

                        if (guild.Antispam.Mention.WarnOnDetection)
                        {
                            await guild.AddWarn("AutoMod - Mention All", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                            guild.Save();
                        }

                        return(true);
                        //if
                        // 1. The server Has Mention Deletions turned on
                        // 2. The user is not an admin
                        // 3. The user does not have one of the mention excempt roles
                    }
                }
            }

            return(false);
        }
Exemple #11
0
        public async Task <bool> AntiSpam(LithiumContext context, List <GuildModel.Guild.antispams.IgnoreRole> exemptcheck)
        {
            var guild     = context.Server;
            var detected  = false;
            var SpamGuild = NoSpam.FirstOrDefault(x => x.GuildID == ((SocketGuildUser)context.User).Guild.Id);

            if (SpamGuild == null)
            {
                NoSpam.Add(new NoSpamGuild
                {
                    GuildID = context.Guild.Id,
                    Users   = new List <NoSpamGuild.NoSpam>
                    {
                        new NoSpamGuild.NoSpam
                        {
                            UserID   = context.User.Id,
                            Messages = new List <NoSpamGuild.NoSpam.Msg>
                            {
                                new NoSpamGuild.NoSpam.Msg
                                {
                                    LastMessage     = context.Message.Content,
                                    LastMessageDate = DateTime.UtcNow
                                }
                            }
                        }
                    }
                });
            }
            else
            {
                var user = SpamGuild.Users.FirstOrDefault(x => x.UserID == context.User.Id);
                if (user == null)
                {
                    SpamGuild.Users.Add(new NoSpamGuild.NoSpam
                    {
                        UserID   = context.User.Id,
                        Messages = new List <NoSpamGuild.NoSpam.Msg>
                        {
                            new NoSpamGuild.NoSpam.Msg
                            {
                                LastMessage     = context.Message.Content,
                                LastMessageDate = DateTime.UtcNow
                            }
                        }
                    });
                }
                else
                {
                    user.Messages.Add(new NoSpamGuild.NoSpam.Msg
                    {
                        LastMessage     = context.Message.Content,
                        LastMessageDate = DateTime.UtcNow
                    });
                    if (user.Messages.Count >= 2)
                    {
                        var msgs = user.Messages.Where(x => x.LastMessageDate > DateTime.UtcNow - TimeSpan.FromSeconds(10)).ToList();
                        //Here we detect spam based on wether or not a user is sending the same message repeatedly
                        //Or wether they have sent a message more than 3 times in the last 5 seconds
                        if (msgs.GroupBy(n => n.LastMessage.ToLower()).Any(c => c.Count() > 1) || msgs.Count(x => x.LastMessageDate > DateTime.UtcNow - TimeSpan.FromSeconds(5)) > 3)
                        {
                            detected = true;
                        }
                    }

                    if (user.Messages.Count > 10)
                    {
                        //Filter out messages so that we only keep a log of the most recent ones within the last 10 seconds.
                        var msgs = user.Messages.OrderBy(x => x.LastMessageDate).ToList();
                        msgs.RemoveRange(0, 1);
                        msgs          = msgs.Where(x => x.LastMessageDate > DateTime.UtcNow - TimeSpan.FromSeconds(10)).ToList();
                        user.Messages = msgs;
                    }

                    if (detected)
                    {
                        var BypassAntispam = exemptcheck.Any(x => x.AntiSpam);
                        if (!BypassAntispam)
                        {
                            if (!guild.Antispam.Antispam.AntiSpamSkip.Any(x => context.Message.Content.ToLower().Contains(x.ToLower())))
                            {
                                await context.Message?.DeleteAsync();

                                var delay = AntiSpamMsgDelays.FirstOrDefault(x => x.GuildID == guild.GuildID);
                                if (delay != null)
                                {
                                    if (delay._delay > DateTime.UtcNow)
                                    {
                                        return(true);
                                    }

                                    delay._delay = DateTime.UtcNow.AddSeconds(5);
                                    var emb = new EmbedBuilder
                                    {
                                        Description = $"{context.User} - No Spamming!!"
                                    };
                                    await context.Channel.SendMessageAsync("", false, emb.Build());

                                    if (guild.Antispam.Antispam.WarnOnDetection)
                                    {
                                        await guild.AddWarn("AutoMod - AntiSpam", context.User as IGuildUser, context.Client.CurrentUser, context.Channel);

                                        guild.Save();
                                    }
                                }
                                else
                                {
                                    AntiSpamMsgDelays.Add(new Delays
                                    {
                                        _delay  = DateTime.UtcNow.AddSeconds(5),
                                        GuildID = guild.GuildID
                                    });
                                }
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }