Пример #1
0
        public MovieRequestingWorkflow CreateRequestingWorkflow(DiscordInteraction interaction, int categoryId)
        {
            var settings = _settingsProvider.Provide();

            return(new MovieRequestingWorkflow(new MovieUserRequester(interaction.User.Id.ToString(), interaction.User.Username),
                                               categoryId,
                                               GetMovieClient <IMovieSearcher>(settings),
                                               GetMovieClient <IMovieRequester>(settings),
                                               new DiscordMovieUserInterface(interaction, GetMovieClient <IMovieSearcher>(settings)),
                                               CreateMovieNotificationWorkflow(interaction, settings)));
        }
Пример #2
0
        public TvShowRequestingWorkflow CreateRequestingWorkflow(DiscordInteraction interaction, int categoryId)
        {
            var settings = _settingsProvider.Provide();

            return(new TvShowRequestingWorkflow(new TvShowUserRequester(interaction.User.Id.ToString(), interaction.User.Username),
                                                categoryId,
                                                GetTvShowClient <ITvShowSearcher>(settings),
                                                GetTvShowClient <ITvShowRequester>(settings),
                                                new DiscordTvShowUserInterface(interaction),
                                                CreateMovieNotificationWorkflow(interaction, settings, GetTvShowClient <ITvShowSearcher>(settings)),
                                                _tvShowsSettingsProvider.Provide()));
        }
Пример #3
0
 public RequestrrModuleBase(DiscordSocketClient discord, SocketCommandContext commandContext, DiscordSettingsProvider discordSettingsProvider)
 {
     _discord                = discord;
     Context                 = commandContext;
     _discordSettings        = discordSettingsProvider.Provide();
     _discord.ReactionAdded += HandleReactionAsync;
 }
        public async Task <HashSet <string> > NotifyAsync(IReadOnlyCollection <string> userIds, TvShow tvShow, int seasonNumber, CancellationToken token)
        {
            var userNotified = new HashSet <string>();

            if (_discordClient.Guilds.Any())
            {
                foreach (var userId in userIds)
                {
                    if (token.IsCancellationRequested)
                    {
                        return(userNotified);
                    }

                    try
                    {
                        DiscordMember user = null;

                        foreach (var guild in _discordClient.Guilds.Values)
                        {
                            try
                            {
                                user = await guild.GetMemberAsync(ulong.Parse(userId));

                                break;
                            }
                            catch { }
                        }

                        if (user != null)
                        {
                            var channel = await user.CreateDmChannelAsync();

                            if (_discordSettingsProvider.Provide().TvShowDownloadClient == DownloadClient.Overseerr)
                            {
                                await channel.SendMessageAsync(Language.Current.DiscordNotificationTvDMSeason.ReplaceTokens(tvShow, seasonNumber), DiscordTvShowUserInterface.GenerateTvShowDetailsAsync(tvShow));
                            }
                            else
                            {
                                await channel.SendMessageAsync(Language.Current.DiscordNotificationTvDMFirstEpisode.ReplaceTokens(tvShow, seasonNumber), DiscordTvShowUserInterface.GenerateTvShowDetailsAsync(tvShow));
                            }
                        }
                        else
                        {
                            _logger.LogWarning($"Removing tv show notification for user with ID {userId} as it could not be found in any of the guilds.");
                        }

                        userNotified.Add(userId);
                    }
                    catch (System.Exception ex)
                    {
                        _logger.LogError(ex, "An error occurred while sending a tv show notification to a specific user: " + ex.Message);
                    }
                }
            }

            return(userNotified);
        }
