diff --git a/CollabBot/DiscordCommands.cs b/CollabBot/DiscordCommands.cs index 5920576..225a940 100644 --- a/CollabBot/DiscordCommands.cs +++ b/CollabBot/DiscordCommands.cs @@ -9,14 +9,29 @@ public class DiscordCommands : ApplicationCommandModule { [SlashCommand("vnc", "View insecure VNCs around the world!")] public async Task VNC(InteractionContext ctx, - [Option("country", "If specified, a random VNC from this country")] string? country = null) + [Option("country", "If specified, a random VNC from this country")] string? country = null, + [Option("id", "If specified, a specific VNC by ID")] long? id = null) { - Console.WriteLine($"Getting random VNC for {ctx.User.Username} in {country ?? "any country"}"); await ctx.DeferAsync(); VNC vnc; using var http = new HttpClient(); - if (country != null) + if (id != null) { + Console.WriteLine($"Getting VNC ID {id} for {ctx.User.Username}"); + try + { + var response = await http.GetStringAsync($"https://computernewb.com/vncresolver/api/scans/vnc/id/{id}"); + 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; + } + } + else if (country != null) + { + Console.WriteLine($"Getting VNC from {country} for {ctx.User.Username}"); if (!Regex.IsMatch(country, "^[a-zA-Z]{2}$")) { await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent( @@ -39,15 +54,10 @@ public class DiscordCommands : ApplicationCommandModule return; } var random = new Random().Next(results.count); - response = await http.GetStringAsync( - $"https://computernewb.com/vncresolver/api/scans/vnc/id/{results.result[random]}"); - vnc = JsonSerializer.Deserialize(response); - } - else - { - var response = await http.GetStringAsync("https://computernewb.com/vncresolver/api/scans/vnc/random"); try { + response = await http.GetStringAsync( + $"https://computernewb.com/vncresolver/api/scans/vnc/id/{results.result[random]}"); vnc = JsonSerializer.Deserialize(response); } catch (Exception ex) @@ -56,12 +66,33 @@ public class DiscordCommands : ApplicationCommandModule return; } } + else + { + Console.WriteLine($"Getting random VNC for {ctx.User.Username}"); + try + { + var response = await http.GetStringAsync("https://computernewb.com/vncresolver/api/scans/vnc/random"); + 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; } + if (vnc.error != null) + { + Console.WriteLine($"Error: {vnc.error}"); + await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent(vnc.error)); + return; + } var embed = new DiscordEmbedBuilder() .WithTitle($"VNC {vnc.id}").WithUrl($"https://computernewb.com/vncresolver/browse/#id/{vnc.id}") diff --git a/CollabBot/VNC.cs b/CollabBot/VNC.cs index ade3323..a05091d 100644 --- a/CollabBot/VNC.cs +++ b/CollabBot/VNC.cs @@ -2,6 +2,7 @@ namespace CollabBot; public class VNC { + public string? error { get; set; } public int id { get; set; } public string ip { get; set; } public int port { get; set; }