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); } } }
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); }