Пример #5
0
        public async void Start()
        {
            _commandService = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
            });

            _client            = new DiscordSocketClient();
            _client.Log       += LogAsync;
            _client.Connected += Connected;
            _commandService.CommandExecuted += CommandExecutedAsync;
            _client.MessageReceived         += MessageReceivedAsync;

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var newSettings = _discordSettingsProvider.Provide();
                        var mustRestart = false;

                        try
                        {
                            if (_client.ConnectionState == ConnectionState.Connected)
                            {
                                mustRestart = string.IsNullOrEmpty(_client.CurrentUser.Username);
                            }
                        }
                        catch
                        {
                            mustRestart = true;
                        }

                        if (mustRestart)
                        {
                            _logger.LogWarning("Restarting bot due to incorrect automatic reconnection.");
                        }

                        if (!_currentSettings.Equals(newSettings) || mustRestart || _client.ConnectionState == ConnectionState.Disconnected)
                        {
                            _logger.LogWarning("Bot configuration changed/not connected to Discord: restarting bot");
                            _currentSettings = newSettings;
                            await RestartBot(newSettings);
                            _logger.LogWarning("Bot has been restarted.");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error while restarting the bot: " + ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }
Пример #6
0
        public async void Start()
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var previousGuildCount = _currentGuilds.Count;
                        var newSettings        = _discordSettingsProvider.Provide();

                        try
                        {
                            var newGuilds = new HashSet <ulong>(_client?.Guilds.Keys.ToArray() ?? Array.Empty <ulong>());

                            if (newGuilds.Any())
                            {
                                _currentGuilds.UnionWith(newGuilds);
                            }
                        }
                        catch (System.Exception) { }

                        if (!_currentSettings.Equals(newSettings) || Language.Current != _previousLanguage || _currentGuilds.Count != previousGuildCount)
                        {
                            var previousSettings = _currentSettings;
                            _logger.LogWarning("Bot configuration changed: restarting bot");
                            _currentSettings  = newSettings;
                            _previousLanguage = Language.Current;
                            await RestartBot(previousSettings, newSettings, _currentGuilds);
                            _logger.LogWarning("Bot has been restarted.");

                            SlashCommandBuilder.CleanUp();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error while restarting the bot: " + ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }
Пример #7
0
        public async Task <HashSet <string> > NotifyAsync(IReadOnlyCollection <string> userIds, TvShow tvShow, int seasonNumber, CancellationToken token)
        {
            var userNotified = new HashSet <string>();

            foreach (var userId in userIds)
            {
                if (token.IsCancellationRequested)
                {
                    return(userNotified);
                }

                try
                {
                    var user = _discordClient.GetUser(ulong.Parse(userId));

                    if (user != null)
                    {
                        var channel = await user.GetOrCreateDMChannelAsync();

                        if (_discordSettingsProvider.Provide().TvShowDownloadClient == DownloadClient.Overseerr)
                        {
                            await channel.SendMessageAsync($"The **season {seasonNumber}** of **{tvShow.Title}** that you requested has finished downloading!", false, DiscordTvShowsRequestingWorkFlow.GenerateTvShowDetailsAsync(tvShow, user));
                        }
                        else
                        {
                            await channel.SendMessageAsync($"The first episode of **season {seasonNumber}** of **{tvShow.Title}** that you requested has finished downloading!", false, DiscordTvShowsRequestingWorkFlow.GenerateTvShowDetailsAsync(tvShow, user));
                        }

                        userNotified.Add(userId);
                    }
                    else if (!token.IsCancellationRequested && _discordClient.ConnectionState == ConnectionState.Connected)
                    {
                        _logger.LogWarning($"An error occurred while sending a show notification to a specific user, could not find user with ID {userId}. {System.Environment.NewLine} Make sure [Presence Intent] and [Server Members Intent] are enabled in the bots configuration.");
                    }
                }
                catch (System.Exception ex)
                {
                    _logger.LogError(ex, "An error occurred while sending a tv show notification to a specific user: " + ex.Message);
                }
            }

            return(userNotified);
        }
Пример #8
0
        public async void Start()
        {
            _commandService = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
            });

            _client            = new DiscordSocketClient();
            _client.Log       += LogAsync;
            _client.Connected += Connected;
            _commandService.CommandExecuted += CommandExecutedAsync;
            _client.MessageReceived         += MessageReceivedAsync;

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var newSettings = _discordSettingsProvider.Provide();

                        if (!_currentSettings.Equals(newSettings) || _client.ConnectionState == ConnectionState.Disconnected)
                        {
                            _logger.LogWarning("Bot changes detected: restarting bot");
                            _currentSettings = newSettings;
                            await RestartBot(newSettings);
                            _logger.LogWarning("Bot changes detected: bot restarted");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogWarning(ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }
Пример #9
0
        private async Task NotifyUsersInChannel(TvShow tvShow, int seasonNumber, HashSet <ulong> discordUserIds, HashSet <ulong> userNotified, DiscordChannel channel)
        {
            var usersToNotify = channel.Users
                                .Where(x => discordUserIds.Contains(x.Id))
                                .Where(x => !userNotified.Contains(x.Id));

            if (usersToNotify.Any())
            {
                var messageBuilder = new StringBuilder();

                if (_discordSettingsProvider.Provide().TvShowDownloadClient == DownloadClient.Overseerr)
                {
                    messageBuilder.AppendLine(Language.Current.DiscordNotificationTvChannelSeason.ReplaceTokens(tvShow, seasonNumber));
                }
                else
                {
                    messageBuilder.AppendLine(Language.Current.DiscordNotificationTvChannelFirstEpisode.ReplaceTokens(tvShow, seasonNumber));
                }

                foreach (var user in usersToNotify)
                {
                    var userMentionText = $"{user.Mention} ";

                    if (messageBuilder.Length + userMentionText.Length < DiscordConstants.MaxMessageLength)
                    {
                        messageBuilder.Append(userMentionText);
                    }
                }

                await channel.SendMessageAsync(messageBuilder.ToString(), DiscordTvShowUserInterface.GenerateTvShowDetailsAsync(tvShow));

                foreach (var user in usersToNotify)
                {
                    userNotified.Add(user.Id);
                }
            }
        }
Пример #10
0
        private async Task NotifyUsersInChannel(TvShow tvShow, int seasonNumber, HashSet <ulong> discordUserIds, HashSet <string> userNotified, SocketTextChannel channel)
        {
            var usersToMention = channel.Users
                                 .Where(x => discordUserIds.Contains(x.Id))
                                 .Where(x => !userNotified.Contains(x.Id.ToString()));

            if (usersToMention.Any())
            {
                var messageBuilder = new StringBuilder();

                if (_discordSettingsProvider.Provide().TvShowDownloadClient == DownloadClient.Overseerr)
                {
                    messageBuilder.AppendLine($"The **season {seasonNumber}** of **{tvShow.Title}** has finished downloading!");
                }
                else
                {
                    messageBuilder.AppendLine($"The first episode of **season {seasonNumber}** of **{tvShow.Title}** has finished downloading!");
                }

                foreach (var user in usersToMention)
                {
                    var userMentionText = $"{user.Mention} ";

                    if (messageBuilder.Length + userMentionText.Length < DiscordConstants.MaxMessageLength)
                    {
                        messageBuilder.Append(userMentionText);
                    }
                }

                await channel.SendMessageAsync(messageBuilder.ToString(), false, DiscordTvShowsRequestingWorkFlow.GenerateTvShowDetailsAsync(tvShow));

                foreach (var user in usersToMention)
                {
                    userNotified.Add(user.Id.ToString());
                }
            }
        }