From 938601bf78fc2489c8b95bc8f07b5137f7e1e291 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Tue, 24 Dec 2024 23:43:32 +1300 Subject: [PATCH] Updated court command to support multiple hearings --- KfChatDotNetBot/Commands/AdminCommands.cs | 61 ++++++++++++++++++++ KfChatDotNetBot/Commands/MemeCommands.cs | 64 ++++++++++++++++----- KfChatDotNetBot/Models/BotServicesModels.cs | 7 +++ KfChatDotNetBot/Settings/BuiltIn.cs | 20 +++++++ 4 files changed, 137 insertions(+), 15 deletions(-) diff --git a/KfChatDotNetBot/Commands/AdminCommands.cs b/KfChatDotNetBot/Commands/AdminCommands.cs index 448c1f6..cbdad68 100644 --- a/KfChatDotNetBot/Commands/AdminCommands.cs +++ b/KfChatDotNetBot/Commands/AdminCommands.cs @@ -327,4 +327,65 @@ public class ReconnectKickCommand : ICommand botInstance.BotServices.KickClient.Disconnect(); await botInstance.SendChatMessageAsync("Disconnected from Kick. Client should reconnect shortly.", true); } +} + +public class AddCourtHearingCommand : ICommand +{ + public List Patterns => [ + new Regex(@"^admin hearing add (?\S+) (?\S+) (?.+)$") + ]; + + public string? HelpText => "Add a court hearing to the bot's calendar"; + public UserRight RequiredRight => UserRight.TrueAndHonest; + public TimeSpan Timeout => TimeSpan.FromSeconds(10); + public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) + { + var hearings = (await Helpers.GetValue(BuiltIn.Keys.BotCourtCalendar)).JsonDeserialize>(); + if (hearings == null) + { + await botInstance.SendChatMessageAsync("Hearings list was null", true); + return; + } + var caseNumber = arguments["case"].Value; + if (!DateTimeOffset.TryParse(arguments["date"].Value, out var date)) + { + await botInstance.SendChatMessageAsync("Failed to parse date", true); + return; + } + + hearings.Add(new CourtHearingModel {CaseNumber = caseNumber, Description = arguments["description"].Value, Time = date}); + await Helpers.SetValueAsJsonObject(BuiltIn.Keys.BotCourtCalendar, hearings); + await botInstance.SendChatMessageAsync("Updated list of hearings", true); + } +} + +public class RemoveCourtHearingCommand : ICommand +{ + public List Patterns => [ + new Regex(@"^admin hearing remove (?\d+)$") + ]; + + public string? HelpText => "Remove a hearing from the bot's calendar"; + public UserRight RequiredRight => UserRight.TrueAndHonest; + public TimeSpan Timeout => TimeSpan.FromSeconds(10); + public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) + { + var hearings = (await Helpers.GetValue(BuiltIn.Keys.BotCourtCalendar)).JsonDeserialize>(); + if (hearings == null) + { + await botInstance.SendChatMessageAsync("Hearings list was null", true); + return; + } + var hearingIndex = Convert.ToInt32(arguments["index"].Value); + if (hearings.Count < hearingIndex) + { + await botInstance.SendChatMessageAsync( + $"Index supplied is out of range. There are only {hearings.Count} hearings in the database", true); + return; + } + + hearings.RemoveAt(hearingIndex + 1); + await Helpers.SetValueAsJsonObject(BuiltIn.Keys.BotCourtCalendar, hearings); + await botInstance.SendChatMessageAsync("Updated list of hearings", true); + } } \ No newline at end of file diff --git a/KfChatDotNetBot/Commands/MemeCommands.cs b/KfChatDotNetBot/Commands/MemeCommands.cs index 846999c..44b02cc 100644 --- a/KfChatDotNetBot/Commands/MemeCommands.cs +++ b/KfChatDotNetBot/Commands/MemeCommands.cs @@ -254,22 +254,20 @@ public class NextCourtHearingCommand : ICommand public TimeSpan Timeout => TimeSpan.FromSeconds(120); public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) { - var time = await Helpers.GetValue(BuiltIn.Keys.BotNextCourtHearing); - if (time.Value == null) + var hearings = (await Helpers.GetValue(BuiltIn.Keys.BotCourtCalendar)).JsonDeserialize>(); + if (hearings == null) { - await botInstance.SendChatMessageAsync("There is no next court hearing :(", true); - return; - } - var timespan = DateTimeOffset.Parse(time.Value) - DateTimeOffset.UtcNow; - if (timespan.TotalSeconds < 0) - { - await botInstance.SendChatMessageAsync("It's over", true); + await botInstance.SendChatMessageAsync("Caught a null when grabbing hearings", true); return; } - var sent = await botInstance.SendChatMessageAsync( - $"Bossman's next court hearing will be in {timespan.Humanize(precision: 10, minUnit: TimeUnit.Millisecond)}", - true); + if (RenderHearings(hearings) == string.Empty) + { + await botInstance.SendChatMessageAsync("There are no upcoming hearings in the bot's calendar", true); + return; + } + + var sent = await botInstance.SendChatMessageAsync(RenderHearings(hearings),true); while (sent.Status != SentMessageTrackerStatus.ResponseReceived) { await Task.Delay(250, ctx); @@ -278,10 +276,46 @@ public class NextCourtHearingCommand : ICommand while (i < 60) { await Task.Delay(1000, ctx); - timespan = DateTimeOffset.Parse(time.Value) - DateTimeOffset.UtcNow; - await botInstance.KfClient.EditMessageAsync(sent.ChatMessageId!.Value, - $"Bossman's next court hearing will be in {timespan.Humanize(precision: 10, minUnit: TimeUnit.Millisecond)}"); + await botInstance.KfClient.EditMessageAsync(sent.ChatMessageId!.Value, RenderHearings(hearings)); i++; } } + + private static string RenderHearings(List hearings) + { + var i = 0; + var result = string.Empty; + foreach (var hearing in hearings) + { + i++; + var timespan = hearing.Time - DateTimeOffset.UtcNow; + if (timespan.TotalSeconds < 0) continue; // Already passed + result += + //$"{i}: [url=https://eapps.courts.state.va.us/ocis/details;fromOcis=true;fullcaseNumber=109{hearing.CaseNumber.Replace("-", string.Empty)}]{hearing.CaseNumber}[/url] " + + $"{i}: {hearing.CaseNumber} " + + $"{hearing.Description} will be heard in {timespan.Humanize(precision: 10, minUnit: TimeUnit.Second, maxUnit: TimeUnit.Year)}\r\n"; + } + return result.Trim(); + } +} + +public class JailCommand : ICommand +{ + public List Patterns => [ + new Regex("^jail") + ]; + public string? HelpText => "How long has Bossman been in jail?"; + public UserRight RequiredRight => UserRight.Loser; + public TimeSpan Timeout => TimeSpan.FromSeconds(10); + public async Task RunCommand(ChatBot botInstance, MessageModel message, UserDbModel user, GroupCollection arguments, CancellationToken ctx) + { + var start = await Helpers.GetValue(BuiltIn.Keys.BotJailStartTime); + if (start.Value == null) + { + await botInstance.SendChatMessageAsync("He ain't in jail nigga", true); + return; + } + var timespan = DateTimeOffset.UtcNow - DateTimeOffset.Parse(start.Value); + await botInstance.SendChatMessageAsync($"Bossman has been in jail {timespan.Humanize(precision:5)}", true); + } } \ No newline at end of file diff --git a/KfChatDotNetBot/Models/BotServicesModels.cs b/KfChatDotNetBot/Models/BotServicesModels.cs index 071d16b..7eedf25 100644 --- a/KfChatDotNetBot/Models/BotServicesModels.cs +++ b/KfChatDotNetBot/Models/BotServicesModels.cs @@ -5,4 +5,11 @@ public class KickChannelModel public required int ChannelId { get; set; } public required int ForumId { get; set; } public required string ChannelSlug { get; set; } +} + +public class CourtHearingModel +{ + public required string Description { get; set; } + public required DateTimeOffset Time { get; set; } + public required string CaseNumber { get; set; } } \ No newline at end of file diff --git a/KfChatDotNetBot/Settings/BuiltIn.cs b/KfChatDotNetBot/Settings/BuiltIn.cs index fa1dae5..8ac437c 100644 --- a/KfChatDotNetBot/Settings/BuiltIn.cs +++ b/KfChatDotNetBot/Settings/BuiltIn.cs @@ -607,6 +607,24 @@ public static class BuiltIn Default = "2024-10-29T09:00:00-04:00", IsSecret = false, CacheDuration = TimeSpan.FromHours(1) + }, + new BuiltInSettingsModel + { + Key = Keys.BotJailStartTime, + Regex = ".+", + Description = "ISO8601 date of when Bossman's incarceration began", + Default = "2024-10-27T03:25:00-05:00", + IsSecret = false, + CacheDuration = TimeSpan.FromHours(1) + }, + new BuiltInSettingsModel + { + Key = Keys.BotCourtCalendar, + Regex = ".+", + Description = "JSON array containing court hearings", + Default = "[]", + IsSecret = false, + CacheDuration = TimeSpan.Zero } ]; @@ -666,5 +684,7 @@ public static class BuiltIn public static string BotRehabEndTime = "Bot.Rehab.EndTime"; public static string BotPoNextVisit = "Bot.Po.NextVisit"; public static string BotNextCourtHearing = "Bot.Court.NextHearing"; + public static string BotJailStartTime = "Bot.Jail.StartTime"; + public static string BotCourtCalendar = "Bot.Court.Calendar"; } } \ No newline at end of file