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"
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,17 +19,36 @@ export class ReacordDiscordJs extends Reacord {
})
}
override send(channel: Discord.TextBasedChannel): ReacordInstance {
return this.createChannelRendererInstance({
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({
override reply(
interaction: Discord.CommandInteraction,
initialContent?: React.ReactNode,
): ReacordInstance {
return this.createCommandReplyRendererInstance(
{
type: "command",
id: interaction.id,
channelId: interaction.channelId,
@@ -47,7 +66,9 @@ export class ReacordDiscordJs extends Reacord {
})
return createReacordMessage(message as Discord.Message)
},
})
},
initialContent,
)
}
}

View File

@@ -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)

View File

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

View File

@@ -12,7 +12,9 @@ const client = new Client({
const reacord = new ReacordDiscordJs(client)
// client.on("ready", async () => {
client.on("ready", () => {
console.info("ready 💖")
// const now = new Date()
// function UptimeCounter() {
@@ -30,18 +32,8 @@ const reacord = new ReacordDiscordJs(client)
// )
// }
// const channelId = "671787605624487941"
// 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 />)
// })
// reacord.send("671787605624487941", <UptimeCounter />)
})
createCommandHandler(client, [
{
@@ -56,7 +48,7 @@ createCommandHandler(client, [
name: "select",
description: "shows a select",
run: (interaction) => {
reacord.reply(interaction).render(<FruitSelect />)
reacord.reply(interaction, <FruitSelect />)
},
},
])