Exemplo n.º 1
0
        private async void FlickrServiceOnNewFlickrServicePhotoAsync(object sender, List <CachedFlickrPhoto> photos)
        {
            var subscribedChannels = _subscriptionsService.GetSubscribedChannels(SubscriptionType.Flickr);
            var embedsToSend       = photos.Select(p => _flickrEmbedGenerator.Build(p)).ToList();

            var stopwatch = Stopwatch.StartNew();

            foreach (var channelData in subscribedChannels)
            {
                try
                {
                    await SendPhotosToChannel(channelData, embedsToSend);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"General error occurred when trying to send Flickr notification (channel [{channelData.ChannelId}], guild [{channelData.GuildId}])");
                }
            }

            _logger.Info($"{photos.Count} Flickr notifications sent to {subscribedChannels.Count} channels " +
                         $"in {stopwatch.Elapsed.TotalSeconds:0.0} seconds");
        }
Exemplo n.º 2
0
        private async void Reddit_OnNewHotTopicAsync(object sender, List <RedditChildData> topics)
        {
            var channels     = _subscriptionsService.GetSubscribedChannels(SubscriptionType.Reddit);
            var embedsToSend = topics.Select(p => _redditEmbedGenerator.Build(p)).ToList();

            var stopwatch = Stopwatch.StartNew();

            foreach (var channelData in channels)
            {
                try
                {
                    await SendTopicToChannel(channelData, embedsToSend);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"General error occurred when trying to send Reddit notification (channel [{channelData.ChannelId}], guild [{channelData.GuildId}])");
                }
            }

            _logger.Info($"{topics.Count} Reddit notifications sent to {channels.Count} channels " +
                         $"in {stopwatch.Elapsed.TotalSeconds:0.0} seconds");
        }
Exemplo n.º 3
0
        private async void TwitterOnNewTweetsAsync(object sender, List <CachedTweet> tweets)
        {
            var user             = (KeyValuePair <TwitterUserType, string>)sender;
            var subscriptionType = _twitterService.GetSubscriptionTypeByUserName(user.Value);
            var channels         = _subscriptionsService.GetSubscribedChannels(subscriptionType);
            var embedsToSend     = tweets.Select(p => _twitterEmbedGenerator.Build(p)).ToList();

            var stopwatch = Stopwatch.StartNew();

            foreach (var channelData in channels)
            {
                try
                {
                    await SendTweetsToChannel(channelData, embedsToSend);
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"General error occurred when trying to send Twitter notification (channel [{channelData.ChannelId}], guild [{channelData.GuildId}])");
                }
            }

            _logger.Info($"{tweets.Count} Twitter notifications sent to {channels.Count} channels " +
                         $"in {stopwatch.Elapsed.TotalSeconds:0.0} seconds");
        }
Exemplo n.º 4
0
        private async void LaunchNotificationsOnLaunchNotificationAsync(object sender, LaunchNotification launchNotification)
        {
            var embed    = _launchNotificationEmbedBuilder.Build(launchNotification);
            var channels = _subscriptionsService.GetSubscribedChannels(SubscriptionType.NextLaunch);

            var launchTime = launchNotification.NewLaunchState.DateUtc ?? DateTime.MinValue;

            if (launchTime == DateTime.MinValue)
            {
                return;
            }

            var timeLeft  = (launchTime - DateTime.Now.ToUniversalTime()).TotalMinutes;
            var stopwatch = Stopwatch.StartNew();

            foreach (var channelData in channels)
            {
                try
                {
                    var channel = await Bot.Client.GetChannelAsync(ulong.Parse(channelData.ChannelId));

                    var sentMessage = await channel.SendMessageAsync(string.Empty, false, embed);

                    await sentMessage.CreateReactionAsync(DiscordEmoji.FromName(Bot.Client, ":regional_indicator_s:"));

                    await _launchNotificationsService.AddMessageToSubscribe(channel, sentMessage);

                    if (launchNotification.Type == LaunchNotificationType.Reminder && timeLeft < 60 && launchNotification.NewLaunchState.Links.Webcast != null)
                    {
                        await channel.SendMessageAsync($"**YouTube stream:** {launchNotification.NewLaunchState.Links.Webcast}");
                    }
                }
                catch (UnauthorizedException ex)
                {
                    await _subscriptionsService.RemoveAllSubscriptionsFromChannelAsync(ulong.Parse(channelData.ChannelId));

                    try
                    {
                        var guild = await Bot.Client.GetGuildAsync(ulong.Parse(channelData.GuildId));

                        var guildOwner = guild.Owner;

                        _logger.Warn($"No permissions to send message to channel [{channelData.ChannelId}], " +
                                     $"removing all subscriptions and sending message to {guildOwner.Username} [{guildOwner.Id}]");
                        _logger.Warn($"JSON: {ex.JsonMessage}");


                        var ownerDm = await guildOwner.CreateDmChannelAsync();

                        var errorEmbed = _launchNotificationEmbedBuilder.BuildUnauthorizedError();

                        await ownerDm.SendMessageAsync(embed : errorEmbed);
                    }
                    catch (Exception e)
                    {
                        _logger.Fatal(e);
                    }
                }
                catch (NotFoundException ex)
                {
                    _logger.Warn($"Channel [{channelData.ChannelId}] not found, removing all subscriptions");
                    _logger.Warn($"JSON: {ex.JsonMessage}");

                    await _subscriptionsService.RemoveAllSubscriptionsFromChannelAsync(ulong.Parse(channelData.ChannelId));
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, $"Can't send launch notification to the channel with id [{channelData.ChannelId}]");
                }
            }

            _logger.Info($"Launch notifications sent to {channels.Count} channels " +
                         $"in {stopwatch.Elapsed.TotalSeconds:0.0} seconds");
        }