embed children

This commit is contained in:
MapleLeaf
2021-12-25 03:54:35 -06:00
parent 6f3c97812c
commit bce472ab37
7 changed files with 130 additions and 44 deletions

View File

@@ -1,9 +1,9 @@
import type { MessageOptions } from "discord.js"
import type { MessageEmbedOptions, MessageOptions } from "discord.js"
import React from "react"
import { ReacordElement } from "./element.js"
import { Node } from "./node.js"
export type EmbedProps = {
title?: string
description?: string
url?: string
timestamp?: Date
@@ -23,22 +23,78 @@ export type EmbedProps = {
url?: string
iconURL?: string
}
fields?: Array<{
name: string
value: string
inline?: boolean
}>
children?: React.ReactNode
}
export function Embed(props: EmbedProps) {
return (
<reacord-element props={props} createNode={() => new EmbedNode(props)} />
<ReacordElement props={props} createNode={() => new EmbedNode(props)}>
{props.children}
</ReacordElement>
)
}
class EmbedNode extends Node<EmbedProps> {
override modifyMessageOptions(options: MessageOptions): void {
const embed = { ...this.props }
for (const child of this.children) {
if (child instanceof EmbedChildNode) {
child.modifyEmbedOptions(embed)
}
}
options.embeds ??= []
options.embeds.push(this.props)
options.embeds.push(embed)
}
}
abstract class EmbedChildNode<Props> extends Node<Props> {
abstract modifyEmbedOptions(options: MessageEmbedOptions): void
}
export type EmbedTitleProps = {
children: string
url?: string
}
Embed.Title = function Title(props: EmbedTitleProps) {
return (
<ReacordElement
props={props}
createNode={() => new EmbedTitleNode(props)}
/>
)
}
class EmbedTitleNode extends EmbedChildNode<EmbedTitleProps> {
override modifyEmbedOptions(options: MessageEmbedOptions): void {
options.title = this.props.children
options.url = this.props.url
}
}
export type EmbedFieldProps = {
name: string
inline?: boolean
children: string
}
Embed.Field = function Field(props: EmbedFieldProps) {
return (
<ReacordElement
props={props}
createNode={() => new EmbedFieldNode(props)}
/>
)
}
class EmbedFieldNode extends EmbedChildNode<EmbedFieldProps> {
override modifyEmbedOptions(options: MessageEmbedOptions): void {
options.fields ??= []
options.fields.push({
name: this.props.name,
value: this.props.children,
inline: this.props.inline,
})
}
}