Ejemplo n.º 1
0
        public static async Task <AvwxMainModel> GetAvwxMetarTaskAsync(string icao)
        {
            try
            {
                using var httpClient = new HttpClient { BaseAddress = new Uri("https://avwx.rest/api/") };
                if (!httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization",
                                                                              ApiKeysHelper.ApiKeysModelDeserialized().AvwxToken))
                {
                    return(null);
                }
                using var response = await httpClient.GetAsync(@$ "metar/{icao}");

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    File.Delete(Program.ApiKeysPath);
                    Console.WriteLine(
                        @"There was an error with your Avwx authorization token. To streamline its correction, both Discord Bot and Avwx keys have been deleted and the application will now close. When you next restart the application, you will be provided an opportunity to enter the corrected API keys.");
                    Environment.Exit(0);
                }

                var responseData = await response.Content.ReadAsStringAsync();

                return(JsonSerializer.Deserialize <AvwxMainModel>(responseData));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task RunBotAsync()
        {
            _discordSocketClient = new DiscordSocketClient();
            _commandService      = new CommandService();
            _services            = new ServiceCollection().AddSingleton(_discordSocketClient).AddSingleton(_commandService)
                                   .BuildServiceProvider();

            _discordSocketClient.Log += DiscordSocketClient_Log;
            await RegisterCommandsAsync();

            await _discordSocketClient.LoginAsync(TokenType.Bot, ApiKeysHelper.ApiKeysModelDeserialized().DiscordBotToken);

            await _discordSocketClient.StartAsync();

            await Task.Delay(-1);
        }