Пример #1
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);
            }
Пример #2
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);
            }