public async Task NextLaunchAsync(CommandContext ctx)
        {
            await ctx.TriggerTypingAsync();

            var launchData = await _cacheService.GetAsync <LaunchInfo>(CacheContentType.NextLaunch);

            var embed = _launchInfoEmbedGenerator.Build(launchData, ctx.Guild.Id, true);

            var sentMessage = await ctx.RespondAsync(string.Empty, false, embed);

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

            await _launchNotificationsService.AddMessageToSubscribe(ctx.Channel, sentMessage);
        }
Exemple #2
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");
        }