Ejemplo n.º 1
0
            public async Task AntiSpam(int messageCount = 3, PunishmentAction action = PunishmentAction.Mute)
            {
                if (messageCount < 2 || messageCount > 10)
                {
                    return;
                }

                AntiSpamStats throwaway;

                if (antiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway))
                {
                    using (var uow = DbHandler.UnitOfWork())
                    {
                        var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting)
                                                      .ThenInclude(x => x.IgnoredChannels));

                        gc.AntiSpamSetting = null;
                        await uow.CompleteAsync().ConfigureAwait(false);
                    }
                    await Context.Channel.SendConfirmAsync("**Anti-Spam** has been **disabled** on this server.").ConfigureAwait(false);

                    return;
                }

                try
                {
                    await MuteCommands.GetMuteRole(Context.Guild).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await Context.Channel.SendErrorAsync("⚠️ Failed creating a mute role. Give me ManageRoles permission" +
                                                         "or create 'nadeko-mute' role with disabled SendMessages and try again.")
                    .ConfigureAwait(false);

                    _log.Warn(ex);
                    return;
                }

                var stats = new AntiSpamStats
                {
                    AntiSpamSettings = new AntiSpamSetting()
                    {
                        Action           = action,
                        MessageThreshold = messageCount,
                    }
                };

                antiSpamGuilds.AddOrUpdate(Context.Guild.Id, stats, (key, old) => stats);

                using (var uow = DbHandler.UnitOfWork())
                {
                    var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting));

                    gc.AntiSpamSetting = stats.AntiSpamSettings;
                    await uow.CompleteAsync().ConfigureAwait(false);
                }

                await Context.Channel.SendConfirmAsync("Anti-Spam Enabled", $"{Context.User.Mention} {stats.ToString()}").ConfigureAwait(false);
            }
Ejemplo n.º 2
0
            public async Task AntiSpam(int messageCount, PunishmentAction action = PunishmentAction.Mute, int time = 0)
            {
                if (messageCount < 2 || messageCount > 10)
                {
                    return;
                }

                if (time < 0 || time > 60 * 12)
                {
                    return;
                }

                try
                {
                    await _mute.GetMuteRole(Context.Guild).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _log.Warn(ex);
                    await ReplyErrorLocalized("prot_error").ConfigureAwait(false);

                    return;
                }

                var stats = new AntiSpamStats
                {
                    AntiSpamSettings = new AntiSpamSetting()
                    {
                        Action           = action,
                        MessageThreshold = messageCount,
                        MuteTime         = time,
                    }
                };

                stats = _service.AntiSpamGuilds.AddOrUpdate(Context.Guild.Id, stats, (key, old) =>
                {
                    stats.AntiSpamSettings.IgnoredChannels = old.AntiSpamSettings.IgnoredChannels;
                    return(stats);
                });

                using (var uow = _db.UnitOfWork)
                {
                    var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting));

                    if (gc.AntiSpamSetting != null)
                    {
                        gc.AntiSpamSetting.Action           = stats.AntiSpamSettings.Action;
                        gc.AntiSpamSetting.MessageThreshold = stats.AntiSpamSettings.MessageThreshold;
                        gc.AntiSpamSetting.MuteTime         = stats.AntiSpamSettings.MuteTime;
                    }
                    else
                    {
                        gc.AntiSpamSetting = stats.AntiSpamSettings;
                    }
                    await uow.CompleteAsync().ConfigureAwait(false);
                }

                await Context.Channel.SendConfirmAsync(GetText("prot_enable", "Anti-Spam"), $"{Context.User.Mention} {GetAntiSpamString(stats)}").ConfigureAwait(false);
            }
Ejemplo n.º 3
0
            public async Task AntiSpam(int messageCount = 3, PunishmentAction action = PunishmentAction.Mute)
            {
                if (messageCount < 2 || messageCount > 10)
                {
                    return;
                }

                AntiSpamStats throwaway;

                if (_antiSpamGuilds.TryRemove(Context.Guild.Id, out throwaway))
                {
                    throwaway.UserStats.ForEach(x => x.Value.Dispose());
                    using (var uow = DbHandler.UnitOfWork())
                    {
                        var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting)
                                                      .ThenInclude(x => x.IgnoredChannels));

                        gc.AntiSpamSetting = null;
                        await uow.CompleteAsync().ConfigureAwait(false);
                    }
                    await ReplyConfirmLocalized("prot_disable", "Anti-Spam").ConfigureAwait(false);

                    return;
                }

                try
                {
                    await MuteCommands.GetMuteRole(Context.Guild).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    _log.Warn(ex);
                    await ReplyErrorLocalized("prot_error").ConfigureAwait(false);

                    return;
                }

                var stats = new AntiSpamStats
                {
                    AntiSpamSettings = new AntiSpamSetting()
                    {
                        Action           = action,
                        MessageThreshold = messageCount,
                    }
                };

                _antiSpamGuilds.AddOrUpdate(Context.Guild.Id, stats, (key, old) => stats);

                using (var uow = DbHandler.UnitOfWork())
                {
                    var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiSpamSetting));

                    gc.AntiSpamSetting = stats.AntiSpamSettings;
                    await uow.CompleteAsync().ConfigureAwait(false);
                }

                await Context.Channel.SendConfirmAsync(GetText("prot_enable", "Anti-Spam"), $"{Context.User.Mention} {GetAntiSpamString(stats)}").ConfigureAwait(false);
            }
