clean up the public interface a tiny bit

This commit is contained in:
MapleLeaf
2021-12-27 21:16:50 -06:00
parent ef26b66cb8
commit 6515f5e3d7
4 changed files with 88 additions and 59 deletions

View File

@@ -7,7 +7,7 @@ import type { ReacordConfig, ReacordInstance } from "./reacord"
import { Reacord } from "./reacord" import { Reacord } from "./reacord"
export class ReacordDiscordJs extends Reacord { export class ReacordDiscordJs extends Reacord {
constructor(client: Discord.Client, config: ReacordConfig = {}) { constructor(private client: Discord.Client, config: ReacordConfig = {}) {
super(config) super(config)
client.on("interactionCreate", (interaction) => { client.on("interactionCreate", (interaction) => {
@@ -19,17 +19,36 @@ export class ReacordDiscordJs extends Reacord {
}) })
} }
override send(channel: Discord.TextBasedChannel): ReacordInstance { override send(
return this.createChannelRendererInstance({ channelId: string,
initialContent?: React.ReactNode,
): ReacordInstance {
return this.createChannelRendererInstance(
{
send: async (options) => { 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)) const message = await channel.send(getDiscordMessageOptions(options))
return createReacordMessage(message) return createReacordMessage(message)
}, },
}) },
initialContent,
)
} }
override reply(interaction: Discord.CommandInteraction): ReacordInstance { override reply(
return this.createCommandReplyRendererInstance({ interaction: Discord.CommandInteraction,
initialContent?: React.ReactNode,
): ReacordInstance {
return this.createCommandReplyRendererInstance(
{
type: "command", type: "command",
id: interaction.id, id: interaction.id,
channelId: interaction.channelId, channelId: interaction.channelId,
@@ -47,7 +66,9 @@ export class ReacordDiscordJs extends Reacord {
}) })
return createReacordMessage(message as Discord.Message) return createReacordMessage(message as Discord.Message)
}, },
}) },
initialContent,
)
} }
} }

View File

@@ -32,9 +32,12 @@ export abstract class Reacord {
constructor(private readonly config: ReacordConfig = {}) {} 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) { protected handleComponentInteraction(interaction: ComponentInteraction) {
for (const renderer of this.renderers) { for (const renderer of this.renderers) {
@@ -46,17 +49,27 @@ export abstract class Reacord {
return this.config.maxInstances ?? 50 return this.config.maxInstances ?? 50
} }
protected createChannelRendererInstance(channel: Channel) { protected createChannelRendererInstance(
return this.createInstance(new ChannelMessageRenderer(channel)) channel: Channel,
initialContent?: ReactNode,
) {
return this.createInstance(
new ChannelMessageRenderer(channel),
initialContent,
)
} }
protected createCommandReplyRendererInstance( protected createCommandReplyRendererInstance(
commandInteraction: CommandInteraction, commandInteraction: CommandInteraction,
initialContent?: ReactNode,
): ReacordInstance { ): 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) { if (this.renderers.length > this.maxInstances) {
this.deactivate(this.renderers[0]!) this.deactivate(this.renderers[0]!)
} }
@@ -65,6 +78,10 @@ export abstract class Reacord {
const container = reconciler.createContainer(renderer, 0, false, {}) const container = reconciler.createContainer(renderer, 0, false, {})
if (initialContent !== undefined) {
reconciler.updateContainer(initialContent, container)
}
return { return {
render: (content: ReactNode) => { render: (content: ReactNode) => {
reconciler.updateContainer(content, container) reconciler.updateContainer(content, container)

View File

@@ -19,7 +19,6 @@ export function createCommandHandler(client: Client, commands: Command[]) {
) )
} }
} }
console.info("ready 💖")
}) })
client.on("interactionCreate", async (interaction) => { client.on("interactionCreate", async (interaction) => {

View File

@@ -12,7 +12,9 @@ const client = new Client({
const reacord = new ReacordDiscordJs(client) const reacord = new ReacordDiscordJs(client)
// client.on("ready", async () => { client.on("ready", () => {
console.info("ready 💖")
// const now = new Date() // const now = new Date()
// function UptimeCounter() { // function UptimeCounter() {
@@ -30,18 +32,8 @@ const reacord = new ReacordDiscordJs(client)
// ) // )
// } // }
// const channelId = "671787605624487941" // reacord.send("671787605624487941", <UptimeCounter />)
})
// const channel =
// client.channels.cache.get(channelId) ||
// (await client.channels.fetch(channelId))
// if (!channel?.isText()) {
// throw new Error("channel is not text")
// }
// reacord.send(channel).render(<UptimeCounter />)
// })
createCommandHandler(client, [ createCommandHandler(client, [
{ {
@@ -56,7 +48,7 @@ createCommandHandler(client, [
name: "select", name: "select",
description: "shows a select", description: "shows a select",
run: (interaction) => { run: (interaction) => {
reacord.reply(interaction).render(<FruitSelect />) reacord.reply(interaction, <FruitSelect />)
}, },
}, },
]) ])