コード例 #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);
            }
コード例 #2
0
ファイル: ProtectionCommands.cs プロジェクト: Aryan10/Bot
            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);
            }
コード例 #3
0
            private static async Task PunishUsers(PunishmentAction action, ProtectionType pt, params IGuildUser[] gus)
            {
                _log.Info($"[{pt}] - Punishing [{gus.Length}] users with [{action}] in {gus[0].Guild.Name} guild");
                foreach (var gu in gus)
                {
                    switch (action)
                    {
                    case PunishmentAction.Mute:
                        try
                        {
                            await MuteCommands.MuteUser(gu).ConfigureAwait(false);
                        }
                        catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); }
                        break;

                    case PunishmentAction.Kick:
                        try
                        {
                            await gu.KickAsync().ConfigureAwait(false);
                        }
                        catch (Exception ex) { _log.Warn(ex, "I can't apply punishement"); }
                        break;

                    case PunishmentAction.Softban:
                        try
                        {
                            await gu.Guild.AddBanAsync(gu, 7).ConfigureAwait(false);

                            try
                            {
                                await gu.Guild.RemoveBanAsync(gu).ConfigureAwait(false);
                            }
                            catch
                            {
                                await gu.Guild.RemoveBanAsync(gu).ConfigureAwait(false);

                                // try it twice, really don't want to ban user if
                                // only kick has been specified as the punishement
                            }
                        }
                        catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); }
                        break;

                    case PunishmentAction.Ban:
                        try
                        {
                            await gu.Guild.AddBanAsync(gu, 7).ConfigureAwait(false);
                        }
                        catch (Exception ex) { _log.Warn(ex, "I can't apply punishment"); }
                        break;
                    }
                }
                await LogCommands.TriggeredAntiProtection(gus, action, pt).ConfigureAwait(false);
            }
