Add exception handling to /vnc, update for API changes

This commit is contained in:
Elijah R 2024-10-17 09:07:44 -04:00
parent 98b7bbff69
commit 411649c377
2 changed files with 22 additions and 12 deletions

View file

@ -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");
try
{
vnc = JsonSerializer.Deserialize<VNC>(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);

View file

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