Exemple #1
0
 private EmbedBuilder GetWarningIssuedEmbed(IUser user, Warning warning, WarningState state)
 {
     return((warning.Amount == 0
             ? GetWarningEmbed(user, warning)
             : AddWarningsToEmbed(GetWarningEmbed(user, warning), state))
            .WithTitle($"**{warning.Type}**"));
 }
Exemple #2
0
        private async Task RemoveWarningInternalAsync(IUser user, long id, string reason, bool force)
        {
            var warning = await Database.UNSAFE_GetWarningAsync(id);

            if (warning == null)
            {
                throw new Exception("Invalid warning ID.");
            }

            if (warning.UserId != user.Id) // this check exists to catch typos in warning IDs
            {
                throw new Exception("Warning ID does not match with user!");
            }

            if (warning.IssuerId != Context.User.Id && !force)
            {
                throw new Exception("Warning can only be deleted by its issuer, " +
                                    MentionUtils.MentionUser(warning.IssuerId) + ".");
            }

            await Database.UNSAFE_RemoveWarningAsync(id, Context.Message, reason);

            var state = WarningState.FromDatabase(await Database.UNSAFE_GetWarningsAsync(user.Id));
            var embed = AddWarningsToEmbed(GetWarningEmbed(user, warning), state)
                        .WithDescription("**A warning was deleted.**")
                        .Build();

            await AnnounceWarningEverywhereAsync(user, embed);
        }
Exemple #3
0
        private async Task <WarningState> ApplyWarningWithSeverity(IUser user, Warning[] history, Warning warning,
                                                                   Embed tempEmbed)
        {
            var state = WarningState.FromDatabase(history);

            warning.Amount = await RequestSeverity(user, history,
                                                   AddWarningsToEmbed(tempEmbed.ToEmbedBuilder(), state).Build());

            if (history.Contains(warning))
            {
                state = WarningState.FromDatabase(history);
            }
            else
            {
                state.Add(warning.Amount, warning.IssueDate);
            }

            if (state.MutedUntil.HasValue)
            {
                await MuteWatcher.MuteUser(
                    new Mute
                {
                    UserId     = user.Id,
                    IssuerId   = Context.Client.GetGuild(DiscordSettings.GuildId).CurrentUser.Id,
                    IssueDate  = DateTime.UtcNow,
                    ExpiryDate = state.MutedUntil.Value.UtcDateTime
                }, "You got a strike!", true, true);
            }
            else
            {
                await MuteWatcher.UnmuteUser(user.Id, Context.Client.GetGuild(DiscordSettings.GuildId).CurrentUser.Id);
            }

            return(state);
        }
Exemple #4
0
        public async Task UnmuteUser(IUser user, bool force)
        {
            var mute = await Database.UNSAFE_GetMute(user.Id);

            if (mute == null)
            {
                throw new Exception("User is currently not muted.");
            }

            if (mute.IssuerId != Context.User.Id && !force)
            {
                throw new Exception("Mute can only be removed by its issuer, " +
                                    MentionUtils.MentionUser(mute.IssuerId) + ".");
            }

            var state = WarningState.FromDatabase(await Database.UNSAFE_GetWarningsAsync(user.Id));

            if (!state.MutedUntil.HasValue)
            {
                await MuteWatcher.UnmuteUser(user.Id, null);
                await ReplyAsync($"{MentionUtils.MentionUser(user.Id)} unmuted.");
            }
            else if (state.MutedUntil.Value < mute.ExpiryDate)
            {
                var res = await MuteUser(Context.Client.CurrentUser, user, state.MutedUntil.Value, "Mute shortened.",
                                         true, false);
                await ReplyAsync($"{MentionUtils.MentionUser(user.Id)} muted until {res.ExpiryDate}. " +
                                 "A reduction of the mute duration beyond this point is not possible due to an active auto-mute from the warnings system.");
            }
            else
            {
                await ReplyAsync($"{MentionUtils.MentionUser(user.Id)} muted until {mute.ExpiryDate}. " +
                                 "A reduction of the mute duration beyond this point is not possible due to an active auto-mute from the warnings system.");
            }
        }
Exemple #5
0
            public static void Update(WarningState state, DateTimeOffset timestamp)
            {
                var duration = new Func <TimeSpan>(() => timestamp - state.LastTick);

                UpdateWarnings(state, duration);
                UpdateStrikes(state, duration);
                UpdateWarnings(state, duration);
            }