Ejemplo n.º 4
0
            private string GetAntiSpamString(AntiSpamStats stats)
            {
                var ignoredString = string.Join(", ", stats.AntiSpamSettings.IgnoredChannels.Select(c => $"<#{c.ChannelId}>"));

                if (string.IsNullOrWhiteSpace(ignoredString))
                    ignoredString = "none";
                return GetText("spam_stats",
                        Format.Bold(stats.AntiSpamSettings.MessageThreshold.ToString()), 
                        Format.Bold(stats.AntiSpamSettings.Action.ToString()), 
                        ignoredString);
            }
Ejemplo n.º 5
0
        public async Task <AntiSpamStats> StartAntiSpamAsync(ulong guildId, int messageCount, PunishmentAction action,
                                                             int punishDurationMinutes, ulong?roleId)
        {
            var g = _client.GetGuild(guildId);
            await _mute.GetMuteRole(g).ConfigureAwait(false);

            if (!IsDurationAllowed(action))
            {
                punishDurationMinutes = 0;
            }

            var stats = new AntiSpamStats
            {
                AntiSpamSettings = new AntiSpamSetting()
                {
                    Action           = action,
                    MessageThreshold = messageCount,
                    MuteTime         = punishDurationMinutes,
                    RoleId           = roleId,
                }
            };

            stats = _antiSpamGuilds.AddOrUpdate(guildId, stats, (key, old) =>
            {
                stats.AntiSpamSettings.IgnoredChannels = old.AntiSpamSettings.IgnoredChannels;
                return(stats);
            });

            using (var uow = _db.GetDbContext())
            {
                var gc = uow.GuildConfigs.ForId(guildId, set => set.Include(x => x.AntiSpamSetting));

                if (gc.AntiSpamSetting != null)
                {
                    gc.AntiSpamSetting.Action           = stats.AntiSpamSettings.Action;
                    gc.AntiSpamSetting.MessageThreshold = stats.AntiSpamSettings.MessageThreshold;
                    gc.AntiSpamSetting.MuteTime         = stats.AntiSpamSettings.MuteTime;
                    gc.AntiSpamSetting.RoleId           = stats.AntiSpamSettings.RoleId;
                }
                else
                {
                    gc.AntiSpamSetting = stats.AntiSpamSettings;
                }
                await uow.SaveChangesAsync();
            }
            return(stats);
        }
Ejemplo n.º 6
0
            private string GetAntiSpamString(AntiSpamStats stats)
            {
                var ignoredString = string.Join(", ", stats.AntiSpamSettings.IgnoredChannels.Select(c => $"<#{c.ChannelId}>"));

                if (string.IsNullOrWhiteSpace(ignoredString))
                {
                    ignoredString = "none";
                }

                string add = "";

                if (stats.AntiSpamSettings.Action == PunishmentAction.Mute &&
                    stats.AntiSpamSettings.MuteTime > 0)
                {
                    add = " (" + stats.AntiSpamSettings.MuteTime + "s)";
                }

                return(GetText("spam_stats",
                               Format.Bold(stats.AntiSpamSettings.MessageThreshold.ToString()),
                               Format.Bold(stats.AntiSpamSettings.Action.ToString() + add),
                               ignoredString));
            }
Ejemplo n.º 7
0
        public async Task <AntiSpamStats> StartAntiSpamAsync(ulong guildId, int messageCount, int time, PunishmentAction action)
        {
            var g = _client.GetGuild(guildId);
            await _mute.GetMuteRole(g).ConfigureAwait(false);

            var stats = new AntiSpamStats
            {
                AntiSpamSettings = new AntiSpamSetting()
                {
                    Action           = action,
                    MessageThreshold = messageCount,
                    MuteTime         = time,
                }
            };

            stats = _antiSpamGuilds.AddOrUpdate(guildId, stats, (key, old) =>
            {
                stats.AntiSpamSettings.IgnoredChannels = old.AntiSpamSettings.IgnoredChannels;
                return(stats);
            });

            using (var uow = _db.UnitOfWork)
            {
                var gc = uow.GuildConfigs.ForId(guildId, set => set.Include(x => x.AntiSpamSetting));

                if (gc.AntiSpamSetting != null)
                {
                    gc.AntiSpamSetting.Action           = stats.AntiSpamSettings.Action;
                    gc.AntiSpamSetting.MessageThreshold = stats.AntiSpamSettings.MessageThreshold;
                    gc.AntiSpamSetting.MuteTime         = stats.AntiSpamSettings.MuteTime;
                }
                else
                {
                    gc.AntiSpamSetting = stats.AntiSpamSettings;
                }
                await uow.CompleteAsync().ConfigureAwait(false);
            }
            return(stats);
        }
Ejemplo n.º 8
0
            private string GetAntiSpamString(AntiSpamStats stats)
            {
                var settings      = stats.AntiSpamSettings;
                var ignoredString = string.Join(", ", settings.IgnoredChannels.Select(c => $"<#{c.ChannelId}>"));

                if (string.IsNullOrWhiteSpace(ignoredString))
                {
                    ignoredString = "none";
                }

                string add = "";

                if (settings.MuteTime > 0)
                {
                    add = $" ({TimeSpan.FromMinutes(settings.MuteTime):hh\\hmm\\m})";
                }

                return(GetText("spam_stats",
                               Format.Bold(settings.MessageThreshold.ToString()),
                               Format.Bold(settings.Action.ToString() + add),
                               ignoredString));
            }