Exemple #1
0
        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);
        }
        public async Task xkcd()
        {
            WebClient client = new WebClient();

            var random = new Random();

            string s = client.DownloadString($"https://xkcd.com/{random.Next(1,1850)}/info.0.json");

            XkcdResponse response = JsonConvert.DeserializeObject <XkcdResponse>(s);

            await ReplyAsync($"{response.title} - {response.img}");
        }
Exemple #3
0
 public async Task GetXkcdAsync(int number)
 {
     try
     {
         XkcdResponse 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");
     }
 }
Exemple #4
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());
        }
Exemple #5
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);
        }