Updated court command to support multiple hearings

This commit is contained in:
barelyprofessional
2024-12-24 23:43:32 +13:00
parent 0ec8253bef
commit 938601bf78
4 changed files with 137 additions and 15 deletions

View File

@@ -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<Regex> Patterns => [
new Regex(@"^admin hearing add (?<case>\S+) (?<date>\S+) (?<description>.+)$")
];
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<List<CourtHearingModel>>();
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<Regex> Patterns => [
new Regex(@"^admin hearing remove (?<index>\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<List<CourtHearingModel>>();
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);
}
}

View File

@@ -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<List<CourtHearingModel>>();
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<CourtHearingModel> 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<Regex> 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);
}
}

View File

@@ -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; }
}

View File

@@ -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";
}
}