public async Task XkcdAsync()
        {
            // Get a stream containing an image of a cat
            var stream = await XkcdService.GetXkcdComicAsync();

            // Streams must be seeked to their beginning before being uploaded!
            stream.Seek(0, SeekOrigin.Begin);
            await Context.Channel.SendFileAsync(stream, "xkcd.png");
        }
Exemplo n.º 2
0
        public async Task RandomAsync(CommandContext ctx)
        {
            XkcdComic?comic = await XkcdService.GetRandomComicAsync();

            if (comic is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-xkcd");
            }

            await this.PrintComicAsync(ctx, comic);
        }
Exemplo n.º 3
0
        public async Task RandomAsync(CommandContext ctx)
        {
            XkcdComic comic = await XkcdService.GetRandomComicAsync();

            if (comic is null)
            {
                throw new CommandFailedException("Failed to retrieve comic from xkcd.");
            }

            await ctx.RespondAsync(embed : comic.ToDiscordEmbedBuilder(this.ModuleColor));
        }
Exemplo n.º 4
0
        public async Task ByIdAsync(CommandContext ctx,
                                    [Description("Comic ID.")] int?id = null)
        {
            XkcdComic comic = await XkcdService.GetComicAsync(id);

            if (comic is null)
            {
                throw new CommandFailedException("Failed to retrieve comic from xkcd.");
            }

            await ctx.RespondAsync(embed : comic.ToDiscordEmbedBuilder(this.ModuleColor));
        }
Exemplo n.º 5
0
 private Task PrintComicAsync(CommandContext ctx, XkcdComic comic)
 {
     return(ctx.RespondWithLocalizedEmbedAsync(emb => {
         emb.WithTitle($"xkcd #{comic.Id} - {comic.Title}");
         emb.WithImageUrl(comic.ImageUrl);
         string?url = XkcdService.CreateUrlForComic(comic.Id);
         if (url is { })
         {
             emb.WithUrl(url);
         }
         emb.WithColor(this.ModuleColor);
         emb.WithLocalizedFooter("fmt-xkcd", null, $"{comic.Month}/{comic.Year}");
     }));
Exemplo n.º 6
0
        public async Task GetComicAsyncTest()
        {
            Assert.IsNotNull(await XkcdService.GetComicAsync());
            Assert.IsNotNull(await XkcdService.GetComicAsync(1));
            Assert.IsNotNull(await XkcdService.GetComicAsync(2));
            Assert.IsNotNull(await XkcdService.GetComicAsync(3));
            Assert.IsNotNull(await XkcdService.GetComicAsync(4));
            Assert.IsNotNull(await XkcdService.GetComicAsync(5));
            Assert.IsNotNull(await XkcdService.GetComicAsync(1000));
            Assert.IsNotNull(await XkcdService.GetComicAsync(2000));

            Assert.ThrowsAsync(typeof(ArgumentException), () => XkcdService.GetComicAsync(-1));
            Assert.ThrowsAsync(typeof(ArgumentException), () => XkcdService.GetComicAsync(0));
            Assert.ThrowsAsync(typeof(ArgumentException), () => XkcdService.GetComicAsync(100000));
            Assert.ThrowsAsync(typeof(ArgumentException), () => XkcdService.GetComicAsync(XkcdService.TotalComics + 1));
        }
Exemplo n.º 7
0
        public async Task ByIdAsync(CommandContext ctx,
                                    [Description("desc-id")] int?id = null)
        {
            if (id < 0 || id > XkcdService.TotalComics)
            {
                throw new CommandFailedException(ctx, "cmd-err-xkcd");
            }

            XkcdComic?comic = await XkcdService.GetComicAsync(id);

            if (comic is null)
            {
                throw new CommandFailedException(ctx, "cmd-err-xkcd");
            }

            await this.PrintComicAsync(ctx, comic);
        }
Exemplo n.º 8
0
        public static DiscordEmbedBuilder ToDiscordEmbedBuilder(this XkcdComic comic, DiscordColor?color = null)
        {
            var emb = new DiscordEmbedBuilder {
                Title    = $"xkcd #{comic.Id} : {comic.Title}",
                ImageUrl = comic.ImageUrl,
                Url      = XkcdService.CreateUrlForComic(comic.Id)
            };

            if (!(color is null))
            {
                emb.WithColor(color.Value);
            }

            emb.WithFooter($"Publish date: {comic.Month}/{comic.Year}");

            return(emb);
        }
Exemplo n.º 9
0
        public DiscordEmbed ToDiscordEmbed(DiscordColor?color = null)
        {
            var emb = new DiscordEmbedBuilder()
            {
                Title    = $"xkcd #{this.Id} : {this.Title}",
                ImageUrl = ImageUrl,
                Url      = XkcdService.CreateUrlForComic(this.Id)
            };

            if (color != null)
            {
                emb.WithColor(color.Value);
            }

            emb.WithFooter($"Publish date: {this.Month}/{this.Year}");

            return(emb.Build());
        }
Exemplo n.º 10
0
 public async Task GetRandomComicAsyncTest()
 {
     Assert.IsNotNull(await XkcdService.GetRandomComicAsync());
 }
Exemplo n.º 11
0
 public ComicApiProvider(XkcdService xkcdService)
 {
     _xkcdService = xkcdService;
 }