예제 #1
0
        public async Task <PunishmentAction?> Warn(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 = _db.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 _mute.MuteUser(user).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedMute(user, TimeSpan.FromMinutes(p.Time)).ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.Kick:
                    await user.KickAsync("Warned too many times.").ConfigureAwait(false);

                    break;

                case PunishmentAction.Ban:
                    if (p.Time == 0)
                    {
                        await guild.AddBanAsync(user, reason : "Warned too many times.").ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedBan(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.").ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.Softban:
                    await guild.AddBanAsync(user, 7, reason : "Warned too many times").ConfigureAwait(false);

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

                case PunishmentAction.RemoveRoles:
                    await user.RemoveRolesAsync(user.GetRoles().Where(x => x.Id != guild.EveryoneRole.Id));

                    break;

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

            return(null);
        }
예제 #2
0
        public async Task <WarningPunishment> Warn(IGuild guild, ulong userId, IUser mod, string reason)
        {
            var modName = mod.ToString();

            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 = _db.GetDbContext())
            {
                ps = uow.GuildConfigs.ForId(guildId, set => set.Include(x => x.WarnPunishments))
                     .WarnPunishments;

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

                uow.Warnings.Add(warn);

                uow.SaveChanges();
            }

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

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

                if (user == null)
                {
                    return(null);
                }

                var muteReason = "Warning punishment - " + reason;
                switch (p.Punishment)
                {
                case PunishmentAction.Mute:
                    if (p.Time == 0)
                    {
                        await _mute.MuteUser(user, mod, reason : muteReason).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), reason : muteReason).ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.VoiceMute:
                    if (p.Time == 0)
                    {
                        await _mute.MuteUser(user, mod, MuteType.Voice, muteReason).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), MuteType.Voice, muteReason).ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.ChatMute:
                    if (p.Time == 0)
                    {
                        await _mute.MuteUser(user, mod, MuteType.Chat, muteReason).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), MuteType.Chat, muteReason).ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.Kick:
                    await user.KickAsync("Warned too many times.").ConfigureAwait(false);

                    break;

                case PunishmentAction.Ban:
                    if (p.Time == 0)
                    {
                        await guild.AddBanAsync(user, reason : "Warned too many times.").ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedBan(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.").ConfigureAwait(false);
                    }
                    break;

                case PunishmentAction.Softban:
                    await guild.AddBanAsync(user, 7, reason : "Softban | Warned too many times").ConfigureAwait(false);

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

                case PunishmentAction.RemoveRoles:
                    await user.RemoveRolesAsync(user.GetRoles().Where(x => x.Id != guild.EveryoneRole.Id)).ConfigureAwait(false);

                    break;

                case PunishmentAction.AddRole:
                    var role = guild.GetRole(p.RoleId.Value);
                    if (!(role is null))
                    {
                        if (p.Time == 0)
                        {
                            await user.AddRoleAsync(role).ConfigureAwait(false);
                        }
                        else
                        {
                            await _mute.TimedRole(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.", role).ConfigureAwait(false);
                        }
                    }
                    else
                    {
                        _log.Warn($"Warnpunish can't find role {p.RoleId.Value} on server {guild.Id}");
                    }
                    break;
예제 #3
0
        private async Task PunishUsers(PunishmentAction action, ProtectionType pt, int muteTime, 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
                    {
                        if (muteTime <= 0)
                        {
                            await _mute.MuteUser(gu, _client.CurrentUser).ConfigureAwait(false);
                        }
                        else
                        {
                            await _mute.TimedMute(gu, _client.CurrentUser, TimeSpan.FromSeconds(muteTime)).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;

                case PunishmentAction.RemoveRoles:
                    await gu.RemoveRolesAsync(gu.GetRoles().Where(x => x.Id != gu.Guild.EveryoneRole.Id)).ConfigureAwait(false);

                    break;
                }
            }
            await OnAntiProtectionTriggered(action, pt, gus).ConfigureAwait(false);
        }
예제 #4
0
        public async Task ApplyPunishment(IGuild guild, IGuildUser user, IUser mod, PunishmentAction p, int minutes,
                                          ulong?roleId, string reason)
        {
            switch (p)
            {
            case PunishmentAction.Mute:
                if (minutes == 0)
                {
                    await _mute.MuteUser(user, mod, reason : reason).ConfigureAwait(false);
                }
                else
                {
                    await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), reason : reason)
                    .ConfigureAwait(false);
                }
                break;

            case PunishmentAction.VoiceMute:
                if (minutes == 0)
                {
                    await _mute.MuteUser(user, mod, MuteType.Voice, reason).ConfigureAwait(false);
                }
                else
                {
                    await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Voice, reason)
                    .ConfigureAwait(false);
                }
                break;

            case PunishmentAction.ChatMute:
                if (minutes == 0)
                {
                    await _mute.MuteUser(user, mod, MuteType.Chat, reason).ConfigureAwait(false);
                }
                else
                {
                    await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Chat, reason)
                    .ConfigureAwait(false);
                }
                break;

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

                break;

            case PunishmentAction.Ban:
                if (minutes == 0)
                {
                    await guild.AddBanAsync(user, reason : reason).ConfigureAwait(false);
                }
                else
                {
                    await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason)
                    .ConfigureAwait(false);
                }
                break;

            case PunishmentAction.Softban:
                await guild.AddBanAsync(user, 7, reason : $"Softban | {reason}").ConfigureAwait(false);

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

                break;

            case PunishmentAction.RemoveRoles:
                await user.RemoveRolesAsync(user.GetRoles().Where(x => !x.IsManaged && x != x.Guild.EveryoneRole))
                .ConfigureAwait(false);

                break;

            case PunishmentAction.AddRole:
                if (roleId is null)
                {
                    return;
                }
                var role = guild.GetRole(roleId.Value);
                if (!(role is null))
                {
                    if (minutes == 0)
                    {
                        await user.AddRoleAsync(role).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.TimedRole(user, TimeSpan.FromMinutes(minutes), reason, role)
                        .ConfigureAwait(false);
                    }
                }
                else
                {
                    Log.Warning($"Can't find role {roleId.Value} on server {guild.Id} to apply punishment.");
                }

                break;
예제 #5
0
        public async Task <PunishmentAction?> Warn(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 = _db.UnitOfWork)
            {
                ps = uow.GuildConfigs.For(guildId, set => set.Include(x => x.WarnPunishments))
                     .WarnPunishments;
                if (TimeSpan.TryParse(uow.GuildConfigs.For(guildId).WarnExpiry, out TimeSpan expiry))
                {
                    warnings += uow.Warnings
                                .For(guildId, userId)
                                .Where(w => !w.Forgiven && w.UserId == userId &&
                                       (DateTime.UtcNow - w.DateAdded) < expiry)
                                .Count();
                }
                else
                {
                    warnings += uow.Warnings
                                .For(guildId, userId)
                                .Where(w => !w.Forgiven && w.UserId == userId &&
                                       (DateTime.UtcNow - w.DateAdded).Value.TotalDays < 14)
                                .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 _mute.MuteUser(user).ConfigureAwait(false);
                    }
                    else
                    {
                        await _mute.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);
        }