Beispiel #1
0
        public void ShouldreturnProperComicUrl()
        {
            // Arrange
            var comicNumber = 88;
            var expectedUri = _baseUri + comicNumber;

            // Act
            var comicUri = xkcdService.GetComicUrl(comicNumber);

            // Assert
            Assert.Equal(new Uri(expectedUri), comicUri);
        }
Beispiel #2
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());
        }
Beispiel #3
0
        private async Task EmbedComicAsync(XkcdResponse comic, IMessageChannel channel)
        {
            if (comic == null)
            {
                return;
            }
            var    builder  = new EmbedBuilder();
            string comicUrl = xkcdService.GetComicUrl(comic.Number).ToString();

            builder.WithImageUrl(comic.ImgUrl.ToString());
            builder.WithAuthor($"xkcd #{comic.Number}", "https://xkcd.com/s/919f27.ico", comicUrl);
            builder.WithTitle(comic.SafeTitle);
            builder.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))
            {
                DateTime date = new DateTime(year, month, day);
                builder.WithFooter(date.ToString("yyyy-MM-dd"));
            }
            await channel.SendMessageAsync("", false, builder.Build()).ConfigureAwait(false);
        }