Exemple #6
0
 public static void UpdateWarnings(WarningState state, Func <TimeSpan> duration)
 {
     while (state.Warnings > 0 && duration().TotalDays >= GetWarningExpiryDays(state))
     {
         state.LastTick = state.LastTick.AddDays(WarningExpiryDays);
         state.Warnings--;
     }
 }
Exemple #7
0
 public static void UpdateStrikes(WarningState state, Func <TimeSpan> duration)
 {
     while (state.Strikes > 0 && duration().TotalDays >= StrikeExpiryDays)
     {
         state.LastTick = state.LastTick.AddDays(StrikeExpiryDays + ProbationDays);
         state.Warnings++;
         state.Strikes--;
     }
 }
Exemple #8
0
        public async Task WarnStats(IUser user = null)
        {
            if (user == null)
            {
                user = Context.User;
            }

            var state = WarningState.FromDatabase(await Database.UNSAFE_GetWarningsAsync(user.Id));
            var embed = AddWarningsToEmbed(new EmbedBuilder().WithAuthor(user), state).Build();

            await ReplyAsync(embed : embed);
        }
Exemple #9
0
        public static WarningState FromDatabase(Warning[] warnings)
        {
            var state = new WarningState();

            foreach (var item in warnings.Where(w => !w.RemoveDate.HasValue).Concat(DefaultWarnings)
                     .OrderBy(w => w.IssueDate))
            {
                state.Add(item.Amount, item.IssueDate);
            }

            state.Update(DateTimeOffset.UtcNow);
            return(state);
        }
Exemple #10
0
            public static void Update(WarningState state, DateTimeOffset timestamp)
            {
                var duration = new Func <TimeSpan>(() => timestamp - state.LastTick);

                while (state.Warnings > 0 && duration().TotalDays >= WarningExpiryDays)
                {
                    state.LastTick = state.LastTick.AddDays(WarningExpiryDays);
                    state.Warnings--;
                }

                while (state.Strikes > 0 && duration().TotalDays >= StrikeExpiryDays)
                {
                    state.LastTick = state.LastTick.AddDays(StrikeExpiryDays);
                    state.Strikes--;
                }
            }
Exemple #11
0
        private static EmbedBuilder AddWarningsToEmbed(EmbedBuilder embed, WarningState state)
        {
            if (state.Warnings > 0)
            {
                embed.AddField($"Warnings ({state.Warnings})", string.Concat(Enumerable.Repeat("⚠️ ", state.Warnings)),
                               true);
                embed.WithColor(Color.Orange);
            }

            if (state.Strikes > 0)
            {
                embed.AddField($"Strikes ({state.Strikes})", string.Concat(Enumerable.Repeat("❌ ", state.Strikes)),
                               true);
                embed.WithColor(Color.Red);
            }

            if (state.Warnings == 0 && state.Strikes == 0)
            {
                embed.AddField("Warnings (0)", "No warnings, well done!");
                embed.WithColor(Color.Green);
            }
            else if (state.MutedUntil.HasValue)
            {
                embed.AddField("Strike mute penalty expires in",
                               (state.MutedUntil.Value - DateTimeOffset.UtcNow).ToHumanReadableString());
            }
            else if (state.LastTick > DateTimeOffset.UtcNow)
            {
                embed.AddField("Probation ends in", (state.LastTick - DateTimeOffset.UtcNow).ToHumanReadableString());
            }
            else
            {
                var next     = state.Warnings == 0 ? "strike" : "warning";
                var duration = state.Warnings == 0
                    ? TimeSpan.FromDays(WarningState.WarningDecayLogicV3.StrikeExpiryDays)
                    : TimeSpan.FromDays(WarningState.WarningDecayLogicV3.GetWarningExpiryDays(state));
                embed.AddField($"Next {next} expires in",
                               (duration - (DateTimeOffset.UtcNow - state.LastTick)).ToHumanReadableString());
            }

            return(embed);
        }
Exemple #12
0
            public const int ProbationDays = WarningExpiryDays * 2;           // 18

            public static int GetWarningExpiryDays(WarningState state)
            {
                return(state.Strikes > 0 ? WarningExpiryDaysWithStrike : WarningExpiryDays);
            }