Beispiel #1
0
        private async Task <Embed> SendMessageAsync(YouTubeToCheck youTubeToCheck, YouTubeVideoListItem video)
        {
            try
            {
                var channel = _client.GetChannel((ulong)youTubeToCheck.DiscordChannelId) as SocketTextChannel;

                var embed = await CreateEmbedAsync(video);

                if (embed == null)
                {
                    await channel.SendMessageAsync(
                        "Something broke when posting a new YouTube video. Bug Thing about it. (error: e)");

                    return(null);
                }

                await channel.SendMessageAsync($"{video.Snippet.ChannelTitle} posted a new YouTube video.",
                                               embed : embed);

                return(embed);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("[" + DateTime.UtcNow + "] YOUTUBE ERROR, SendMessageAsync");
                Console.Error.WriteLine(ex.ToString());
                Console.Error.WriteLine(ex.InnerException?.ToString());
                Console.Error.WriteLine("------------");
                Console.Error.WriteLine();
            }

            return(null);
        }
Beispiel #2
0
        private async Task <Embed> CreateEmbedAsync(YouTubeVideoListItem video)
        {
            video.Snippet.Thumbnails.Maxres =
                new YouTubeVideoThumbnail {
                Url = $"https://i.ytimg.com/vi/{video.Id.VideoId}/maxresdefault.jpg"
            };

            // GET CHANNEL OBJECT FROM API
            var response = await _httpClient
                           .GetAsync($"channels?part=snippet&id={video.Snippet.ChannelId}&key={_config["youtubeKey"]}");

            var responseString = await response.Content.ReadAsStringAsync();

            var channelList = JsonConvert.DeserializeObject <YouTubeChannelList>(responseString);

            if (channelList.Items.Count != 1)
            {
                return(null);
            }

            var channel = channelList.Items[0];

            var embed  = new EmbedBuilder();
            var author = new EmbedAuthorBuilder
            {
                Name    = channel.Snippet.Title,
                Url     = $"https://www.youtube.com/channel/{channel.Id}",
                IconUrl = channel.Snippet.Thumbnails.Default.Url
            };

            var publishedAt = TimeZoneInfo.ConvertTime(video.Snippet.PublishedAt, Helpers.CentralTimeZone());

            var footer = new EmbedFooterBuilder
            {
                Text = $"Posted on {publishedAt:MMM d, yyyy} at {publishedAt:H:mm} Central"
            };

            var shortDescription = video.Snippet.Description;

            var lineBreakIndex = video.Snippet.Description.IndexOf("\n");

            if (lineBreakIndex != -1)
            {
                shortDescription = shortDescription.Substring(0, lineBreakIndex);
            }

            if (shortDescription.Length > 500)
            {
                shortDescription = shortDescription.Substring(0, 500) + "...";
            }

            embed.Author      = author;
            embed.Footer      = footer;
            embed.Color       = new Color((uint)_youTubeToCheck.EmbedColor);
            embed.ImageUrl    = video.Snippet.Thumbnails.Maxres.Url;
            embed.Title       = video.Snippet.Title;
            embed.Description = shortDescription;
            embed.Url         = $"https://www.youtube.com/watch?v={video.Id.VideoId}";

            return(embed.Build());
        }