mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-02-02 08:09:16 +00:00
Small cleanup
This commit is contained in:
@@ -18,7 +18,8 @@ namespace DiscordChatExporter.Models
|
||||
|
||||
public IReadOnlyList<Attachment> Attachments { get; }
|
||||
|
||||
public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content, IEnumerable<Attachment> attachments)
|
||||
public Message(string id, DateTime timeStamp, DateTime? editedTimeStamp, User author, string content,
|
||||
IEnumerable<Attachment> attachments)
|
||||
{
|
||||
Id = id;
|
||||
TimeStamp = timeStamp;
|
||||
|
||||
@@ -8,11 +8,16 @@ using Tyrrrz.Extensions;
|
||||
|
||||
namespace DiscordChatExporter.Services
|
||||
{
|
||||
public class DiscordApiService
|
||||
public class DiscordApiService : IDisposable
|
||||
{
|
||||
private const string ApiRoot = "https://discordapp.com/api";
|
||||
private readonly HttpClient _httpClient = new HttpClient();
|
||||
|
||||
~DiscordApiService()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
private IEnumerable<Message> ParseMessages(string json)
|
||||
{
|
||||
var messagesJson = JArray.Parse(json);
|
||||
@@ -35,9 +40,8 @@ namespace DiscordChatExporter.Services
|
||||
var authorAvatarHash = authorJson.Value<string>("avatar");
|
||||
|
||||
// Get attachment
|
||||
var attachmentsJson = messageJson["attachments"];
|
||||
var attachments = new List<Attachment>();
|
||||
foreach (var attachmentJson in attachmentsJson)
|
||||
foreach (var attachmentJson in messageJson["attachments"].EmptyIfNull())
|
||||
{
|
||||
var attachmentId = attachmentJson.Value<string>("id");
|
||||
var attachmentUrl = attachmentJson.Value<string>("url");
|
||||
@@ -60,7 +64,7 @@ namespace DiscordChatExporter.Services
|
||||
var result = new List<Message>();
|
||||
|
||||
// We are going backwards from last message to first
|
||||
// ...collecting everything between them in batches
|
||||
// collecting everything between them in batches
|
||||
string beforeId = null;
|
||||
while (true)
|
||||
{
|
||||
@@ -95,5 +99,19 @@ namespace DiscordChatExporter.Services
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_httpClient.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user