コード例 #1
0
        public async Task DisableAllNotificationsAsync(CommandContext ctx)
        {
            await ctx.TriggerTypingAsync();

            await _subscriptionsService.RemoveAllSubscriptionsFromChannelAsync(ctx.Channel.Id);

            var embed = _subscriptionEmbedGenerator.BuildMessageOnRemoveAll();
            await ctx.RespondAsync(string.Empty, false, embed);
        }
コード例 #2
0
        private async Task SendTweetsToChannel(SubscribedChannel channelData, List <DiscordEmbed> tweetEmbeds)
        {
            try
            {
                foreach (var embed in tweetEmbeds)
                {
                    var channel = await Bot.Client.GetChannelAsync(ulong.Parse(channelData.ChannelId));

                    await channel.SendMessageAsync(embed : embed);
                }
            }
            catch (UnauthorizedException ex)
            {
                var guild = await Bot.Client.GetGuildAsync(ulong.Parse(channelData.GuildId));

                var guildOwner = guild.Owner;

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

                await _subscriptionsService.RemoveAllSubscriptionsFromChannelAsync(
                    ulong.Parse(channelData.ChannelId));

                var ownerDm = await guildOwner.CreateDmChannelAsync();

                var errorEmbed = _twitterEmbedGenerator.BuildUnauthorizedError();
                await ownerDm.SendMessageAsync(embed : errorEmbed);
            }
            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 tweet on the channel with id [{channelData.ChannelId}]");
            }
        }
コード例 #3
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");
        }