private Embed CreateEmbed(YouTubeRecord record, YouTubeToCheck youtube)
        {
            var embed  = new EmbedBuilder();
            var author = new EmbedAuthorBuilder
            {
                Name    = record.AuthorName,
                Url     = record.AuthorUrl,
                IconUrl = record.AuthorIconUrl
            };

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

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

            var descriptionField = new EmbedFieldBuilder
            {
                Name     = "Description",
                Value    = record.VideoDescription,
                IsInline = false
            };

            embed.Author   = author;
            embed.Footer   = footer;
            embed.Color    = new Color((uint)youtube.EmbedColor);
            embed.ImageUrl = record.ImageUrl;
            embed.Title    = record.VideoTitle;
            embed.Url      = "https://www.youtube.com/watch?v=" + record.VideoId;

            embed.AddField(descriptionField);

            return(embed.Build());
        }
示例#2
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);
        }
        public async Task AddYoutube(string youtubeName, IGuildChannel guildChannel, string hexColor)
        {
            var channels = await _youtubeService.GetYouTubeVideoChannelInfoAsync(youtubeName);

            if (channels.Items is null)
            {
                await ReplyAsync("Unable to get info from Youtube API");

                return;
            }

            using (var context = new DggContext())
            {
                var channel = channels.Items.FirstOrDefault();
                if (await context.YouTubesToCheck.FirstOrDefaultAsync(x => x.ChannelId == channel.Id) is null)
                {
                    var youtubeToCheck = new YouTubeToCheck
                    {
                        DiscordChannelId = (long)guildChannel.Id,
                        DiscordServerId  = (long)Context.Guild.Id,
                        ChannelId        = channel.Id,
                        Frequency        = 60,
                        FriendlyUsername = channel.Snippet.Title,
                        EmbedColor       = (int)Helpers.GetColorFromHex(hexColor).RawValue
                    };

                    await context.YouTubesToCheck.AddAsync(youtubeToCheck);

                    var changes = await context.SaveChangesAsync();

                    if (changes > 0)
                    {
                        Log.Information("{author} added Youtube Channel:{channel}", Context.Message.Author,
                                        youtubeName);
                        await ReplyAsync($"{channel.Snippet.Title} added to the database");

                        JobManager.AddJob(
                            new YoutubeJob(Context.SocketClient, _youtubeService, youtubeToCheck, new HttpClient(),
                                           _config),
                            s => s.WithName(youtubeToCheck.ChannelId).ToRunEvery(youtubeToCheck.Frequency)
                            .Seconds());
                        return;
                    }

                    await ReplyAsync($"Unable to save youtube to database");

                    return;
                }
                await ReplyAsync("Youtube account already exists in the Database");
            }
        }
示例#4
0
 public YoutubeJob(
     DiscordSocketClient client,
     YoutubeService youtubeService,
     YouTubeToCheck youTubeToCheck,
     HttpClient httpClient,
     IConfiguration config
     )
 {
     _client                 = client;
     _youtubeService         = youtubeService;
     _youTubeToCheck         = youTubeToCheck;
     _httpClient             = httpClient;
     _config                 = config;
     _httpClient.BaseAddress = new Uri("https://www.googleapis.com/youtube/v3/");
     _httpClient.Timeout     = new TimeSpan(0, 0, 8);
 }