Skip to content

Commit

Permalink
fix ephemeral messages
Browse files Browse the repository at this point in the history
  • Loading branch information
djobbo committed Apr 13, 2024
1 parent a6eeaf1 commit a573e39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/rick-and-morty-api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ const rickCmd = createSlashCommand("rick", "Rick and Morty characters info.")
<App search={search} />
</QueryClientProvider>
),
{ unmountAfter: 300 },
{
unmountAfter: 300,
ephemeral: true,
},
)

client.registerCommand(rickCmd)
Expand Down
1 change: 1 addition & 0 deletions packages/reaccord/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type MessageRenderOptions = {
* @default 300 (5min)
*/
unmountAfter?: number | null
ephemeral?: boolean
}

type ClientOptions = DiscordClientOptions & {
Expand Down
17 changes: 13 additions & 4 deletions packages/reaccord/src/renderer/RootNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class RootNode extends ReaccordNode {

this.ref = ref

const mergedMessageRenderOptions = {
this.messageRenderOptions = {
...(this.discordClient.messageRenderOptions ?? {}),
...messageRenderOptions,
}
Expand All @@ -118,10 +118,10 @@ export class RootNode extends ReaccordNode {
this.unmount()
}

if (!!mergedMessageRenderOptions.unmountAfter) {
if (!!this.messageRenderOptions.unmountAfter) {
timeout = setTimeout(() => {
this.terminateInteraction()
}, mergedMessageRenderOptions.unmountAfter * 1000)
}, this.messageRenderOptions.unmountAfter * 1000)
}

this.reconcilerInstance.updateContainer(
Expand Down Expand Up @@ -154,6 +154,7 @@ export class RootNode extends ReaccordNode {
} else if (this.ref instanceof BaseInteraction) {
reply = await this.ref.reply({
...messageContent,
ephemeral: this.messageRenderOptions?.ephemeral ?? false,
fetchReply: true,
})
} else {
Expand Down Expand Up @@ -181,7 +182,15 @@ export class RootNode extends ReaccordNode {

if (!this.message.editable) throw new Error("Message is not editable")

await this.message.edit(messageContent)
if (this.messageRenderOptions?.ephemeral) {
if (!(this.ref instanceof BaseInteraction))
throw new Error("Can't send ephemeral message to non-interaction")

await this.ref.editReply(messageContent)
} else {
await this.message.edit(messageContent)
}

return this.message
}, MESSAGE_UPDATE_DEBOUNCE_MS)
}

0 comments on commit a573e39

Please # to comment.