コード例 #1
0
ファイル: ProtectionService.cs プロジェクト: Nielk1/NadekoBot
        public ProtectionService(DiscordSocketClient client, NadekoBot bot,
                                 MuteService mute, DbService db, UserPunishService punishService)
        {
            _client        = client;
            _mute          = mute;
            _db            = db;
            _punishService = punishService;

            var ids = client.GetGuildIds();

            using (var uow = db.GetDbContext())
            {
                var configs = uow._context.Set <GuildConfig>()
                              .AsQueryable()
                              .Include(x => x.AntiRaidSetting)
                              .Include(x => x.AntiSpamSetting)
                              .ThenInclude(x => x.IgnoredChannels)
                              .Include(x => x.AntiAltSetting)
                              .Where(x => ids.Contains(x.GuildId))
                              .ToList();

                foreach (var gc in configs)
                {
                    Initialize(gc);
                }
            }

            _client.MessageReceived += HandleAntiSpam;
            _client.UserJoined      += HandleUserJoined;

            bot.JoinedGuild   += _bot_JoinedGuild;
            _client.LeftGuild += _client_LeftGuild;

            _ = Task.Run(RunQueue);
        }
コード例 #2
0
ファイル: UserPunishService.cs プロジェクト: tsujon12/Nadeko
        public UserPunishService(MuteService mute, DbService db)
        {
            _mute = mute;
            _db   = db;
            _log  = LogManager.GetCurrentClassLogger();

            _warnExpiryTimer = new Timer(async _ =>
            {
                await CheckAllWarnExpiresAsync();
            }, null, TimeSpan.FromSeconds(0), TimeSpan.FromHours(12));
        }
コード例 #3
0
ファイル: UserPunishService.cs プロジェクト: Nielk1/NadekoBot
        public UserPunishService(MuteService mute, DbService db, BlacklistService blacklistService)
        {
            _mute             = mute;
            _db               = db;
            _blacklistService = blacklistService;

            _warnExpiryTimer = new Timer(async _ =>
            {
                await CheckAllWarnExpiresAsync();
            }, null, TimeSpan.FromSeconds(0), TimeSpan.FromHours(12));
        }
コード例 #4
0
        public ProtectionService(DiscordSocketClient client, NadekoBot bot, MuteService mute, DbService db)
        {
            _log    = LogManager.GetCurrentClassLogger();
            _client = client;
            _mute   = mute;
            _db     = db;
            _bot    = bot;

            Initialize();

            _client.MessageReceived += HandleAntiSpam;
            _client.UserJoined      += HandleAntiRaid;

            _bot.JoinedGuild  += _bot_JoinedGuild;
            _client.LeftGuild += _client_LeftGuild;
        }
コード例 #5
0
        public LogCommandService(DiscordSocketClient client, NadekoStrings strings,
                                 NadekoBot bot, DbService db, MuteService mute, ProtectionService prot, GuildTimezoneService tz)
        {
            _client  = client;
            _log     = LogManager.GetCurrentClassLogger();
            _strings = strings;
            _db      = db;
            _mute    = mute;
            _prot    = prot;
            _tz      = tz;

            GuildLogSettings = bot.AllGuildConfigs
                               .ToDictionary(g => g.GuildId, g => g.LogSetting)
                               .ToConcurrent();

            _timerReference = new Timer(async(state) =>
            {
                try
                {
                    var keys = PresenceUpdates.Keys.ToList();

                    await Task.WhenAll(keys.Select(key =>
                    {
                        if (PresenceUpdates.TryRemove(key, out var msgs))
                        {
                            var title = GetText(key.Guild, "presence_updates");
                            var desc  = string.Join(Environment.NewLine, msgs);
                            return(key.SendConfirmAsync(title, desc.TrimTo(2048)));
                        }
                        return(Task.CompletedTask);
                    }));
                }
                catch (Exception ex)
                {
                    _log.Warn(ex);
                }
            }, null, TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(15));

            //_client.MessageReceived += _client_MessageReceived;
            _client.MessageUpdated += _client_MessageUpdated;
            _client.MessageDeleted += _client_MessageDeleted;
            _client.UserBanned     += _client_UserBanned;
            _client.UserUnbanned   += _client_UserUnbanned;
            _client.UserJoined     += _client_UserJoined;
            _client.UserLeft       += _client_UserLeft;
            //_client.UserPresenceUpdated += _client_UserPresenceUpdated;
            _client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated;
            _client.UserVoiceStateUpdated += _client_UserVoiceStateUpdated_TTS;
            _client.GuildMemberUpdated    += _client_GuildUserUpdated;
#if !GLOBAL_NADEKO
            _client.UserUpdated += _client_UserUpdated;
#endif
            _client.ChannelCreated   += _client_ChannelCreated;
            _client.ChannelDestroyed += _client_ChannelDestroyed;
            _client.ChannelUpdated   += _client_ChannelUpdated;

            _mute.UserMuted   += MuteCommands_UserMuted;
            _mute.UserUnmuted += MuteCommands_UserUnmuted;

            _prot.OnAntiProtectionTriggered += TriggeredAntiProtection;
        }
