simplify adapter extension

This commit is contained in:
MapleLeaf
2021-12-28 13:10:37 -06:00
parent f36d550963
commit f62b80287d
3 changed files with 38 additions and 51 deletions

View File

@@ -1,6 +1,8 @@
import type * as Discord from "discord.js" import type * as Discord from "discord.js"
import { raise } from "../../helpers/raise" import { raise } from "../../helpers/raise"
import { toUpper } from "../../helpers/to-upper" import { toUpper } from "../../helpers/to-upper"
import { ChannelMessageRenderer } from "../internal/channel-message-renderer"
import { CommandReplyRenderer } from "../internal/command-reply-renderer"
import type { ComponentInteraction } from "../internal/interaction" import type { ComponentInteraction } from "../internal/interaction"
import type { Message, MessageOptions } from "../internal/message" import type { Message, MessageOptions } from "../internal/message"
import type { ReacordConfig, ReacordInstance } from "./reacord" import type { ReacordConfig, ReacordInstance } from "./reacord"
@@ -23,8 +25,8 @@ export class ReacordDiscordJs extends Reacord {
channelId: string, channelId: string,
initialContent?: React.ReactNode, initialContent?: React.ReactNode,
): ReacordInstance { ): ReacordInstance {
return this.createChannelRendererInstance( return this.createInstance(
{ new ChannelMessageRenderer({
send: async (options) => { send: async (options) => {
const channel = const channel =
this.client.channels.cache.get(channelId) ?? this.client.channels.cache.get(channelId) ??
@@ -38,7 +40,7 @@ export class ReacordDiscordJs extends Reacord {
const message = await channel.send(getDiscordMessageOptions(options)) const message = await channel.send(getDiscordMessageOptions(options))
return createReacordMessage(message) return createReacordMessage(message)
}, },
}, }),
initialContent, initialContent,
) )
} }
@@ -47,8 +49,8 @@ export class ReacordDiscordJs extends Reacord {
interaction: Discord.CommandInteraction, interaction: Discord.CommandInteraction,
initialContent?: React.ReactNode, initialContent?: React.ReactNode,
): ReacordInstance { ): ReacordInstance {
return this.createCommandReplyRendererInstance( return this.createInstance(
{ new CommandReplyRenderer({
type: "command", type: "command",
id: interaction.id, id: interaction.id,
channelId: interaction.channelId, channelId: interaction.channelId,
@@ -66,7 +68,7 @@ export class ReacordDiscordJs extends Reacord {
}) })
return createReacordMessage(message as Discord.Message) return createReacordMessage(message as Discord.Message)
}, },
}, }),
initialContent, initialContent,
) )
} }

View File

@@ -8,6 +8,8 @@ import { logPretty } from "../../helpers/log-pretty"
import { omit } from "../../helpers/omit" import { omit } from "../../helpers/omit"
import { raise } from "../../helpers/raise" import { raise } from "../../helpers/raise"
import type { Channel } from "../internal/channel" import type { Channel } from "../internal/channel"
import { ChannelMessageRenderer } from "../internal/channel-message-renderer"
import { CommandReplyRenderer } from "../internal/command-reply-renderer"
import { Container } from "../internal/container" import { Container } from "../internal/container"
import type { import type {
ButtonInteraction, ButtonInteraction,
@@ -36,15 +38,17 @@ export class ReacordTester extends Reacord {
return [...this.messageContainer] return [...this.messageContainer]
} }
send(): ReacordInstance { override send(): ReacordInstance {
return this.createChannelRendererInstance( return this.createInstance(
new TestChannel(this.messageContainer), new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
) )
} }
reply(): ReacordInstance { override reply(): ReacordInstance {
return this.createCommandReplyRendererInstance( return this.createInstance(
new CommandReplyRenderer(
new TestCommandInteraction(this.messageContainer), new TestCommandInteraction(this.messageContainer),
),
) )
} }
@@ -177,8 +181,7 @@ class TestCommandInteraction implements CommandInteraction {
} }
} }
class TestButtonInteraction implements ButtonInteraction { class TestInteraction {
readonly type = "button"
readonly id = nanoid() readonly id = nanoid()
readonly channelId = "test-channel-id" readonly channelId = "test-channel-id"
@@ -187,21 +190,29 @@ class TestButtonInteraction implements ButtonInteraction {
async update(options: MessageOptions): Promise<void> { async update(options: MessageOptions): Promise<void> {
this.message.options = options this.message.options = options
} }
async deferUpdate(): Promise<void> {}
} }
class TestSelectInteraction implements SelectInteraction { class TestButtonInteraction
extends TestInteraction
implements ButtonInteraction
{
readonly type = "button"
}
class TestSelectInteraction
extends TestInteraction
implements SelectInteraction
{
readonly type = "select" readonly type = "select"
readonly id = nanoid()
readonly channelId = "test-channel-id"
constructor( constructor(
readonly customId: string, customId: string,
readonly message: TestMessage, message: TestMessage,
readonly values: string[], readonly values: string[],
) {} ) {
super(customId, message)
async update(options: MessageOptions): Promise<void> {
this.message.options = options
} }
} }

View File

@@ -1,11 +1,5 @@
import type { ReactNode } from "react" import type { ReactNode } from "react"
import type { Channel } from "../internal/channel" import type { ComponentInteraction } from "../internal/interaction"
import { ChannelMessageRenderer } from "../internal/channel-message-renderer"
import { CommandReplyRenderer } from "../internal/command-reply-renderer.js"
import type {
CommandInteraction,
ComponentInteraction,
} from "../internal/interaction"
import { reconciler } from "../internal/reconciler.js" import { reconciler } from "../internal/reconciler.js"
import type { Renderer } from "../internal/renderer" import type { Renderer } from "../internal/renderer"
@@ -49,27 +43,7 @@ export abstract class Reacord {
return this.config.maxInstances ?? 50 return this.config.maxInstances ?? 50
} }
protected createChannelRendererInstance( protected createInstance(renderer: Renderer, initialContent?: ReactNode) {
channel: Channel,
initialContent?: ReactNode,
) {
return this.createInstance(
new ChannelMessageRenderer(channel),
initialContent,
)
}
protected createCommandReplyRendererInstance(
commandInteraction: CommandInteraction,
initialContent?: ReactNode,
): ReacordInstance {
return this.createInstance(
new CommandReplyRenderer(commandInteraction),
initialContent,
)
}
private createInstance(renderer: Renderer, initialContent?: ReactNode) {
if (this.renderers.length > this.maxInstances) { if (this.renderers.length > this.maxInstances) {
this.deactivate(this.renderers[0]!) this.deactivate(this.renderers[0]!)
} }