add channel renderer + try to simplify adapter generics
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import type { Channel } from "../../internal/channel"
|
||||
import type {
|
||||
CommandInteraction,
|
||||
ComponentInteraction,
|
||||
} from "../../internal/interaction"
|
||||
|
||||
export type Adapter<CommandReplyInit> = {
|
||||
export type AdapterGenerics = {
|
||||
commandReplyInit: unknown
|
||||
channelInit: unknown
|
||||
}
|
||||
|
||||
export type Adapter<Generics extends AdapterGenerics> = {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
@@ -14,5 +20,12 @@ export type Adapter<CommandReplyInit> = {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
createCommandInteraction(init: CommandReplyInit): CommandInteraction
|
||||
createCommandInteraction(
|
||||
init: Generics["commandReplyInit"],
|
||||
): CommandInteraction
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
createChannel(init: Generics["channelInit"]): Channel
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type * as Discord from "discord.js"
|
||||
import { raise } from "../../../helpers/raise"
|
||||
import { toUpper } from "../../../helpers/to-upper"
|
||||
import type { Channel } from "../../internal/channel"
|
||||
import type {
|
||||
CommandInteraction,
|
||||
ComponentInteraction,
|
||||
@@ -8,7 +9,12 @@ import type {
|
||||
import type { Message, MessageOptions } from "../../internal/message"
|
||||
import type { Adapter } from "./adapter"
|
||||
|
||||
export class DiscordJsAdapter implements Adapter<Discord.CommandInteraction> {
|
||||
type DiscordJsAdapterGenerics = {
|
||||
commandReplyInit: Discord.CommandInteraction
|
||||
channelInit: Discord.TextBasedChannel
|
||||
}
|
||||
|
||||
export class DiscordJsAdapter implements Adapter<DiscordJsAdapterGenerics> {
|
||||
constructor(private client: Discord.Client) {}
|
||||
|
||||
/**
|
||||
@@ -51,6 +57,19 @@ export class DiscordJsAdapter implements Adapter<Discord.CommandInteraction> {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
// eslint-disable-next-line class-methods-use-this
|
||||
createChannel(channel: Discord.TextBasedChannel): Channel {
|
||||
return {
|
||||
send: async (options) => {
|
||||
const message = await channel.send(getDiscordMessageOptions(options))
|
||||
return createReacordMessage(message)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createReacordComponentInteraction(
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import type { ReactNode } from "react"
|
||||
import { ChannelMessageRenderer } from "../internal/channel-message-renderer"
|
||||
import { CommandReplyRenderer } from "../internal/command-reply-renderer.js"
|
||||
import { reconciler } from "../internal/reconciler.js"
|
||||
import { Renderer } from "../internal/renderer.js"
|
||||
import type { Adapter } from "./adapters/adapter"
|
||||
import type { Renderer } from "../internal/renderer"
|
||||
import type { Adapter, AdapterGenerics } from "./adapters/adapter"
|
||||
|
||||
export type ReacordConfig<InteractionInit> = {
|
||||
adapter: Adapter<InteractionInit>
|
||||
export type ReacordConfig<Generics extends AdapterGenerics> = {
|
||||
adapter: Adapter<Generics>
|
||||
|
||||
/**
|
||||
* The max number of active instances.
|
||||
@@ -19,10 +21,10 @@ export type ReacordInstance = {
|
||||
destroy: () => void
|
||||
}
|
||||
|
||||
export class Reacord<InteractionInit> {
|
||||
export class Reacord<Generics extends AdapterGenerics> {
|
||||
private renderers: Renderer[] = []
|
||||
|
||||
constructor(private readonly config: ReacordConfig<InteractionInit>) {
|
||||
constructor(private readonly config: ReacordConfig<Generics>) {
|
||||
config.adapter.addComponentInteractionListener((interaction) => {
|
||||
for (const renderer of this.renderers) {
|
||||
if (renderer.handleComponentInteraction(interaction)) return
|
||||
@@ -34,15 +36,25 @@ export class Reacord<InteractionInit> {
|
||||
return this.config.maxInstances ?? 50
|
||||
}
|
||||
|
||||
createCommandReply(target: InteractionInit): ReacordInstance {
|
||||
send(init: Generics["channelInit"]): ReacordInstance {
|
||||
return this.createInstance(
|
||||
new ChannelMessageRenderer(this.config.adapter.createChannel(init)),
|
||||
)
|
||||
}
|
||||
|
||||
reply(init: Generics["commandReplyInit"]): ReacordInstance {
|
||||
return this.createInstance(
|
||||
new CommandReplyRenderer(
|
||||
this.config.adapter.createCommandInteraction(init),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private createInstance(renderer: Renderer) {
|
||||
if (this.renderers.length > this.maxInstances) {
|
||||
this.deactivate(this.renderers[0]!)
|
||||
}
|
||||
|
||||
const renderer = new Renderer(
|
||||
this.config.adapter.createCommandInteraction(target),
|
||||
)
|
||||
|
||||
this.renderers.push(renderer)
|
||||
|
||||
const container = reconciler.createContainer(renderer, 0, false, {})
|
||||
|
||||
Reference in New Issue
Block a user