clean up the public interface a tiny bit
This commit is contained in:
@@ -7,7 +7,7 @@ import type { ReacordConfig, ReacordInstance } from "./reacord"
|
||||
import { Reacord } from "./reacord"
|
||||
|
||||
export class ReacordDiscordJs extends Reacord {
|
||||
constructor(client: Discord.Client, config: ReacordConfig = {}) {
|
||||
constructor(private client: Discord.Client, config: ReacordConfig = {}) {
|
||||
super(config)
|
||||
|
||||
client.on("interactionCreate", (interaction) => {
|
||||
@@ -19,35 +19,56 @@ export class ReacordDiscordJs extends Reacord {
|
||||
})
|
||||
}
|
||||
|
||||
override send(channel: Discord.TextBasedChannel): ReacordInstance {
|
||||
return this.createChannelRendererInstance({
|
||||
send: async (options) => {
|
||||
const message = await channel.send(getDiscordMessageOptions(options))
|
||||
return createReacordMessage(message)
|
||||
override send(
|
||||
channelId: string,
|
||||
initialContent?: React.ReactNode,
|
||||
): ReacordInstance {
|
||||
return this.createChannelRendererInstance(
|
||||
{
|
||||
send: async (options) => {
|
||||
const channel =
|
||||
this.client.channels.cache.get(channelId) ??
|
||||
(await this.client.channels.fetch(channelId)) ??
|
||||
raise(`Channel ${channelId} not found`)
|
||||
|
||||
if (!channel.isText()) {
|
||||
raise(`Channel ${channelId} is not a text channel`)
|
||||
}
|
||||
|
||||
const message = await channel.send(getDiscordMessageOptions(options))
|
||||
return createReacordMessage(message)
|
||||
},
|
||||
},
|
||||
})
|
||||
initialContent,
|
||||
)
|
||||
}
|
||||
|
||||
override reply(interaction: Discord.CommandInteraction): ReacordInstance {
|
||||
return this.createCommandReplyRendererInstance({
|
||||
type: "command",
|
||||
id: interaction.id,
|
||||
channelId: interaction.channelId,
|
||||
reply: async (options) => {
|
||||
const message = await interaction.reply({
|
||||
...getDiscordMessageOptions(options),
|
||||
fetchReply: true,
|
||||
})
|
||||
return createReacordMessage(message as Discord.Message)
|
||||
override reply(
|
||||
interaction: Discord.CommandInteraction,
|
||||
initialContent?: React.ReactNode,
|
||||
): ReacordInstance {
|
||||
return this.createCommandReplyRendererInstance(
|
||||
{
|
||||
type: "command",
|
||||
id: interaction.id,
|
||||
channelId: interaction.channelId,
|
||||
reply: async (options) => {
|
||||
const message = await interaction.reply({
|
||||
...getDiscordMessageOptions(options),
|
||||
fetchReply: true,
|
||||
})
|
||||
return createReacordMessage(message as Discord.Message)
|
||||
},
|
||||
followUp: async (options) => {
|
||||
const message = await interaction.followUp({
|
||||
...getDiscordMessageOptions(options),
|
||||
fetchReply: true,
|
||||
})
|
||||
return createReacordMessage(message as Discord.Message)
|
||||
},
|
||||
},
|
||||
followUp: async (options) => {
|
||||
const message = await interaction.followUp({
|
||||
...getDiscordMessageOptions(options),
|
||||
fetchReply: true,
|
||||
})
|
||||
return createReacordMessage(message as Discord.Message)
|
||||
},
|
||||
})
|
||||
initialContent,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,9 +32,12 @@ export abstract class Reacord {
|
||||
|
||||
constructor(private readonly config: ReacordConfig = {}) {}
|
||||
|
||||
abstract send(channel: unknown): ReacordInstance
|
||||
abstract send(channel: unknown, initialContent?: ReactNode): ReacordInstance
|
||||
|
||||
abstract reply(commandInteraction: unknown): ReacordInstance
|
||||
abstract reply(
|
||||
commandInteraction: unknown,
|
||||
initialContent?: ReactNode,
|
||||
): ReacordInstance
|
||||
|
||||
protected handleComponentInteraction(interaction: ComponentInteraction) {
|
||||
for (const renderer of this.renderers) {
|
||||
@@ -46,17 +49,27 @@ export abstract class Reacord {
|
||||
return this.config.maxInstances ?? 50
|
||||
}
|
||||
|
||||
protected createChannelRendererInstance(channel: Channel) {
|
||||
return this.createInstance(new ChannelMessageRenderer(channel))
|
||||
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))
|
||||
return this.createInstance(
|
||||
new CommandReplyRenderer(commandInteraction),
|
||||
initialContent,
|
||||
)
|
||||
}
|
||||
|
||||
private createInstance(renderer: Renderer) {
|
||||
private createInstance(renderer: Renderer, initialContent?: ReactNode) {
|
||||
if (this.renderers.length > this.maxInstances) {
|
||||
this.deactivate(this.renderers[0]!)
|
||||
}
|
||||
@@ -65,6 +78,10 @@ export abstract class Reacord {
|
||||
|
||||
const container = reconciler.createContainer(renderer, 0, false, {})
|
||||
|
||||
if (initialContent !== undefined) {
|
||||
reconciler.updateContainer(initialContent, container)
|
||||
}
|
||||
|
||||
return {
|
||||
render: (content: ReactNode) => {
|
||||
reconciler.updateContainer(content, container)
|
||||
|
||||
Reference in New Issue
Block a user