mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2025-06-08 02:14:42 -04:00
Clean up
This commit is contained in:
parent
d00ecf0c37
commit
1ba0057174
3 changed files with 28 additions and 30 deletions
|
@ -189,17 +189,17 @@ public class DiscordClient
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync(url, cancellationToken);
|
var response = await GetJsonResponseAsync(url, cancellationToken);
|
||||||
|
|
||||||
var isEmpty = true;
|
var count = 0;
|
||||||
foreach (var guildJson in response.EnumerateArray())
|
foreach (var guildJson in response.EnumerateArray())
|
||||||
{
|
{
|
||||||
var guild = Guild.Parse(guildJson);
|
var guild = Guild.Parse(guildJson);
|
||||||
yield return guild;
|
yield return guild;
|
||||||
|
|
||||||
currentAfter = guild.Id;
|
currentAfter = guild.Id;
|
||||||
isEmpty = false;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isEmpty)
|
if (count <= 0)
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -491,38 +491,36 @@ public class DiscordClient
|
||||||
Emoji emoji,
|
Emoji emoji,
|
||||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
var reactionName = emoji.Id is not null
|
||||||
var reactionName = Uri.EscapeDataString(
|
|
||||||
emoji.Id is not null
|
|
||||||
// Custom emoji
|
// Custom emoji
|
||||||
? emoji.Name + ':' + emoji.Id
|
? emoji.Name + ':' + emoji.Id
|
||||||
// Standard emoji
|
// Standard emoji
|
||||||
: emoji.Name
|
: emoji.Name;
|
||||||
);
|
|
||||||
|
|
||||||
var currentAfter = Snowflake.Zero;
|
var currentAfter = Snowflake.Zero;
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
var url = new UrlBuilder()
|
var url = new UrlBuilder()
|
||||||
.SetPath($"channels/{channelId}/messages/{messageId}/reactions/{reactionName}")
|
.SetPath($"channels/{channelId}/messages/{messageId}/reactions/{Uri.EscapeDataString(reactionName)}")
|
||||||
.SetQueryParameter("limit", "100")
|
.SetQueryParameter("limit", "100")
|
||||||
.SetQueryParameter("after", currentAfter.ToString())
|
.SetQueryParameter("after", currentAfter.ToString())
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var response = await GetJsonResponseAsync(url, cancellationToken);
|
var response = await GetJsonResponseAsync(url, cancellationToken);
|
||||||
|
|
||||||
var users = response.EnumerateArray().Select(User.Parse).ToArray();
|
var count = 0;
|
||||||
if (!users.Any())
|
foreach (var userJson in response.EnumerateArray())
|
||||||
yield break;
|
|
||||||
foreach (var user in users)
|
|
||||||
{
|
{
|
||||||
|
var user = User.Parse(userJson);
|
||||||
yield return user;
|
yield return user;
|
||||||
|
|
||||||
currentAfter = user.Id;
|
currentAfter = user.Id;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each batch can contain up to 100 users.
|
// Each batch can contain up to 100 users.
|
||||||
// If we got fewer, then it's definitely the last batch.
|
// If we got fewer, then it's definitely the last batch.
|
||||||
if (users.Length < 100)
|
if (count < 100)
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -361,16 +361,16 @@ internal class JsonMessageWriter : MessageWriter
|
||||||
_writer.WriteNumber("count", reaction.Count);
|
_writer.WriteNumber("count", reaction.Count);
|
||||||
|
|
||||||
_writer.WriteStartArray("users");
|
_writer.WriteStartArray("users");
|
||||||
var users = await Context.Discord.GetMessageReactionsAsync(Context.Request.Channel.Id, message.Id, reaction.Emoji, cancellationToken);
|
await foreach (var user in Context.Discord.GetMessageReactionsAsync(
|
||||||
foreach (var user in users) {
|
Context.Request.Channel.Id,
|
||||||
|
message.Id,
|
||||||
// write limited user information without color and roles,
|
reaction.Emoji,
|
||||||
// because if we would write the full user information,
|
cancellationToken))
|
||||||
// we would have to fetch the guild member information for each user
|
{
|
||||||
// which would be a lot of requests
|
|
||||||
|
|
||||||
_writer.WriteStartObject();
|
_writer.WriteStartObject();
|
||||||
|
|
||||||
|
// Write limited user information without color and roles,
|
||||||
|
// so we can avoid fetching guild member information for each user.
|
||||||
_writer.WriteString("id", user.Id.ToString());
|
_writer.WriteString("id", user.Id.ToString());
|
||||||
_writer.WriteString("name", user.Name);
|
_writer.WriteString("name", user.Name);
|
||||||
_writer.WriteString("discriminator", user.DiscriminatorFormatted);
|
_writer.WriteString("discriminator", user.DiscriminatorFormatted);
|
||||||
|
@ -386,7 +386,6 @@ internal class JsonMessageWriter : MessageWriter
|
||||||
);
|
);
|
||||||
|
|
||||||
_writer.WriteEndObject();
|
_writer.WriteEndObject();
|
||||||
await _writer.FlushAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_writer.WriteEndArray();
|
_writer.WriteEndArray();
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System.Text;
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Utils.Extensions;
|
namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ public static class BinaryExtensions
|
||||||
foreach (var b in data)
|
foreach (var b in data)
|
||||||
{
|
{
|
||||||
buffer.Append(
|
buffer.Append(
|
||||||
b.ToString(isUpperCase ? "X2" : "x2")
|
b.ToString(isUpperCase ? "X2" : "x2", CultureInfo.InvariantCulture)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue