remove initial content for create methods

This commit is contained in:
Domin-MND
2023-10-24 19:58:48 +03:00
parent def0c46f13
commit 390da4cab6
10 changed files with 42 additions and 67 deletions

View File

@@ -7,7 +7,7 @@ import type { ReactNode } from "react"
*/ */
export interface ReacordInstance { export interface ReacordInstance {
/** Render some JSX to this instance (edits the message) */ /** Render some JSX to this instance (edits the message) */
render: (content: ReactNode) => void render: (content: ReactNode) => ReacordInstance
/** Remove this message */ /** Remove this message */
destroy: () => void destroy: () => void

View File

@@ -88,17 +88,14 @@ export class ReacordDiscordJs extends Reacord {
* *
* @param target - Discord channel object. * @param target - Discord channel object.
* @param [options] - Options for the channel message * @param [options] - Options for the channel message
* @param [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages * @see https://reacord.mapleleaf.dev/guides/sending-messages
*/ */
public createChannelMessage( public createChannelMessage(
target: Discord.Channel, target: Discord.Channel,
options: CreateChannelMessageOptions = {}, options: CreateChannelMessageOptions = {},
content?: React.ReactNode,
): ReacordInstance { ): ReacordInstance {
return this.createInstance( return this.createInstance(
this.createChannelMessageRenderer(target, options), this.createChannelMessageRenderer(target, options),
content,
) )
} }
@@ -107,17 +104,14 @@ export class ReacordDiscordJs extends Reacord {
* *
* @param message - Discord message event object. * @param message - Discord message event object.
* @param [options] - Options for the message reply method. * @param [options] - Options for the message reply method.
* @param [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages * @see https://reacord.mapleleaf.dev/guides/sending-messages
*/ */
public createMessageReply( public createMessageReply(
message: Discord.Message, message: Discord.Message,
options: CreateMessageReplyOptions = {}, options: CreateMessageReplyOptions = {},
content?: React.ReactNode,
): ReacordInstance { ): ReacordInstance {
return this.createInstance( return this.createInstance(
this.createMessageReplyRenderer(message, options), this.createMessageReplyRenderer(message, options),
content,
) )
} }
@@ -126,17 +120,14 @@ export class ReacordDiscordJs extends Reacord {
* *
* @param interaction - Discord command interaction object. * @param interaction - Discord command interaction object.
* @param [options] - Custom options for the interaction reply method. * @param [options] - Custom options for the interaction reply method.
* @param [content] - Initial React node content to render.
* @see https://reacord.mapleleaf.dev/guides/sending-messages * @see https://reacord.mapleleaf.dev/guides/sending-messages
*/ */
public createInteractionReply( public createInteractionReply(
interaction: Discord.CommandInteraction, interaction: Discord.CommandInteraction,
options: CreateInteractionReplyOptions = {}, options: CreateInteractionReplyOptions = {},
content?: React.ReactNode,
): ReacordInstance { ): ReacordInstance {
return this.createInstance( return this.createInstance(
this.createInteractionReplyRenderer(interaction, options), this.createInteractionReplyRenderer(interaction, options),
content,
) )
} }

View File

@@ -57,6 +57,7 @@ export abstract class Reacord {
<InstanceProvider value={instance}>{content}</InstanceProvider>, <InstanceProvider value={instance}>{content}</InstanceProvider>,
container, container,
) )
return instance
}, },
deactivate: () => { deactivate: () => {
this.deactivate(renderer) this.deactivate(renderer)

View File

@@ -50,7 +50,7 @@ const createTest = async (
} }
await createTest("basic", (channel) => { await createTest("basic", (channel) => {
reacord.createChannelMessage(channel, {}, "Hello, world!") reacord.createChannelMessage(channel).render("Hello, world!")
}) })
await createTest("counter", (channel) => { await createTest("counter", (channel) => {
@@ -73,7 +73,7 @@ await createTest("counter", (channel) => {
</> </>
) )
} }
reacord.createChannelMessage(channel, {}, <Counter />) reacord.createChannelMessage(channel).render(<Counter />)
}) })
await createTest("select", (channel) => { await createTest("select", (channel) => {
@@ -102,9 +102,7 @@ await createTest("select", (channel) => {
) )
} }
const instance = reacord.createChannelMessage( const instance = reacord.createChannelMessage(channel).render(
channel,
{},
<FruitSelect <FruitSelect
onConfirm={(value) => { onConfirm={(value) => {
instance.render(`you chose ${value}`) instance.render(`you chose ${value}`)
@@ -115,9 +113,7 @@ await createTest("select", (channel) => {
}) })
await createTest("ephemeral button", (channel) => { await createTest("ephemeral button", (channel) => {
reacord.createChannelMessage( reacord.createChannelMessage(channel).render(
channel,
{},
<> <>
<Button <Button
label="public clic" label="public clic"
@@ -138,13 +134,11 @@ await createTest("delete this", (channel) => {
const instance = useInstance() const instance = useInstance()
return <Button label="delete this" onClick={() => instance.destroy()} /> return <Button label="delete this" onClick={() => instance.destroy()} />
} }
reacord.createChannelMessage(channel, {}, <DeleteThis />) reacord.createChannelMessage(channel).render(<DeleteThis />)
}) })
await createTest("link", (channel) => { await createTest("link", (channel) => {
reacord.createChannelMessage( reacord
channel, .createChannelMessage(channel)
{}, .render(<Link label="hi" url="https://mapleleaf.dev" />)
<Link label="hi" url="https://mapleleaf.dev" />,
)
}) })

View File

@@ -6,8 +6,9 @@ import { test } from "vitest"
test("rendering behavior", async () => { test("rendering behavior", async () => {
const tester = new ReacordTester() const tester = new ReacordTester()
const reply = tester.createInteractionReply() const reply = tester
reply.render(<KitchenSinkCounter onDeactivate={() => reply.deactivate()} />) .createInteractionReply()
.render(<KitchenSinkCounter onDeactivate={() => reply.deactivate()} />)
await tester.assertMessages([ await tester.assertMessages([
{ {
@@ -244,8 +245,7 @@ test("rendering behavior", async () => {
test("delete", async () => { test("delete", async () => {
const tester = new ReacordTester() const tester = new ReacordTester()
const reply = tester.createInteractionReply() const reply = tester.createInteractionReply().render(
reply.render(
<> <>
some text some text
<Embed>some embed</Embed> <Embed>some embed</Embed>

View File

@@ -53,9 +53,7 @@ test("single select", async () => {
]) ])
} }
const reply = tester.createInteractionReply() tester.createInteractionReply().render(<TestSelect />)
reply.render(<TestSelect />)
await assertSelect([]) await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0) expect(onSelect).toHaveBeenCalledTimes(0)
@@ -119,9 +117,7 @@ test("multiple select", async () => {
]) ])
} }
const reply = tester.createInteractionReply() tester.createInteractionReply().render(<TestSelect />)
reply.render(<TestSelect />)
await assertSelect([]) await assertSelect([])
expect(onSelect).toHaveBeenCalledTimes(0) expect(onSelect).toHaveBeenCalledTimes(0)

View File

@@ -43,29 +43,23 @@ export class ReacordTester extends Reacord {
return [...this.messageContainer] return [...this.messageContainer]
} }
public createChannelMessage(initialContent?: ReactNode): ReacordInstance { public createChannelMessage(): ReacordInstance {
return this.createInstance( return this.createInstance(
new ChannelMessageRenderer(new TestChannel(this.messageContainer)), new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
initialContent,
) )
} }
public createMessageReply(initialContent?: ReactNode): ReacordInstance { public createMessageReply(): ReacordInstance {
return this.createInstance( return this.createInstance(
new ChannelMessageRenderer(new TestChannel(this.messageContainer)), new ChannelMessageRenderer(new TestChannel(this.messageContainer)),
initialContent,
) )
} }
public createInteractionReply( public createInteractionReply(_options?: ReplyInfo): ReacordInstance {
initialContent?: ReactNode,
_options?: ReplyInfo,
): ReacordInstance {
return this.createInstance( return this.createInstance(
new InteractionReplyRenderer( new InteractionReplyRenderer(
new TestCommandInteraction(this.messageContainer), new TestCommandInteraction(this.messageContainer),
), ),
initialContent,
) )
} }
@@ -261,11 +255,13 @@ class TestComponentEvent {
guild: GuildInfo = {} as GuildInfo // todo guild: GuildInfo = {} as GuildInfo // todo
reply(content?: ReactNode): ReacordInstance { reply(content?: ReactNode): ReacordInstance {
return this.tester.createInteractionReply(content) return this.tester.createInteractionReply().render(content)
} }
ephemeralReply(content?: ReactNode): ReacordInstance { ephemeralReply(content?: ReactNode): ReacordInstance {
return this.tester.createInteractionReply(content, { ephemeral: true }) return this.tester
.createInteractionReply({ ephemeral: true })
.render(content)
} }
} }

View File

@@ -49,9 +49,9 @@ describe("useInstance", () => {
} }
const tester = new ReacordTester() const tester = new ReacordTester()
const instance = tester.createChannelMessage( const instance = tester
<TestComponent name="parent" />, .createChannelMessage()
) .render(<TestComponent name="parent" />)
await tester.assertMessages([messageOutput("parent")]) await tester.assertMessages([messageOutput("parent")])
expect(instanceFromHook).toBe(instance) expect(instanceFromHook).toBe(instance)

View File

@@ -11,7 +11,8 @@ You can send messages via Reacord to a channel like so.
```jsx ```jsx
client.on("ready", () => { client.on("ready", () => {
const channel = await client.channels.fetch("abc123deadbeef") const channel = await client.channels.fetch("abc123deadbeef")
reacord.createChannelMessage(channel, {}, "Hello, world!") const instance = reacord.createChannelMessage(channel)
instance.render("Hello, world!")
}) })
``` ```
@@ -35,7 +36,7 @@ function Uptime() {
} }
client.on("ready", () => { client.on("ready", () => {
reacord.createChannelMessage(channel, {}, <Uptime />) reacord.createChannelMessage(channel).render(<Uptime />)
}) })
``` ```
@@ -59,11 +60,9 @@ Instead of sending messages to a channel, you may want to reply to a specific me
const Hello = ({ username }) => <>Hello, {username}!</> const Hello = ({ username }) => <>Hello, {username}!</>
client.on("messageCreate", (message) => { client.on("messageCreate", (message) => {
reacord.createMessageReply( reacord
message, .createMessageReply(message)
{}, .render(<Hello username={message.author.displayName} />)
<Hello username={message.author.displayName} />,
)
}) })
``` ```
@@ -110,7 +109,7 @@ client.on("ready", () => {
client.on("interactionCreate", (interaction) => { client.on("interactionCreate", (interaction) => {
if (interaction.isCommand() && interaction.commandName === "ping") { if (interaction.isCommand() && interaction.commandName === "ping") {
// Use the createInteractionReply() function instead of createChannelMessage // Use the createInteractionReply() function instead of createChannelMessage
reacord.createInteractionReply(interaction, {}, <>pong!</>) reacord.createInteractionReply(interaction).render(<>pong!</>)
} }
}) })
@@ -149,14 +148,14 @@ handleCommands(client, [
name: "ping", name: "ping",
description: "pong!", description: "pong!",
run: (interaction) => { run: (interaction) => {
reacord.createInteractionReply(interaction, {}, <>pong!</>) reacord.createInteractionReply(interaction).render(<>pong!</>)
}, },
}, },
{ {
name: "hi", name: "hi",
description: "say hi", description: "say hi",
run: (interaction) => { run: (interaction) => {
reacord.createInteractionReply(interaction, {}, <>hi</>) reacord.createInteractionReply(interaction).render(<>hi</>)
}, },
}, },
]) ])
@@ -176,11 +175,9 @@ handleCommands(client, [
name: "pong", name: "pong",
description: "pong, but in secret", description: "pong, but in secret",
run: (interaction) => { run: (interaction) => {
reacord.createInteractionReply( reacord
interaction, .createInteractionReply(interaction, { ephemeral: true })
{ ephemeral: true }, .render(<>(pong)</>)
<>(pong)</>,
)
}, },
}, },
]) ])
@@ -196,7 +193,9 @@ handleCommands(client, [
name: "pong", name: "pong",
description: "pong, but converted into audio", description: "pong, but converted into audio",
run: (interaction) => { run: (interaction) => {
reacord.createInteractionReply(interaction, { tts: true }, <>pong!</>) reacord
.createInteractionReply(interaction, { tts: true })
.render(<>pong!</>)
}, },
}, },
]) ])

View File

@@ -56,9 +56,7 @@ function FancyMessage({ children }) {
``` ```
```jsx ```jsx
reacord.createChannelMessage( reacord.createChannelMessage(channel).render(
channel,
{},
<FancyMessage> <FancyMessage>
<FancyDetails title="Hello" description="World" /> <FancyDetails title="Hello" description="World" />
</FancyMessage>, </FancyMessage>,