From 8dd6e3dab26e27731b2a3ab96b8b8830e91a8a7c Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Thu, 23 Dec 2021 09:57:44 -0600 Subject: [PATCH] organization --- src/channel-renderer.ts | 9 +- src/collect-interaction-handlers.ts | 20 ++++ ...node-tree.ts => create-message-options.ts} | 93 +------------------ src/jsx.d.ts | 2 +- src/node.ts | 72 ++++++++++++++ src/reconciler.ts | 2 +- 6 files changed, 102 insertions(+), 96 deletions(-) create mode 100644 src/collect-interaction-handlers.ts rename src/{node-tree.ts => create-message-options.ts} (64%) create mode 100644 src/node.ts diff --git a/src/channel-renderer.ts b/src/channel-renderer.ts index e1b6f66..6bde1b9 100644 --- a/src/channel-renderer.ts +++ b/src/channel-renderer.ts @@ -7,8 +7,9 @@ import type { } from "discord.js" import type { Action } from "./action-queue.js" import { ActionQueue } from "./action-queue.js" -import type { MessageNode } from "./node-tree.js" -import { collectInteractionHandlers, getMessageOptions } from "./node-tree.js" +import { collectInteractionHandlers } from "./collect-interaction-handlers" +import { createMessageOptions } from "./create-message-options" +import type { MessageNode } from "./node.js" export class ChannelRenderer { private channel: TextBasedChannels @@ -60,12 +61,12 @@ export class ChannelRenderer { return this.actions.done() } - private createUpdateMessageAction(tree: MessageNode): Action { + private createUpdateMessageAction(node: MessageNode): Action { return { id: "updateMessage", priority: 0, run: async () => { - const options = getMessageOptions(tree) + const options = createMessageOptions(node) // eslint-disable-next-line unicorn/prefer-ternary if (this.message) { diff --git a/src/collect-interaction-handlers.ts b/src/collect-interaction-handlers.ts new file mode 100644 index 0000000..9b730cd --- /dev/null +++ b/src/collect-interaction-handlers.ts @@ -0,0 +1,20 @@ +import type { ButtonInteraction } from "discord.js" +import type { Node } from "./node" + +type InteractionHandler = { + type: "button" + customId: string + onClick: (interaction: ButtonInteraction) => void +} + +export function collectInteractionHandlers(node: Node): InteractionHandler[] { + if (node.type === "button") { + return [{ type: "button", customId: node.customId, onClick: node.onClick }] + } + + if ("children" in node) { + return node.children.flatMap(collectInteractionHandlers) + } + + return [] +} diff --git a/src/node-tree.ts b/src/create-message-options.ts similarity index 64% rename from src/node-tree.ts rename to src/create-message-options.ts index 05e0e5c..e70685a 100644 --- a/src/node-tree.ts +++ b/src/create-message-options.ts @@ -1,83 +1,14 @@ import type { BaseMessageComponentOptions, - ButtonInteraction, - ColorResolvable, - EmojiResolvable, MessageActionRowOptions, MessageEmbedOptions, MessageOptions, } from "discord.js" -import type { ButtonStyle } from "./components/button.js" import { last } from "./helpers/last.js" import { toUpper } from "./helpers/to-upper.js" +import type { EmbedNode, MessageNode, Node } from "./node" -export type MessageNode = { - type: "message" - children: Node[] -} - -export type TextNode = { - type: "text" - text: string -} - -type TextElementNode = { - type: "textElement" - children: Node[] -} - -type EmbedNode = { - type: "embed" - title?: string - color?: ColorResolvable - url?: string - timestamp?: Date | number | string - imageUrl?: string - thumbnailUrl?: string - author?: { - name: string - url?: string - iconUrl?: string - } - footer?: { - text: string - iconUrl?: string - } - children: Node[] -} - -type EmbedFieldNode = { - type: "embedField" - name: string - inline?: boolean - children: Node[] -} - -type ActionRowNode = { - type: "actionRow" - children: Node[] -} - -type ButtonNode = { - type: "button" - style?: ButtonStyle - emoji?: EmojiResolvable - disabled?: boolean - customId: string - onClick: (interaction: ButtonInteraction) => void - children: Node[] -} - -export type Node = - | MessageNode - | TextNode - | TextElementNode - | EmbedNode - | EmbedFieldNode - | ActionRowNode - | ButtonNode - -export function getMessageOptions(node: MessageNode): MessageOptions { +export function createMessageOptions(node: MessageNode): MessageOptions { if (node.children.length === 0) { // can't send an empty message return { content: "_ _" } @@ -183,7 +114,7 @@ function addActionRowItems(components: ActionRowOptions[], nodes: Node[]) { if (node.type === "button") { actionRow.components.push({ type: "BUTTON", - label: node.children.map(getNodeText).join("") || "_ _", + label: node.children.map(getNodeText).join(""), style: node.style ? toUpper(node.style) : "SECONDARY", emoji: node.emoji, disabled: node.disabled, @@ -192,21 +123,3 @@ function addActionRowItems(components: ActionRowOptions[], nodes: Node[]) { } } } - -type InteractionHandler = { - type: "button" - customId: string - onClick: (interaction: ButtonInteraction) => void -} - -export function collectInteractionHandlers(node: Node): InteractionHandler[] { - if (node.type === "button") { - return [{ type: "button", customId: node.customId, onClick: node.onClick }] - } - - if ("children" in node) { - return node.children.flatMap(collectInteractionHandlers) - } - - return [] -} diff --git a/src/jsx.d.ts b/src/jsx.d.ts index 2311575..fc1ca15 100644 --- a/src/jsx.d.ts +++ b/src/jsx.d.ts @@ -1,5 +1,5 @@ import type { ReactNode } from "react" -import type { Node } from "./node-tree" +import type { Node } from "./node" declare global { namespace JSX { diff --git a/src/node.ts b/src/node.ts new file mode 100644 index 0000000..5326f40 --- /dev/null +++ b/src/node.ts @@ -0,0 +1,72 @@ +import type { + ButtonInteraction, + ColorResolvable, + EmojiResolvable, +} from "discord.js" +import type { ButtonStyle } from "./components/button.jsx" + +export type MessageNode = { + type: "message" + children: Node[] +} + +export type TextNode = { + type: "text" + text: string +} + +type TextElementNode = { + type: "textElement" + children: Node[] +} + +export type EmbedNode = { + type: "embed" + title?: string + color?: ColorResolvable + url?: string + timestamp?: Date | number | string + imageUrl?: string + thumbnailUrl?: string + author?: { + name: string + url?: string + iconUrl?: string + } + footer?: { + text: string + iconUrl?: string + } + children: Node[] +} + +type EmbedFieldNode = { + type: "embedField" + name: string + inline?: boolean + children: Node[] +} + +type ActionRowNode = { + type: "actionRow" + children: Node[] +} + +type ButtonNode = { + type: "button" + style?: ButtonStyle + emoji?: EmojiResolvable + disabled?: boolean + customId: string + onClick: (interaction: ButtonInteraction) => void + children: Node[] +} + +export type Node = + | MessageNode + | TextNode + | TextElementNode + | EmbedNode + | EmbedFieldNode + | ActionRowNode + | ButtonNode diff --git a/src/reconciler.ts b/src/reconciler.ts index 6f896e2..b5755c8 100644 --- a/src/reconciler.ts +++ b/src/reconciler.ts @@ -3,7 +3,7 @@ import { inspect } from "node:util" import ReactReconciler from "react-reconciler" import type { ChannelRenderer } from "./channel-renderer.js" import { raise } from "./helpers/raise.js" -import type { MessageNode, Node, TextNode } from "./node-tree.js" +import type { MessageNode, Node, TextNode } from "./node.js" type ElementTag = string