embed author + some refactors

This commit is contained in:
MapleLeaf
2021-12-19 23:28:20 -06:00
parent 7cd0b683e2
commit a9c5122615
8 changed files with 171 additions and 59 deletions

View File

@@ -1,4 +1,4 @@
import type { MessageOptions } from "discord.js"
import type { MessageEmbedOptions, MessageOptions } from "discord.js"
export abstract class BaseInstance {
/** The name of the JSX element represented by this instance */
@@ -10,4 +10,8 @@ export abstract class BaseInstance {
/** If this element can be a child of a message,
* the function to modify the message options */
renderToMessage?(options: MessageOptions): void
/** If this element can be a child of an embed,
* the function to modify the embed options */
renderToEmbed?(options: MessageEmbedOptions): void
}

View File

@@ -0,0 +1,35 @@
import type { MessageEmbedOptions } from "discord.js"
import type { ReactNode } from "react"
import React from "react"
import { ContainerInstance } from "./container-instance.js"
export type EmbedAuthorProps = {
url?: string
iconUrl?: string
children?: ReactNode
}
export function EmbedAuthor({ children, ...options }: EmbedAuthorProps) {
return (
<reacord-element createInstance={() => new EmbedAuthorInstance(options)}>
{children}
</reacord-element>
)
}
type EmbedAuthorOptions = Omit<EmbedAuthorProps, "children">
class EmbedAuthorInstance extends ContainerInstance {
readonly name = "EmbedAuthor"
constructor(private readonly props: EmbedAuthorOptions) {
super({ warnOnNonTextChildren: true })
}
override renderToEmbed(options: MessageEmbedOptions) {
options.author ??= {}
options.author.name = this.getChildrenText()
options.author.url = this.props.url
options.author.iconURL = this.props.iconUrl
}
}

View File

@@ -33,9 +33,26 @@ class EmbedInstance extends ContainerInstance {
}
get embedOptions(): MessageEmbedOptions {
return {
/* eslint-disable unicorn/no-null */
const options: MessageEmbedOptions = {
color: this.color,
description: this.getChildrenText() || "_ _",
description: null as unknown as undefined,
}
/* eslint-enable unicorn/no-null */
for (const child of this.children) {
if (!child.renderToEmbed) {
console.warn(`${child.name} is not a valid child of ${this.name}`)
continue
}
child.renderToEmbed(options)
}
// can't render an empty embed
if (!options.description) {
options.description = "_ _"
}
return options
}
}

View File

@@ -1,3 +1,4 @@
export * from "./embed-author.js"
export * from "./embed.js"
export * from "./root.js"
export * from "./text.js"

View File

@@ -6,6 +6,8 @@ import { reconciler } from "./reconciler"
export type ReacordRenderTarget = TextBasedChannels
export type ReacordRoot = ReturnType<typeof createRoot>
export function createRoot(target: ReacordRenderTarget) {
const container = new ReacordContainer(target)
const containerId = reconciler.createContainer(container, 0, false, null)

View File

@@ -1,4 +1,4 @@
import type { MessageOptions } from "discord.js"
import type { MessageEmbedOptions, MessageOptions } from "discord.js"
import { BaseInstance } from "./base-instance.js"
/** Represents raw strings in JSX */
@@ -16,4 +16,8 @@ export class TextInstance extends BaseInstance {
override renderToMessage(options: MessageOptions) {
options.content = (options.content ?? "") + this.getText()
}
override renderToEmbed(options: MessageEmbedOptions) {
options.description = (options.description ?? "") + this.getText()
}
}

View File

@@ -1,4 +1,4 @@
import type { MessageOptions } from "discord.js"
import type { MessageEmbedOptions, MessageOptions } from "discord.js"
import type { ReactNode } from "react"
import React from "react"
import { ContainerInstance } from "./container-instance.js"
@@ -29,4 +29,8 @@ class TextElementInstance extends ContainerInstance {
override renderToMessage(options: MessageOptions) {
options.content = (options.content ?? "") + this.getText()
}
override renderToEmbed(options: MessageEmbedOptions) {
options.description = (options.description ?? "") + this.getText()
}
}