コード例 #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:
                    await guild.AddBanAsync(user, reason : "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
ファイル: ProtectionService.cs プロジェクト: Slamerz/DmC
        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).ConfigureAwait(false);
                        }
                        else
                        {
                            await _mute.TimedMute(gu, 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));

                    break;
                }
            }
            await OnAntiProtectionTriggered(action, pt, gus).ConfigureAwait(false);
        }