simplify adapter extension
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import type * as Discord from "discord.js"
|
||||
import { raise } from "../../helpers/raise"
|
||||
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 { Message, MessageOptions } from "../internal/message"
|
||||
import type { ReacordConfig, ReacordInstance } from "./reacord"
|
||||
@@ -23,8 +25,8 @@ export class ReacordDiscordJs extends Reacord {
|
||||
channelId: string,
|
||||
initialContent?: React.ReactNode,
|
||||
): ReacordInstance {
|
||||
return this.createChannelRendererInstance(
|
||||
{
|
||||
return this.createInstance(
|
||||
new ChannelMessageRenderer({
|
||||
send: async (options) => {
|
||||
const channel =
|
||||
this.client.channels.cache.get(channelId) ??
|
||||
@@ -38,7 +40,7 @@ export class ReacordDiscordJs extends Reacord {
|
||||
const message = await channel.send(getDiscordMessageOptions(options))
|
||||
return createReacordMessage(message)
|
||||
},
|
||||
},
|
||||
}),
|
||||
initialContent,
|
||||
)
|
||||
}
|
||||
@@ -47,8 +49,8 @@ export class ReacordDiscordJs extends Reacord {
|
||||
interaction: Discord.CommandInteraction,
|
||||
initialContent?: React.ReactNode,
|
||||
): ReacordInstance {
|
||||
return this.createCommandReplyRendererInstance(
|
||||
{
|
||||
return this.createInstance(
|
||||
new CommandReplyRenderer({
|
||||
type: "command",
|
||||
id: interaction.id,
|
||||
channelId: interaction.channelId,
|
||||
@@ -66,7 +68,7 @@ export class ReacordDiscordJs extends Reacord {
|
||||
})
|
||||
return createReacordMessage(message as Discord.Message)
|
||||
},
|
||||
},
|
||||
}),
|
||||
initialContent,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import { logPretty } from "../../helpers/log-pretty"
|
||||
import { omit } from "../../helpers/omit"
|
||||
import { raise } from "../../helpers/raise"
|
||||
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 type {
|
||||
ButtonInteraction,
|
||||
@@ -36,15 +38,17 @@ export class ReacordTester extends Reacord {
|
||||
return [...this.messageContainer]
|
||||
}
|
||||
|
||||
send(): ReacordInstance {
|
||||
return this.createChannelRendererInstance(
|
||||
new TestChannel(this.messageContainer),
|
||||
override send(): ReacordInstance {
|
||||
return this.createInstance(
|
||||
new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
|
||||
)
|
||||
}
|
||||
|
||||
reply(): ReacordInstance {
|
||||
return this.createCommandReplyRendererInstance(
|
||||
new TestCommandInteraction(this.messageContainer),
|
||||
override reply(): ReacordInstance {
|
||||
return this.createInstance(
|
||||
new CommandReplyRenderer(
|
||||
new TestCommandInteraction(this.messageContainer),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -177,8 +181,7 @@ class TestCommandInteraction implements CommandInteraction {
|
||||
}
|
||||
}
|
||||
|
||||
class TestButtonInteraction implements ButtonInteraction {
|
||||
readonly type = "button"
|
||||
class TestInteraction {
|
||||
readonly id = nanoid()
|
||||
readonly channelId = "test-channel-id"
|
||||
|
||||
@@ -187,21 +190,29 @@ class TestButtonInteraction implements ButtonInteraction {
|
||||
async update(options: MessageOptions): Promise<void> {
|
||||
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 id = nanoid()
|
||||
readonly channelId = "test-channel-id"
|
||||
|
||||
constructor(
|
||||
readonly customId: string,
|
||||
readonly message: TestMessage,
|
||||
customId: string,
|
||||
message: TestMessage,
|
||||
readonly values: string[],
|
||||
) {}
|
||||
|
||||
async update(options: MessageOptions): Promise<void> {
|
||||
this.message.options = options
|
||||
) {
|
||||
super(customId, message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
import type { ReactNode } from "react"
|
||||
import type { Channel } from "../internal/channel"
|
||||
import { ChannelMessageRenderer } from "../internal/channel-message-renderer"
|
||||
import { CommandReplyRenderer } from "../internal/command-reply-renderer.js"
|
||||
import type {
|
||||
CommandInteraction,
|
||||
ComponentInteraction,
|
||||
} from "../internal/interaction"
|
||||
import type { ComponentInteraction } from "../internal/interaction"
|
||||
import { reconciler } from "../internal/reconciler.js"
|
||||
import type { Renderer } from "../internal/renderer"
|
||||
|
||||
@@ -49,27 +43,7 @@ export abstract class Reacord {
|
||||
return this.config.maxInstances ?? 50
|
||||
}
|
||||
|
||||
protected createChannelRendererInstance(
|
||||
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) {
|
||||
protected createInstance(renderer: Renderer, initialContent?: ReactNode) {
|
||||
if (this.renderers.length > this.maxInstances) {
|
||||
this.deactivate(this.renderers[0]!)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user