コード例 #1
0
ファイル: XkcdModule.cs プロジェクト: MK31973/MonkeyBot
        public async Task GetXkcdAsync(string arg = "")
        {
            XkcdResponse comic = !string.IsNullOrEmpty(arg) && arg.Equals("latest", StringComparison.OrdinalIgnoreCase)
                ? await xkcdService.GetLatestComicAsync().ConfigureAwait(false)
                : await xkcdService.GetRandomComicAsync().ConfigureAwait(false);

            await EmbedComicAsync(comic, Context.Channel).ConfigureAwait(false);
        }
コード例 #2
0
        public async Task ShouldGetLatestComic()
        {
            // Arrange
            var number = 69;
            var latestResponseHandler = PrepareMockResponseHandlerForLatest(number);
            var httpClient            = PrepareHttpClient(latestResponseHandler);

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

            var xkcdResponse = PrepareXkcdResponse(number);

            // Act
            var latestComic = await xkcdService.GetLatestComicAsync();

            // Assert
            PropertyMatcher.Match(xkcdResponse, latestComic);
        }
コード例 #3
0
ファイル: XkcdModule.cs プロジェクト: MarkusKgit/MonkeyBot
        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());
        }