public override Task <TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
 {
     try {
         return(Task.FromResult(TypeReaderResult.FromSuccess(ModerationPoints.FromString(input))));
     } catch (ArgumentException e) {
         return(Task.FromResult(TypeReaderResult.FromError(e)));
     }
 }
            public async Task Warn(IGuildUser user, ModerationPoints points, [Remainder] string reason = null)
            {
                if (Context.User.Id == user.Guild.OwnerId || user.GetRoles().Where(r => r.IsHoisted).Select(r => r.Position).FallbackIfEmpty(int.MinValue).Max() < ((IGuildUser)Context.User).GetRoles().Where(r => r.IsHoisted).Select(r => r.Position).FallbackIfEmpty(int.MinValue).Max())
                {
                    try {
                        await(await user.CreateDMChannelAsync()).EmbedAsync(new EmbedBuilder().WithErrorColor().WithDescription(GetText("userpunish_warn_warned_on_server", Context.Guild.ToString())).AddField(efb => efb.WithName(GetText("userpunish_warn_reason")).WithValue(reason ?? "-"))).ConfigureAwait(false);
                    } catch { }

                    var punishment = await Service.Warn(Context.Guild, user.Id, Context.User.ToString(), reason, points).ConfigureAwait(false);

                    if (punishment == null)
                    {
                        await ConfirmLocalized("userpunish_warn_user_warned", Format.Bold(user.ToString())).ConfigureAwait(false);
                    }
                    else
                    {
                        await ConfirmLocalized("userpunish_warn_user_warned_and_punished", Format.Bold(user.ToString()), Format.Bold(punishment.ToString())).ConfigureAwait(false);
                    }
                }
                else
                {
                    await ErrorLocalized("userpunish_warn_hierarchy").ConfigureAwait(false);
                }
            }
Пример #3
0
        public async Task <PunishmentAction?> Warn(IGuild guild, ulong userId, string modName, string reason, ModerationPoints points)
        {
            if (string.IsNullOrWhiteSpace(reason))
            {
                reason = "-";
            }

            var guildId = guild.Id;

            var warn = new Warning {
                UserId       = userId,
                GuildId      = guildId,
                Forgiven     = false,
                Reason       = reason,
                Moderator    = modName,
                PointsHard   = points.PointsHard,
                PointsMedium = points.PointsMedium,
                PointsLight  = points.PointsLight,
            };

            var 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)
                            .OrderByDescending(w => w.DateAdded)
                            .Count(w => !w.Forgiven && w.UserId == userId);

                uow.Warnings.Add(warn);

                uow.SaveChanges();
            }

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

            if (p == null)
            {
                return(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);
        }