コード例 #6
0
 public UserPunishService(MuteService mute, DbService db)
 {
     _mute = mute;
     _db   = db;
 }
コード例 #7
0
ファイル: ProtectionService.cs プロジェクト: tepup/LinBot
        public ProtectionService(DiscordSocketClient client, NadekoBot bot, MuteService mute)
        {
            _log    = LogManager.GetCurrentClassLogger();
            _client = client;
            _mute   = mute;

            foreach (var gc in bot.AllGuildConfigs)
            {
                var raid = gc.AntiRaidSetting;
                var spam = gc.AntiSpamSetting;

                if (raid != null)
                {
                    var raidStats = new AntiRaidStats()
                    {
                        AntiRaidSettings = raid
                    };
                    AntiRaidGuilds.TryAdd(gc.GuildId, raidStats);
                }

                if (spam != null)
                {
                    AntiSpamGuilds.TryAdd(gc.GuildId, new AntiSpamStats()
                    {
                        AntiSpamSettings = spam
                    });
                }
            }

            _client.MessageReceived += (imsg) =>
            {
                var msg = imsg as IUserMessage;
                if (msg == null || msg.Author.IsBot)
                {
                    return(Task.CompletedTask);
                }

                var channel = msg.Channel as ITextChannel;
                if (channel == null)
                {
                    return(Task.CompletedTask);
                }
                var _ = Task.Run(async() =>
                {
                    try
                    {
                        if (!AntiSpamGuilds.TryGetValue(channel.Guild.Id, out var spamSettings) ||
                            spamSettings.AntiSpamSettings.IgnoredChannels.Contains(new AntiSpamIgnore()
                        {
                            ChannelId = channel.Id
                        }))
                        {
                            return;
                        }

                        var stats = spamSettings.UserStats.AddOrUpdate(msg.Author.Id, (id) => new UserSpamStats(msg),
                                                                       (id, old) =>
                        {
                            old.ApplyNextMessage(msg); return(old);
                        });

                        if (stats.Count >= spamSettings.AntiSpamSettings.MessageThreshold)
                        {
                            if (spamSettings.UserStats.TryRemove(msg.Author.Id, out stats))
                            {
                                stats.Dispose();
                                await PunishUsers(spamSettings.AntiSpamSettings.Action, ProtectionType.Spamming, spamSettings.AntiSpamSettings.MuteTime, (IGuildUser)msg.Author)
                                .ConfigureAwait(false);
                            }
                        }
                    }
                    catch
                    {
                        // ignored
                    }
                });
                return(Task.CompletedTask);
            };

            _client.UserJoined += (usr) =>
            {
                if (usr.IsBot)
                {
                    return(Task.CompletedTask);
                }
                if (!AntiRaidGuilds.TryGetValue(usr.Guild.Id, out var settings))
                {
                    return(Task.CompletedTask);
                }
                if (!settings.RaidUsers.Add(usr))
                {
                    return(Task.CompletedTask);
                }

                var _ = Task.Run(async() =>
                {
                    try
                    {
                        ++settings.UsersCount;

                        if (settings.UsersCount >= settings.AntiRaidSettings.UserThreshold)
                        {
                            var users = settings.RaidUsers.ToArray();
                            settings.RaidUsers.Clear();

                            await PunishUsers(settings.AntiRaidSettings.Action, ProtectionType.Raiding, 0, users).ConfigureAwait(false);
                        }
                        await Task.Delay(1000 * settings.AntiRaidSettings.Seconds).ConfigureAwait(false);

                        settings.RaidUsers.TryRemove(usr);
                        --settings.UsersCount;
                    }
                    catch
                    {
                        // ignored
                    }
                });
                return(Task.CompletedTask);
            };
        }