Пример #1
0
        public async Task <ChatLog> GetChatLogAsync(AuthToken token, Guild guild, Channel channel,
                                                    DateTimeOffset?after = null, DateTimeOffset?before = null, IProgress <double> progress = null)
        {
            // Get messages
            IReadOnlyList <Message> messages = await GetChannelMessagesAsync(token, channel.Id, after, before, progress);

            // Get mentionables
            Mentionables mentionables = await GetMentionablesAsync(token, guild.Id, messages);

            return(new ChatLog(guild, channel, after, before, messages, mentionables));
        }
Пример #2
0
        public async Task GetAndExportChannelMessagesAsync(AuthToken token, Guild guild, Channel channel,
                                                           CsvChatLogRenderer renderer, DateTimeOffset?after = null, DateTimeOffset?before = null,
                                                           IProgress <double> progress = null)
        {
            Message      firstMsg = null, currentLastMsg = null;
            Mentionables mentionables = null;

            // Get the last message
            JToken response = await GetApiResponseAsync(token, "channels", $"{channel.Id}/messages",
                                                        "limit=1", $"before={before?.ToSnowflake()}");

            Message lastMessage = response.Select(ParseMessage).FirstOrDefault();

            // If the last message doesn't exist or it's outside of range - return
            if (lastMessage == null || lastMessage.Timestamp < after)
            {
                progress?.Report(1);
                return;
            }

            // Get other messages
            string offsetId = after?.ToSnowflake() ?? "0";

            while (true)
            {
                // Get message batch
                response = await GetApiResponseAsync(token, "channels", $"{channel.Id}/messages",
                                                     "limit=1000", $"after={offsetId}");

                // Parse
                Message[] messages = response
                                     .Select(ParseMessage)
                                     .Reverse() // reverse because messages appear newest first
                                     .ToArray();

                // Break if there are no messages (can happen if messages are deleted during execution)
                if (!messages.Any())
                {
                    break;
                }

                // Trim messages to range (until last message)
                Message[] messagesInRange = messages
                                            .TakeWhile(m => m.Id != lastMessage.Id && m.Timestamp < lastMessage.Timestamp)
                                            .ToArray();

                mentionables = await GetMentionablesAsync(token, guild.Id, messagesInRange);

                await renderer.RenderAsync(new ChatLog(guild, channel, after, before, messagesInRange, mentionables));

                // Break if messages were trimmed (which means the last message was encountered)
                if (messagesInRange.Length != messages.Length)
                {
                    break;
                }

                if (firstMsg == null)
                {
                    firstMsg = messagesInRange.First();
                }
                currentLastMsg = messagesInRange.Last();

                // Report progress (based on the time range of parsed messages compared to total)
                progress?.Report((currentLastMsg.Timestamp - firstMsg.Timestamp).TotalSeconds /
                                 (lastMessage.Timestamp - firstMsg.Timestamp).TotalSeconds);

                // Move offset
                offsetId = currentLastMsg.Id;
            }

            var lastMessageList = new Message[] { lastMessage };

            mentionables = await GetMentionablesAsync(token, guild.Id, lastMessageList);

            await renderer.RenderAsync(new ChatLog(guild, channel, after, before, lastMessageList, mentionables));

            // Report progress
            progress?.Report(1);
        }