mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-06-10 00:02:37 -06:00
refactor: make MessageComponent accept type
This commit is contained in:
parent
0414da2fb7
commit
2bf8b86243
|
|
@ -7,9 +7,9 @@ using JsonExtensions.Reading;
|
||||||
|
|
||||||
namespace DiscordChatExporter.Core.Discord.Data.Components;
|
namespace DiscordChatExporter.Core.Discord.Data.Components;
|
||||||
|
|
||||||
// https://docs.discord.com/developers/components/reference#what-is-a-component
|
// https://docs.discord.com/developers/components/reference#component-object
|
||||||
public partial record MessageComponent(
|
public partial record MessageComponent(
|
||||||
MessageComponentKind Kind,
|
MessageComponentType Kind,
|
||||||
IReadOnlyList<MessageComponent> Components,
|
IReadOnlyList<MessageComponent> Components,
|
||||||
ButtonComponent? Button
|
ButtonComponent? Button
|
||||||
)
|
)
|
||||||
|
|
@ -29,21 +29,43 @@ public partial record MessageComponent
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var type = rawType.Value;
|
var type = rawType.Value;
|
||||||
if (!Enum.IsDefined(typeof(MessageComponentKind), type))
|
if (!Enum.IsDefined(typeof(MessageComponentType), type))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var kind = (MessageComponentKind)type;
|
return Parse((MessageComponentType)type, json);
|
||||||
|
}
|
||||||
|
|
||||||
var components =
|
private static MessageComponent Parse(MessageComponentType type, JsonElement json)
|
||||||
json.GetPropertyOrNull("components")
|
{
|
||||||
|
return type switch
|
||||||
|
{
|
||||||
|
MessageComponentType.Button => ParseButton(json),
|
||||||
|
_ => ParseDefault(type, json),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageComponent ParseDefault(MessageComponentType type, JsonElement json)
|
||||||
|
{
|
||||||
|
var components = ParseComponents(json);
|
||||||
|
|
||||||
|
return new MessageComponent(type, components, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageComponent ParseButton(JsonElement json)
|
||||||
|
{
|
||||||
|
var components = ParseComponents(json);
|
||||||
|
var button = ButtonComponent.Parse(json);
|
||||||
|
|
||||||
|
return new MessageComponent(MessageComponentType.Button, components, button);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MessageComponent[] ParseComponents(JsonElement json)
|
||||||
|
{
|
||||||
|
return json.GetPropertyOrNull("components")
|
||||||
?.EnumerateArrayOrNull()
|
?.EnumerateArrayOrNull()
|
||||||
?.Select(Parse)
|
?.Select(Parse)
|
||||||
.WhereNotNull()
|
.WhereNotNull()
|
||||||
.ToArray()
|
.ToArray()
|
||||||
?? [];
|
?? [];
|
||||||
|
|
||||||
var button = kind == MessageComponentKind.Button ? ButtonComponent.Parse(json) : null;
|
|
||||||
|
|
||||||
return new MessageComponent(kind, components, button);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
namespace DiscordChatExporter.Core.Discord.Data.Components;
|
namespace DiscordChatExporter.Core.Discord.Data.Components;
|
||||||
|
|
||||||
// https://discord.com/developers/docs/components/reference#component-object-component-types
|
// https://discord.com/developers/docs/components/reference#component-object-component-types
|
||||||
public enum MessageComponentKind
|
public enum MessageComponentType
|
||||||
{
|
{
|
||||||
ActionRow = 1,
|
ActionRow = 1,
|
||||||
Button = 2,
|
Button = 2,
|
||||||
|
|
@ -733,10 +733,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
@* Components *@
|
@* Components *@
|
||||||
@if (message.Components.Any(c => c.Kind == MessageComponentKind.ActionRow && c.HasButtons))
|
@if (message.Components.Any(c => c.Kind == MessageComponentType.ActionRow && c.HasButtons))
|
||||||
{
|
{
|
||||||
<div class="chatlog__components">
|
<div class="chatlog__components">
|
||||||
@foreach (var actionRow in message.Components.Where(c => c.Kind == MessageComponentKind.ActionRow && c.HasButtons))
|
@foreach (var actionRow in message.Components.Where(c => c.Kind == MessageComponentType.ActionRow && c.HasButtons))
|
||||||
{
|
{
|
||||||
<div class="chatlog__action-row">
|
<div class="chatlog__action-row">
|
||||||
@foreach (var button in actionRow.Buttons)
|
@foreach (var button in actionRow.Buttons)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue