diff --git a/Net.Vatprc.Uniapi/Controllers/Discord/MetarModule.cs b/Net.Vatprc.Uniapi/Controllers/Discord/MetarModule.cs new file mode 100644 index 0000000..6eb9cbe --- /dev/null +++ b/Net.Vatprc.Uniapi/Controllers/Discord/MetarModule.cs @@ -0,0 +1,26 @@ +using System.Text.Json.Serialization; +using Discord.Interactions; +using Flurl.Http; +using Net.Vatprc.Uniapi.Services; + +namespace Net.Vatprc.Uniapi.Controllers.Discord; + +public class MetarModule(RudiMetarService MetarService) : InteractionModuleBase +{ + [SlashCommand("metar", "Get METAR for an airport")] + public async Task WhoAmIAsync(string icao) + { + if (icao.Length != 4) + { + await RespondAsync($"ICAO code must be 4 characters long, find {icao}"); + return; + } + var result = await MetarService.GetMetar(icao); + if (string.IsNullOrWhiteSpace(result)) + { + await RespondAsync($"No METAR found for airport {icao}"); + return; + } + await RespondAsync(result); + } +} diff --git a/Net.Vatprc.Uniapi/Program.cs b/Net.Vatprc.Uniapi/Program.cs index 6f700dc..20bc477 100644 --- a/Net.Vatprc.Uniapi/Program.cs +++ b/Net.Vatprc.Uniapi/Program.cs @@ -199,6 +199,8 @@ error message example. }); }); +RudiMetarService.ConfigureOn(builder); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Net.Vatprc.Uniapi/Services/DiscordWorker.cs b/Net.Vatprc.Uniapi/Services/DiscordWorker.cs index 766c097..252609f 100644 --- a/Net.Vatprc.Uniapi/Services/DiscordWorker.cs +++ b/Net.Vatprc.Uniapi/Services/DiscordWorker.cs @@ -46,7 +46,7 @@ public async Task StartAsync(CancellationToken ct) }, message.Message); return Task.CompletedTask; }; - await Interaction.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null); + await Interaction.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: ServiceProvider); Client.InteractionCreated += async (x) => { diff --git a/Net.Vatprc.Uniapi/Services/RudiMetarService.cs b/Net.Vatprc.Uniapi/Services/RudiMetarService.cs new file mode 100644 index 0000000..9528362 --- /dev/null +++ b/Net.Vatprc.Uniapi/Services/RudiMetarService.cs @@ -0,0 +1,30 @@ +using Flurl; +using Flurl.Http; +using Microsoft.Extensions.Options; + +namespace Net.Vatprc.Uniapi.Services; + +public class RudiMetarService(IOptions Options) +{ + public static WebApplicationBuilder ConfigureOn(WebApplicationBuilder builder) + { + builder.Services.Configure