private async Task ApplyBotConfigurationAsync(DiscordSettings discordSettings)
        {
            await _client.SetGameAsync(discordSettings.StatusMessage);

            _moduleInfo = await _commandService.CreateModuleAsync(string.Empty, x =>
            {
                x.AddCommand("ping", async(commandContext, noidea, serviceProvider, commandInfo) =>
                {
                    using (var command = new DiscordPingWorkFlow((SocketCommandContext)commandContext, _client, serviceProvider.Get <DiscordSettingsProvider>()))
                    {
                        await command.HandlePingAsync();
                    }
                }, c => c.WithName("ping").WithRunMode(RunMode.Async));

                x.AddCommand("help", async(commandContext, noidea, serviceProvider, commandInfo) =>
                {
                    using (var command = new DiscordHelpWorkFlow((SocketCommandContext)commandContext, _client, serviceProvider.Get <DiscordSettingsProvider>()))
                    {
                        await command.HandleHelpAsync();
                    }
                }, c => c.WithName("help").WithRunMode(RunMode.Async));

                if (discordSettings.MovieDownloadClient != DownloadClient.Disabled)
                {
                    x.AddCommand(discordSettings.MovieCommand, async(commandContext, message, serviceProvider, commandInfo) =>
                    {
                        using (var command = new DiscordMovieRequestingWorkFlow(
                                   (SocketCommandContext)commandContext,
                                   _client,
                                   GetMovieClient <IMovieSearcher>(discordSettings),
                                   GetMovieClient <IMovieRequester>(discordSettings),
                                   serviceProvider.Get <DiscordSettingsProvider>(),
                                   _movieNotificationRequestRepository))
                        {
                            await command.HandleMovieRequestAsync(message[0].ToString());
                        }
                    }, c => c.WithName("movie").WithSummary($"The correct usage of this command is: ```{discordSettings.CommandPrefix}{discordSettings.MovieCommand} name of movie```").WithRunMode(RunMode.Async).AddParameter <string>("movieName", p => p.WithIsRemainder(true).WithIsOptional(false)));
                }

                if (discordSettings.TvShowDownloadClient != DownloadClient.Disabled)
                {
                    x.AddCommand(discordSettings.TvShowCommand, async(commandContext, message, serviceProvider, commandInfo) =>
                    {
                        using (var command = new DiscordTvShowsRequestingWorkFlow(
                                   (SocketCommandContext)commandContext,
                                   _client,
                                   GetTvShowClient <ITvShowSearcher>(discordSettings),
                                   GetTvShowClient <ITvShowRequester>(discordSettings),
                                   serviceProvider.Get <DiscordSettingsProvider>(),
                                   _tvShowNotificationRequestRepository))
                        {
                            await command.HandleTvShowRequestAsync(message[0].ToString());
                        }
                    }, c => c.WithName("tv").WithSummary($"The correct usage of this command is: ```{discordSettings.CommandPrefix}{discordSettings.TvShowCommand} name of tv show```").WithRunMode(RunMode.Async).AddParameter <string>("tvShowName", p => p.WithIsRemainder(true).WithIsOptional(false)));
                }
            });
        }
Beispiel #2
0
        public async Task NotifyAsync(string userId, Movie movie)
        {
            var user    = _discordClient.GetUser(ulong.Parse(userId));
            var channel = await user.GetOrCreateDMChannelAsync();

            await channel.SendMessageAsync($"The movie **{movie.Title}** you requested has finished downloading and will be available in a few minutes!", false, await DiscordMovieRequestingWorkFlow.GenerateMovieDetailsAsync(movie, user));
        }
        private static async Task NotifyUsersInChannel(Movie movie, HashSet<ulong> discordUserIds, HashSet<string> userNotified, SocketTextChannel channel)
        {
            var usersToMention = channel.Users.Where(x => discordUserIds.Contains(x.Id));
            await channel.SendMessageAsync($"The movie **{movie.Title}** has finished downloading!", false, await DiscordMovieRequestingWorkFlow.GenerateMovieDetailsAsync(movie));
            await SendUserMentionMessageAsync(channel, usersToMention);

            foreach (var user in usersToMention)
            {
                userNotified.Add(user.Id.ToString());
            }
        }
Beispiel #4
0
        public async Task <HashSet <string> > NotifyAsync(IReadOnlyCollection <string> userIds, Movie movie, 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();

                        await channel.SendMessageAsync($"The movie **{movie.Title}** you requested has finished downloading!", false, await DiscordMovieRequestingWorkFlow.GenerateMovieDetailsAsync(movie, user));

                        userNotified.Add(userId);
                    }
                    else if (!token.IsCancellationRequested && _discordClient.ConnectionState == ConnectionState.Connected)
                    {
                        userNotified.Add(userId);
                    }
                }
                catch (System.Exception ex)
                {
                    _logger.LogError(ex, "An error occurred while sending a movie notification to a specific user: " + ex.Message);
                }
            }

            return(userNotified);
        }
        private static async Task NotifyUsersInChannel(Movie movie, 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();
                messageBuilder.AppendLine($"The movie **{movie.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, await DiscordMovieRequestingWorkFlow.GenerateMovieDetailsAsync(movie));

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