public LogCommandService(DiscordSocketClient client, NadekoStrings strings,
                                 IEnumerable <GuildConfig> gcs, DbService db, MuteService mute, ProtectionService prot, GuildTimezoneService tz)
        {
            _client  = client;
            _log     = LogManager.GetCurrentClassLogger();
            _strings = strings;
            _db      = db;
            _tz      = tz;

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

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

                    await Task.WhenAll(keys.Select(key =>
                    {
                        if (!PresenceUpdates.TryRemove(key, out var msgs))
                        {
                            return(Task.CompletedTask);
                        }
                        var title = GetText(key.Guild, "presence_updates");
                        var desc  = string.Join(Environment.NewLine, msgs);
                        return(key.SendConfirmAsync(title, desc.TrimTo(2048)));
                    }));
                }
                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;
        }
Esempio n. 2
0
        public ProtectionService(DiscordSocketClient client, DbService db, MuteService mute)
        {
            _db   = db;
            _mute = mute;
            _log  = LogManager.GetCurrentClassLogger();

            client.UserJoined      += UserJoinedAntiRaidHandler;
            client.MessageReceived += MessageReceivedAntiSpamHandler;
        }
 public UserPunishService(MuteService mute, DbService db)
 {
     _mute = mute;
     _db   = db;
 }
Esempio n. 4
0
        public LogService(DiscordSocketClient client, StringService strings, DbService db, MuteService mute, ProtectionService prot, GuildTimezoneService tz, VerificationService vs)
        {
            _client  = client;
            _db      = db;
            _strings = strings;
            _tzs     = tz;
            _vs      = vs;
            _log     = LogManager.GetCurrentClassLogger();

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

                    await Task.WhenAll(keys.Select(key => {
                        if (!PresenceUpdates.TryRemove(key, out var msgs))
                        {
                            return(Task.CompletedTask);
                        }
                        var title = GetText(key.Guild, "presence_updates");
                        var desc  = string.Join(Environment.NewLine, msgs);
                        return(key.SendConfirmAsync(desc.TrimTo(2048), title));
                    }));
                } 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;
            _client.UserUpdated           += _client_UserUpdated;
            _client.ChannelCreated        += _client_ChannelCreated;
            _client.ChannelDestroyed      += _client_ChannelDestroyed;
            _client.ChannelUpdated        += _client_ChannelUpdated;

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

            prot.OnAntiProtectionTriggered += TriggeredAntiProtection;

            _vs.VerificationStep    += VerificationService_VerificationStep;
            _vs.VerificationMessage += VerificationService_VerificationMessage;
        }
        public ProtectionService(DiscordSocketClient client, IEnumerable <GuildConfig> gcs, MuteService mute)
        {
            _log    = LogManager.GetCurrentClassLogger();
            _client = client;
            _mute   = mute;

            foreach (var gc in gcs)
            {
                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);
            };
        }