From 411649c37719bb83a49919b5510d9498482d723f Mon Sep 17 00:00:00 2001 From: Elijah R Date: Thu, 17 Oct 2024 09:07:44 -0400 Subject: [PATCH] Add exception handling to /vnc, update for API changes --- CollabBot/DiscordCommands.cs | 14 ++++++++++++-- CollabBot/VNC.cs | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CollabBot/DiscordCommands.cs b/CollabBot/DiscordCommands.cs index 1fe3e44..5920576 100644 --- a/CollabBot/DiscordCommands.cs +++ b/CollabBot/DiscordCommands.cs @@ -11,6 +11,7 @@ public class DiscordCommands : ApplicationCommandModule public async Task VNC(InteractionContext ctx, [Option("country", "If specified, a random VNC from this country")] string? country = null) { + Console.WriteLine($"Getting random VNC for {ctx.User.Username} in {country ?? "any country"}"); await ctx.DeferAsync(); VNC vnc; using var http = new HttpClient(); @@ -45,10 +46,19 @@ public class DiscordCommands : ApplicationCommandModule else { var response = await http.GetStringAsync("https://computernewb.com/vncresolver/api/scans/vnc/random"); - vnc = JsonSerializer.Deserialize(response); + try + { + vnc = JsonSerializer.Deserialize(response); + } + catch (Exception ex) + { + await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"Failed to fetch VNC data ({ex.Message}). Please ping an admin!")); + return; + } } if (vnc == null) { + Console.WriteLine("Failed to fetch VNC data (response was null)"); await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Failed to fetch VNC data (response was null, please ping an admin!)")); return; } @@ -59,7 +69,7 @@ public class DiscordCommands : ApplicationCommandModule .AddField("Location", $"{vnc.city}, {vnc.country} :flag_{vnc.country.ToLower()}:") .AddField("ASN", vnc.asn) .WithImageUrl($"https://computernewb.com/vncresolver/api/scans/vnc/screenshot/{vnc.id}.jpeg") - .WithFooter($"Added to database on {vnc.createdat}"); + .WithFooter($"Added to database on {DateTime.UnixEpoch.AddMilliseconds(vnc.createdat):u}"); if (!String.IsNullOrEmpty(vnc.clientname)) embed.AddField("Client", vnc.clientname); if (!String.IsNullOrEmpty(vnc.screenres)) embed.AddField("Screen Resolution", vnc.screenres); if (!String.IsNullOrEmpty(vnc.hostname)) embed.AddField("Hostname", vnc.hostname); diff --git a/CollabBot/VNC.cs b/CollabBot/VNC.cs index f7027db..ade3323 100644 --- a/CollabBot/VNC.cs +++ b/CollabBot/VNC.cs @@ -5,16 +5,16 @@ public class VNC public int id { get; set; } public string ip { get; set; } public int port { get; set; } - public string city { get; set; } - public string state { get; set; } + public string? city { get; set; } + public string? state { get; set; } public string country { get; set; } public string asn { get; set; } - public string clientname { get; set; } - public string screenres { get; set; } - public string hostname { get; set; } - public string osname { get; set; } - public string openports { get; set; } - public string username { get; set; } - public string password { get; set; } - public string createdat { get; set; } + public string? clientname { get; set; } + public string? screenres { get; set; } + public string? hostname { get; set; } + public string? osname { get; set; } + public string? openports { get; set; } + public string? username { get; set; } + public string? password { get; set; } + public Int64 createdat { get; set; } } \ No newline at end of file