コード例 #4
0
            public async Task AntiRaid(IUserMessage imsg, int userThreshold, int seconds, PunishmentAction action)
            {
                var channel = (ITextChannel)imsg.Channel;

                if (userThreshold < 2 || userThreshold > 30)
                {
                    await channel.SendErrorAsync("❗️User threshold must be between **2** and **30**.").ConfigureAwait(false);

                    return;
                }

                if (seconds < 2 || seconds > 300)
                {
                    await channel.SendErrorAsync("❗️Time must be between **2** and **300** seconds.").ConfigureAwait(false);

                    return;
                }

                try
                {
                    await MuteCommands.GetMuteRole(channel.Guild).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await channel.SendConfirmAsync("⚠️ 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 setting = new AntiRaidSetting()
                {
                    Action        = action,
                    Seconds       = seconds,
                    UserThreshold = userThreshold,
                };

                antiRaidGuilds.AddOrUpdate(channel.Guild.Id, setting, (id, old) => setting);

                await channel.SendConfirmAsync($"ℹ️ {imsg.Author.Mention} If **{userThreshold}** or more users join within **{seconds}** seconds, I will **{action}** them.")
                .ConfigureAwait(false);
            }
コード例 #5
0
            public async Task AntiSpam(IUserMessage imsg, int messageCount = 3, PunishmentAction action = PunishmentAction.Mute)
            {
                var channel = (ITextChannel)imsg.Channel;

                if (messageCount < 2 || messageCount > 10)
                {
                    return;
                }

                AntiSpamSetting throwaway;

                if (antiSpamGuilds.TryRemove(channel.Guild.Id, out throwaway))
                {
                    await channel.SendConfirmAsync("🆗 **Anti-Spam feature** has been **disabled** on this server.").ConfigureAwait(false);
                }
                else
                {
                    try
                    {
                        await MuteCommands.GetMuteRole(channel.Guild).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        await 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;
                    }

                    if (antiSpamGuilds.TryAdd(channel.Guild.Id, new AntiSpamSetting()
                    {
                        Action = action,
                        MessageThreshold = messageCount,
                    }))
                    {
                        await channel.SendConfirmAsync("✅ **Anti-Spam feature** has been **enabled** on this server.").ConfigureAwait(false);
                    }
                }
            }
コード例 #6
0
            private async Task <PunishmentAction?> InternalWarn(IGuild guild, ulong userId, string modName, string reason)
            {
                if (string.IsNullOrWhiteSpace(reason))
                {
                    reason = "-";
                }

                var guildId = guild.Id;

                var warn = new Warning()
                {
                    UserId    = userId,
                    GuildId   = guildId,
                    Forgiven  = false,
                    Reason    = reason,
                    Moderator = modName,
                };

                int warnings = 1;
                List <WarningPunishment> ps;

                using (var uow = DbHandler.UnitOfWork())
                {
                    ps = uow.GuildConfigs.For(guildId, set => set.Include(x => x.WarnPunishments))
                         .WarnPunishments;

                    warnings += uow.Warnings
                                .For(guildId, userId)
                                .Where(w => !w.Forgiven && w.UserId == userId)
                                .Count();

                    uow.Warnings.Add(warn);

                    uow.Complete();
                }

                var p = ps.FirstOrDefault(x => x.Count == warnings);

                if (p != null)
                {
                    var user = await guild.GetUserAsync(userId);

                    if (user == null)
                    {
                        return(null);
                    }
                    switch (p.Punishment)
                    {
                    case PunishmentAction.Mute:
                        if (p.Time == 0)
                        {
                            await MuteCommands.MuteUser(user).ConfigureAwait(false);
                        }
                        else
                        {
                            await MuteCommands.TimedMute(user, TimeSpan.FromMinutes(p.Time)).ConfigureAwait(false);
                        }
                        break;

                    case PunishmentAction.Kick:
                        await user.KickAsync().ConfigureAwait(false);

                        break;

                    case PunishmentAction.Ban:
                        await guild.AddBanAsync(user).ConfigureAwait(false);

                        break;

                    case PunishmentAction.Softban:
                        await guild.AddBanAsync(user, 7).ConfigureAwait(false);

                        try
                        {
                            await guild.RemoveBanAsync(user).ConfigureAwait(false);
                        }
                        catch
                        {
                            await guild.RemoveBanAsync(user).ConfigureAwait(false);
                        }
                        break;

                    default:
                        break;
                    }
                    return(p.Punishment);
                }

                return(null);
            }
コード例 #7
0
            public async Task AntiRaid(int userThreshold = 5, int seconds = 10, PunishmentAction action = PunishmentAction.Mute)
            {
                if (userThreshold < 2 || userThreshold > 30)
                {
                    await Context.Channel.SendErrorAsync("❗️User threshold must be between **2** and **30**.").ConfigureAwait(false);

                    return;
                }

                if (seconds < 2 || seconds > 300)
                {
                    await Context.Channel.SendErrorAsync("❗️Time must be between **2** and **300** seconds.").ConfigureAwait(false);

                    return;
                }

                AntiRaidStats throwaway;

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

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

                    return;
                }

                try
                {
                    await MuteCommands.GetMuteRole(Context.Guild).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await Context.Channel.SendConfirmAsync("⚠️ 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 AntiRaidStats()
                {
                    AntiRaidSettings = new AntiRaidSetting()
                    {
                        Action        = action,
                        Seconds       = seconds,
                        UserThreshold = userThreshold,
                    }
                };

                antiRaidGuilds.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.AntiRaidSetting));

                    gc.AntiRaidSetting = stats.AntiRaidSettings;
                    await uow.CompleteAsync().ConfigureAwait(false);
                }

                await Context.Channel.SendConfirmAsync("Anti-Raid Enabled", $"{Context.User.Mention} {stats.ToString()}")
                .ConfigureAwait(false);
            }
コード例 #8
0
ファイル: ProtectionCommands.cs プロジェクト: KoshOTP/Taiga
            public async Task AntiRaid(int userThreshold = 5, int seconds = 10, PunishmentAction action = PunishmentAction.Mute)
            {
                if (userThreshold < 2 || userThreshold > 30)
                {
                    await ReplyErrorLocalized("raid_cnt", 2, 30).ConfigureAwait(false);
                    return;
                }

                if (seconds < 2 || seconds > 300)
                {
                    await ReplyErrorLocalized("raid_time", 2, 300).ConfigureAwait(false);
                    return;
                }

                AntiRaidStats throwaway;
                if (_antiRaidGuilds.TryRemove(Context.Guild.Id, out throwaway))
                {
                    using (var uow = DbHandler.UnitOfWork())
                    {
                        var gc = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(x => x.AntiRaidSetting));

                        gc.AntiRaidSetting = null;
                        await uow.CompleteAsync().ConfigureAwait(false);
                    }
                    await ReplyConfirmLocalized("prot_disable", "Anti-Raid").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 AntiRaidStats()
                {
                    AntiRaidSettings = new AntiRaidSetting()
                    {
                        Action = action,
                        Seconds = seconds,
                        UserThreshold = userThreshold,
                    }
                };

                _antiRaidGuilds.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.AntiRaidSetting));

                    gc.AntiRaidSetting = stats.AntiRaidSettings;
                    await uow.CompleteAsync().ConfigureAwait(false);
                }

                await Context.Channel.SendConfirmAsync(GetText("prot_enable", "Anti-Raid"), $"{Context.User.Mention} {GetAntiRaidString(stats)}")
                        .ConfigureAwait(false);
            }