Ejemplo n.º 1
0
        public async Task TestUploader(CommandMessage message)
        {
            ExploderAPI.Video video = await ExploderAPI.GetLatestVideo("UCBGZf_eNHJPCFxVxzEEWMeA");

            // Send Embed
            await message.Channel.SendMessageAsync(embed : video.ToEmbed(), messageReference : message.MessageReference);
        }
Ejemplo n.º 2
0
        public async Task Update()
        {
            // Load streamers
            List <ContentCreator> streamers = await ContentCreatorDatabase.LoadAll();

            foreach (SocketGuild guild in Program.DiscordClient.Guilds)
            {
                // Load guild settings and check if content creator channel specified
                GuildSettings settings = await SettingsService.GetSettings <GuildSettings>(guild.Id);

                if (settings.ContentCreatorChannel == null)
                {
                    continue;
                }

                // Do not process if invalid
                if (!ulong.TryParse(settings.ContentCreatorChannel, out ulong channelId))
                {
                    continue;
                }

                // Do not process if couldn't find channel
                SocketTextChannel contentCreatorChannel = (SocketTextChannel)Program.DiscordClient.GetChannel(channelId);
                if (contentCreatorChannel == null)
                {
                    continue;
                }

                // Channel found - load streamers
                foreach (ContentCreator streamer in streamers.Where(x => x.DiscordGuildId == guild.Id))
                {
                    try
                    {
                        bool streamerUpdated = false;

                        // Check if display name should be updated
                        SocketGuildUser user = guild.Users.FirstOrDefault(x => x.Id == streamer.DiscordGuildId);
                        if (user != null)
                        {
                            string displayName = string.IsNullOrWhiteSpace(user.Nickname) ? user.Username : user.Nickname;
                            if (streamer.GuildNickName != displayName)
                            {
                                streamer.GuildNickName = displayName;
                                streamerUpdated        = true;
                            }
                        }

                        // Twitch streamer
                        if (streamer.Twitch != null && !string.IsNullOrWhiteSpace(streamer.Twitch.UserName))
                        {
                            StreamerAPI.Stream stream = await StreamerAPI.GetStreams(streamer.Twitch.UserName);

                            // Streamer is live
                            if (stream.IsLive)
                            {
                                // First stream or Current stream hasn't been posted and last stream longer than 30 minutes ago
                                if (streamer.Twitch.LastStream == null ||
                                    (stream.Id != streamer.Twitch.LastStream?.Id &&
                                     (stream.ParsedStartedAt - (streamer.Twitch.LastStream?.Created ?? DateTime.MinValue)).TotalMinutes > 30))
                                {
                                    RestUserMessage message = await contentCreatorChannel.SendMessageAsync(embed : stream.ToEmbed());

                                    // Save streamer id
                                    streamer.Twitch.LastStream = new ContentCreator.ContentInfo.Content(stream.Id, message.Id.ToString());
                                    streamerUpdated            = true;
                                }
                                ////else
                                ////{
                                ////	if (!string.IsNullOrWhiteSpace(streamer.Twitch.LastStreamEmbedMessageId)
                                ////		&& ulong.TryParse(streamer.Twitch.LastStreamEmbedMessageId, out ulong messageId))
                                ////	{
                                ////		if (await contentCreatorChannel.GetMessageAsync(messageId) is RestUserMessage message)
                                ////			await message.UpdateAsync(); // .ModifyAsync(x => x.); // x => x.Embed = stream.ToEmbed());
                                ////	}
                                ////}
                            }
                        }

                        // Youtube
                        if (streamer.Youtube != null && !string.IsNullOrWhiteSpace(streamer.Youtube.LinkId))
                        {
                            // Check video upload
                            ExploderAPI.Video latestVideo = await ExploderAPI.GetLatestVideo(streamer.Youtube.LinkId);

                            // Latest Video hasn't been posted
                            if (latestVideo != null && latestVideo.Id != streamer.Youtube.LastVideo?.Id)
                            {
                                YoutubeAPI.YoutubeVideo youtubeVideo = await YoutubeAPI.GetVideoInformation(latestVideo.Id);

                                latestVideo.UploadDate = youtubeVideo.Items.FirstOrDefault()?.Snippet.PublishedAt ?? latestVideo.UploadDate;

                                await contentCreatorChannel.SendMessageAsync(embed : latestVideo.ToEmbed());

                                // Save last video id
                                streamer.Youtube.LastVideo = new ContentCreator.ContentInfo.Content(latestVideo.Id);
                                streamerUpdated            = true;
                            }

                            // Check live stream
                            ExploderAPI.Video liveStream = await ExploderAPI.GetLiveVideo(streamer.Youtube.LinkId);

                            // Live stream occurring
                            if (liveStream != null && liveStream.Id != streamer.Youtube.LastStream?.Id)
                            {
                                await contentCreatorChannel.SendMessageAsync(embed : liveStream.ToLiveEmbed());

                                // Save last video id
                                streamer.Youtube.LastStream = new ContentCreator.ContentInfo.Content(liveStream.Id);
                                streamerUpdated             = true;
                            }
                        }

                        if (streamerUpdated)
                        {
                            await ContentCreatorDatabase.Save(streamer);
                        }
                    }
                    catch (Exception ex)
                    {
                        await Utils.Logger.LogExceptionToDiscordChannel(ex, "Content Creator Update", streamer.DiscordGuildId.ToString(), streamer.GuildNickName?.ToString());
                    }
                }
            }
        }