Exemplo n.º 1
0
        public async Task ShouldthrowExceptionWhenInvalidNumberIsPassed(int number)
        {
            // Arrange
            var maxNumber             = 690;
            var latestResponseHandler = PrepareMockResponseHandlerForLatest(maxNumber);
            var httpClient            = PrepareHttpClient(latestResponseHandler);

            _mockHttpClientFactory.Setup(f => f.CreateClient(It.IsAny <string>())).Returns(httpClient);

            Func <Task> action = async() => await xkcdService.GetComicAsync(number);

            // Act and Assert
            await Assert.ThrowsAsync <ArgumentOutOfRangeException>(action);
        }
Exemplo n.º 2
0
 public async Task GetXkcdAsync(int number)
 {
     try
     {
         var comic = await xkcdService.GetComicAsync(number).ConfigureAwait(false);
         await EmbedComicAsync(comic, Context.Channel).ConfigureAwait(false);
     }
     catch (ArgumentOutOfRangeException)
     {
         await ReplyAsync("The specified comic does not exist!").ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         logger.LogError(ex, "Error while getting xkcd comic");
     }
 }
Exemplo n.º 3
0
        public async Task GetXkcdAsync(CommandContext ctx, [Description("Random comic if left empty, specific comic by number or latest by supplying \"latest\"")] string arg = "")
        {
            XkcdResponse comic = null;

            if (arg.Equals("latest", StringComparison.OrdinalIgnoreCase))
            {
                comic = await _xkcdService.GetLatestComicAsync();
            }
            else if (int.TryParse(arg, out int comicNumber))
            {
                try
                {
                    comic = await _xkcdService.GetComicAsync(comicNumber);
                }
                catch (ArgumentOutOfRangeException)
                {
                    await ctx.ErrorAsync("The specified comic does not exist!");

                    return;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error while getting xkcd comic");
                    return;
                }
            }
            else
            {
                comic = await _xkcdService.GetRandomComicAsync();
            }

            string comicUrl = _xkcdService.GetComicUrl(comic.Number).ToString();
            var    builder  = new DiscordEmbedBuilder()
                              .WithImageUrl(comic.ImgUrl)
                              .WithAuthor($"xkcd #{comic.Number}", comicUrl, "https://xkcd.com/s/0b7742.png")
                              .WithTitle(comic.SafeTitle)
                              .WithDescription(comic.Alt);

            if (int.TryParse(comic.Year, out int year) && int.TryParse(comic.Month, out int month) && int.TryParse(comic.Day, out int day))
            {
                var date = new DateTime(year, month, day);
                builder.WithFooter(date.ToString("yyyy-MM-dd"));
            }
            await ctx.RespondAsync(builder.Build());
        }