Export role details in JSON format (#1101)

pull/1046/head
Adam Slatinský 1 year ago committed by GitHub
parent d4f62387a5
commit 768fb88c8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -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)

Loading…
Cancel
Save