public async Task <DiscordSearchResult> GetAllMessagesByUserInChannel(string channelId, string userId, bool isGuild, bool onlyNormalMessages, Utils.RateLimitCallbackDelegate rateLimitCallback = null, SearchProgressCallbackDelegate progressCallback = null, CancellationToken ct = default) { var combinedSearchResult = new DiscordSearchResult(); int currentOffset = 0; while (currentOffset <= 5000) { var currentSearchResults = await InternalMakeDiscordSearchRequest(channelId, userId, currentOffset, isGuild, rateLimitCallback, ct); int currentMessageCount = currentSearchResults.messageList.Count; if (currentMessageCount == 0) { break; } currentOffset += currentMessageCount; progressCallback?.Invoke(currentOffset); combinedSearchResult.messageList.AddRange(currentSearchResults.messageList); } if (onlyNormalMessages) { combinedSearchResult.messageList = combinedSearchResult.messageList.Where(x => x.Type == 0 || x.Type == 19 || x.Type == 20).ToList(); } //We have to be sure that there are no duplicates combinedSearchResult.messageList = combinedSearchResult.messageList.DistinctBy(x => x.Id).ToList(); combinedSearchResult.TotalResults = combinedSearchResult.messageList.Count; return(combinedSearchResult); }
private async Task <DiscordSearchResult> InternalMakeDiscordSearchRequest(string channelId, string userId, int offset, bool isGuild, Utils.RateLimitCallbackDelegate rateLimitCallback = null, CancellationToken ct = default) { string targetRestUrl = "/" + (isGuild ? "guilds" : "channels"); targetRestUrl += $"/{channelId}/messages/search?author_id={userId}"; targetRestUrl += (offset != 0) ? $"&offset={offset}" : ""; QuickType.SearchRequestResponse result = null; while (result == null || result.Messages == null) { result = QuickType.SearchRequestResponse.FromJson(await Utils.HttpGetStringAndWaitRatelimit(httpClient, discordApiUrl + targetRestUrl, rateLimitCallback, ct)); } DiscordSearchResult search_result = new DiscordSearchResult { TotalResults = result.TotalResults }; search_result.messageList.AddRange(result.Messages.SelectMany(messageChunk => messageChunk .Where(message => message.Author.Id == userId)).DistinctBy(x => x.Id)); return(search_result); }