Export role details in JSON format (#1101)

This commit is contained in:
Adam Slatinský 2023-07-03 17:18:30 +02:00 committed by GitHub
parent d4f62387a5
commit 768fb88c8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 9 deletions

View file

@ -91,21 +91,26 @@ internal class ExportContext
public Color? TryGetUserColor(Snowflake id) public Color? TryGetUserColor(Snowflake id)
{ {
var member = TryGetMember(id); var memberRoles = GetUserRoles(id);
var memberRoles = member?
.RoleIds
.Select(TryGetRole)
.WhereNotNull()
.ToArray();
return memberRoles? return memberRoles?
.Where(r => r.Color is not null) .Where(r => r.Color is not null)
.OrderByDescending(r => r.Position)
.Select(r => r.Color) .Select(r => r.Color)
.FirstOrDefault(); .FirstOrDefault();
} }
public IReadOnlyList<Role> GetUserRoles(Snowflake id)
{
var member = TryGetMember(id);
return member?
.RoleIds
.Select(TryGetRole)
.WhereNotNull()
.OrderByDescending(r => r.Position)
.ToArray() ?? Array.Empty<Role>();
}
public async ValueTask<string> ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default) public async ValueTask<string> ResolveAssetUrlAsync(string url, CancellationToken cancellationToken = default)
{ {
if (!Request.ShouldDownloadAssets) if (!Request.ShouldDownloadAssets)

View file

@ -1,4 +1,5 @@
using System.IO; using System.Collections.Generic;
using System.IO;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
@ -48,6 +49,9 @@ internal class JsonMessageWriter : MessageWriter
_writer.WriteString("color", Context.TryGetUserColor(user.Id)?.ToHex()); _writer.WriteString("color", Context.TryGetUserColor(user.Id)?.ToHex());
_writer.WriteBoolean("isBot", user.IsBot); _writer.WriteBoolean("isBot", user.IsBot);
_writer.WritePropertyName("roles");
await WriteRolesAsync(Context.GetUserRoles(user.Id), cancellationToken);
_writer.WriteString( _writer.WriteString(
"avatarUrl", "avatarUrl",
await Context.ResolveAssetUrlAsync( await Context.ResolveAssetUrlAsync(
@ -60,6 +64,28 @@ internal class JsonMessageWriter : MessageWriter
await _writer.FlushAsync(cancellationToken); await _writer.FlushAsync(cancellationToken);
} }
private async ValueTask WriteRolesAsync(
IReadOnlyList<Role> roles,
CancellationToken cancellationToken = default)
{
_writer.WriteStartArray();
foreach (var role in roles)
{
_writer.WriteStartObject();
_writer.WriteString("id", role.Id.ToString());
_writer.WriteString("name", role.Name);
_writer.WriteString("color", role.Color?.ToHex());
_writer.WriteNumber("position", role.Position);
_writer.WriteEndObject();
}
_writer.WriteEndArray();
await _writer.FlushAsync(cancellationToken);
}
private async ValueTask WriteEmbedAuthorAsync( private async ValueTask WriteEmbedAuthorAsync(
EmbedAuthor embedAuthor, EmbedAuthor embedAuthor,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)