public async Task AddTwitter(string twitterName, IGuildChannel guildChannel) { var userResponse = await _twitterService.GetUserFromHandle(twitterName); if (userResponse?.Id is null) { await ReplyAsync("User Not Found"); return; } var user = await _botContext.TwitterUsers.Include(x => x.TwitterAlertSubscriptions) .FirstOrDefaultAsync(x => x.Id == userResponse.Id); if (user is null) { user = new TwitterUser { Id = userResponse.Id.Value, Name = userResponse.Name, TwitterAlertSubscriptions = new List <TwitterAlertSubscription>(), ScreenName = userResponse.ScreenName }; _botContext.TwitterUsers.Add(user); JobManager.AddJob( () => new TwitterMonitoringJob(user.Id, _twitterService, _provider.GetService <SteveBotContext>(), Context.Client).Execute(), s => s.WithName(userResponse.ScreenName).ToRunEvery(30).Seconds()); } if (user.TwitterAlertSubscriptions.Any(x => x.DiscordChannelId == (long)guildChannel.Id)) { await ReplyAsync($"You already subscribed to {user.ScreenName} in {guildChannel.Name}"); return; } user.TwitterAlertSubscriptions.Add(new TwitterAlertSubscription { DiscordChannelId = (long)guildChannel.Id, TwitterUserId = user.Id }); var changes = _botContext.SaveChanges(); if (changes > 0) { await ReplyAsync($"Alert for {user.ScreenName} added to {guildChannel.Name}"); } else { await ReplyAsync($"Unable to create Alert for {user.ScreenName}"); } }
public async Task AddTwitch(string twitchName, IGuildChannel guildChannel, string streamMessage) { var userResponse = await _twitchService.GetUserByNameAsync(twitchName); if (userResponse is null) { await ReplyAsync("User Not Found"); return; } var user = await _botContext.TwitchStreamers.Include(x => x.TwitchAlertSubscriptions) .FirstOrDefaultAsync(x => x.Id == long.Parse(userResponse.Id)); if (user is null) { user = new TwitchStreamer { Id = long.Parse(userResponse.Id), Name = userResponse.DisplayName, TwitchAlertSubscriptions = new List <TwitchAlertSubscription>() }; _botContext.TwitchStreamers.Add(user); JobManager.AddJob( () => new TwitchMonitoringJob(user.Id, _twitchService, _provider.GetService <SteveBotContext>(), Context.Client).Execute(), s => s.WithName(userResponse.DisplayName).ToRunEvery(60).Seconds()); } if (user.TwitchAlertSubscriptions.Any(x => x.DiscordChannelId == (long)guildChannel.Id)) { await ReplyAsync($"You already subscribed to {user.Name} in {guildChannel.Name}"); return; } user.TwitchAlertSubscriptions.Add(new TwitchAlertSubscription { DiscordChannelId = (long)guildChannel.Id, StreamMessage = streamMessage, TwitchStreamerId = user.Id }); var changes = _botContext.SaveChanges(); if (changes > 0) { await ReplyAsync($"Alert for {user.Name} added to {guildChannel.Name}"); } else { await ReplyAsync($"Unable to create Alert for {user.Name}"); } }
public async Task AddYoutube(string youtubeId, IGuildChannel guildChannel) { var video = await YoutubeFeedReader.GetLastestVideoFromFeed(youtubeId); if (video is null) { await ReplyAsync("I am unable to find information for that channel"); return; } var youtuber = await _botContext.Youtubers.Include(x => x.YoutubeAlertSubscriptions) .FirstOrDefaultAsync(x => x.Id == youtubeId); if (youtuber is null) { youtuber = new Youtuber { Id = youtubeId, Name = video.User, YoutubeAlertSubscriptions = new List <YoutubeAlertSubscription>() }; _botContext.Youtubers.Add(youtuber); JobManager.AddJob( () => new YoutubeMonitoringJob(youtuber.Id, _provider.GetService <SteveBotContext>(), Context.Client).Execute(), s => s.WithName(youtubeId).ToRunEvery(60).Seconds()); } if (youtuber.YoutubeAlertSubscriptions.Any(x => x.DiscordChannelId == (long)guildChannel.Id)) { await ReplyAsync($"You already subscribed to {youtuber.Name} in {guildChannel.Name}"); return; } youtuber.YoutubeAlertSubscriptions.Add(new YoutubeAlertSubscription { DiscordChannelId = (long)guildChannel.Id, YoutuberId = youtuber.Id }); var changes = _botContext.SaveChanges(); if (changes > 0) { await ReplyAsync($"Alert for {youtuber.Name} added to {guildChannel.Name}"); } else { await ReplyAsync($"Unable to create Alert for {youtuber.Name}"); } }
public void Execute() { using (_botContext) { var user = _botContext.TwitterUsers.Include(x => x.TwitterAlertSubscriptions) .FirstOrDefault(x => x.Id == _twitterUserId); if (user is null) { return; } if (user.LastTweetId == 0) { var tweet = _twitterService.GetLatestTweetForUserAsync(_twitterUserId).AsSync(false); foreach (var twitterAlert in user.TwitterAlertSubscriptions) { var channel = _client.GetChannel((ulong)twitterAlert.DiscordChannelId) as ITextChannel; channel.SendMessageAsync($@"https://twitter.com/{user.ScreenName}/status/{tweet.Id}") .AsSync(false); } Log.Information("{date}: {tweet}", tweet.CreatedAt, tweet.FullText ?? tweet.Text); user.LastTweetId = tweet.Id; } else { var tweets = _twitterService.GetTweetsSinceAsync(user.Id, user.LastTweetId).AsSync(false).ToList(); if (!tweets.Any()) { return; } foreach (var twitterAlert in user.TwitterAlertSubscriptions) { var channel = _client.GetChannel((ulong)twitterAlert.DiscordChannelId) as ITextChannel; foreach (var tweet in tweets) { channel.SendMessageAsync($@"https://twitter.com/{user.ScreenName}/status/{tweet.Id}") .AsSync(false); Log.Information("{date}: {tweet}", tweet.CreatedAt, tweet.FullText); } } user.LastTweetId = tweets.Last().Id; } _botContext.SaveChanges(); } }
public void Execute() { var youtuber = _botContext.Youtubers.Include(x => x.YoutubeAlertSubscriptions) .FirstOrDefault(x => x.Id == _channelId); var video = YoutubeFeedReader.GetLastestVideoFromFeed(_channelId).AsSync(false); if (video.DatePublished - youtuber.LatestVideoDate <= TimeSpan.Zero) { return; } youtuber.LatestVideoDate = video.DatePublished; foreach (var subscription in youtuber.YoutubeAlertSubscriptions) { var channel = _client.GetChannel((ulong)subscription.DiscordChannelId) as SocketTextChannel; channel.SendMessageAsync(video.Url); } _botContext.SaveChanges(); }
public void Execute() { using (_botContext) { var streamer = _botContext.TwitchStreamers.Include(x => x.TwitchAlertSubscriptions) .Include(x => x.Games) .FirstOrDefault(x => x.Id == _channelId); if (streamer is null) { return; } var isStreaming = _twitchService.IsUserStreamingAsync(_channelId).AsSync(false); //after the streamer goes offline the twitch api will sometimes says the stream is online //we wait for 3 minutes after the last stream to make sure the streamer in actually on or offline //this is arbitrary if (DateTimeOffset.UtcNow - streamer.StreamEndTime < TimeSpan.FromMinutes(3)) { return; } //stream has ended and we are waiting for startup again if (!isStreaming && streamer.StreamLength >= TimeSpan.Zero) { return; } // stream started if (isStreaming && streamer.StreamLength >= TimeSpan.Zero) { var stream = _twitchService.GetStreamAsync(_channelId).AsSync(false); streamer.SteamStartTime = stream.CreatedAt.ToUniversalTime(); streamer.Games.Add(new Data.Entities.Game() { StartTime = DateTimeOffset.UtcNow, Name = string.IsNullOrWhiteSpace(stream.Game) ? "No Game" : stream.Game }); foreach (var subscription in streamer.TwitchAlertSubscriptions) { var messageId = CreateTwitchMessage(streamer, stream, subscription).AsSync(false); subscription.MessageId = messageId; } _botContext.SaveChanges(); return; } // stream has started and we update the message embed if (isStreaming && streamer.StreamLength <= TimeSpan.Zero) { var stream = _twitchService.GetStreamAsync(_channelId).AsSync(false); if (stream is null) { return; } var oldGame = streamer.Games.LastOrDefault(); var currentGame = string.IsNullOrWhiteSpace(stream.Game) ? "No Game" : stream.Game; if (oldGame != null && oldGame.Name != currentGame) { oldGame.EndTime = DateTimeOffset.UtcNow; streamer.Games.Add(new Data.Entities.Game() { StartTime = DateTimeOffset.UtcNow, Name = currentGame }); } foreach (var subscription in streamer.TwitchAlertSubscriptions) { if (subscription.MessageId == 0) { var messageId = CreateTwitchMessage(streamer, stream, subscription).AsSync(false); subscription.MessageId = messageId; } var channel = _client.GetChannel((ulong)subscription.DiscordChannelId) as ITextChannel; var message = channel.GetMessageAsync((ulong)subscription.MessageId).AsSync(false) as IUserMessage; if (message is null) { Log.Information("Message was not found"); return; } message.ModifyAsync(x => x.Embed = CreateTwitchEmbed(streamer, stream)).AsSync(false); } } //stream ended if (!isStreaming && streamer.StreamLength <= TimeSpan.Zero) { streamer.Games.Last().EndTime = DateTimeOffset.UtcNow; streamer.StreamEndTime = DateTimeOffset.UtcNow; var user = _twitchService.GetUserByIdAsync(streamer.Id).AsSync(false); var description = new StringBuilder(); description.AppendLine($"**Started at:** {streamer.SteamStartTime:g} UTC"); description.AppendLine($"__**Ended at:** {streamer.StreamEndTime:g} UTC__"); description.AppendLine( $"**Total Time:** {streamer.StreamLength.Humanize(2, maxUnit: TimeUnit.Hour, minUnit: TimeUnit.Minute, collectionSeparator: " ")}"); var embed = new EmbedBuilder() .WithAuthor($"{streamer.Name} was live", url: $"https://twitch.tv/{streamer.Name}") .WithThumbnailUrl(user.Logo) .WithDescription(description.ToString()) .AddField("Games Played", string.Join("\n", streamer.Games.Where(x => x.StartTime >= streamer.SteamStartTime && x.EndTime <= streamer.StreamEndTime).Select(x => $"**{x.Name}:** Played for {x.PlayLength.Humanize(2, maxUnit: TimeUnit.Hour, minUnit: TimeUnit.Minute, collectionSeparator: " ")}"))) .Build(); foreach (var subscription in streamer.TwitchAlertSubscriptions) { var channel = _client.GetChannel((ulong)subscription.DiscordChannelId) as ITextChannel; var message = channel.GetMessageAsync((ulong)subscription.MessageId).AsSync(false) as IUserMessage; if (message is null) { Log.Information("Message was not found"); return; } message.ModifyAsync(x => x.Embed = embed).AsSync(false); } } _botContext.SaveChanges(); } }