Functiondiscord-component-embedv1.1.1
toComponentEmbed
Converts a Container tree into the JSON document Discord reads for a component embed. To write the JSON
into a page, use toComponentEmbedJson or toComponentEmbedScript. Both escape it for a <script> tag.
Throws:
A
ComponentEmbedErrorwhen the tree breaks a rule of the format.A
ComponentEmbedErrorwhen your own code throws while the package reads it. Checkerror.codeto tell the two apart.
toComponentEmbed(root: EmbedElement): ComponentEmbedPayloadconst payload = toComponentEmbed(
<Container accentColor={0xf8f6e8}>
<TextDisplay># seedcord</TextDisplay>
</Container>
);
// payload.component.accent_color is 16316136
// payload.component.components is [{ type: 10, content: '# seedcord' }]// catch a broken preview in CI
for (const page of await getAllGuidePages()) {
expect(() => toComponentEmbed(<GuidePreview page={page} />)).not.toThrow();
}root
root: EmbedElement