Skip to content

Commit

Permalink
fix: make ephemeral responses work properly
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilloftheshadow committed Oct 6, 2024
1 parent 0802dc2 commit f923485
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-ants-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@buape/carbon": patch
---

fix: make ephemeral responses work properly
35 changes: 35 additions & 0 deletions apps/rocko/src/commands/testing/ephemeral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
Command,
type CommandInteraction,
CommandWithSubcommands
} from "@buape/carbon"

export default class EphemeralCommand extends CommandWithSubcommands {
name = "ephemeral"
description = "Ephemeral test"
ephemeral = true

subcommands = [new EphemeralNoDefer(), new EphemeralDefer()]
}

class EphemeralNoDefer extends Command {
name = "no-defer"
description = "Ephemeral test"
ephemeral = true
defer = false

async run(interaction: CommandInteraction): Promise<void> {
return interaction.reply({ content: "Ephemeral no defer" })
}
}

export class EphemeralDefer extends Command {
name = "defer"
description = "Ephemeral test"
ephemeral = true
defer = true

async run(interaction: CommandInteraction): Promise<void> {
return interaction.reply({ content: "Ephemeral defer" })
}
}
27 changes: 18 additions & 9 deletions packages/carbon/src/abstracts/BaseInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type APIInteraction,
InteractionResponseType,
type InteractionType,
MessageFlags,
type RESTPatchAPIInteractionOriginalResponseJSONBody,
type RESTPostAPIInteractionCallbackJSONBody,
type RESTPostAPIInteractionFollowupJSONBody,
Expand Down Expand Up @@ -154,10 +155,12 @@ export abstract class BaseInteraction<T extends APIInteraction> extends Base {
body: {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
...serializePayload(data),
flags: options.ephemeral
? 64 | ("flags" in serialized ? (serialized.flags ?? 0) : 0)
: undefined
...serialized,
flags:
options.ephemeral || this.defaultEphemeral
? MessageFlags.Ephemeral |
("flags" in serialized ? (serialized.flags ?? 0) : 0)
: undefined
}
} as RESTPostAPIInteractionCallbackJSONBody,
files: options.files
Expand All @@ -171,16 +174,21 @@ export abstract class BaseInteraction<T extends APIInteraction> extends Base {
* If the interaction is already deferred, this will do nothing.
* @internal
*/
async defer() {
async defer({ ephemeral = false } = {}) {
if (this._deferred) return
this._deferred = true
await this.client.rest.post(
Routes.interactionCallback(this.rawData.id, this.rawData.token),
{
body: {
type: InteractionResponseType.DeferredChannelMessageWithSource,
flags: this.defaultEphemeral ? 64 : undefined
} as RESTPostAPIInteractionCallbackJSONBody
data: {
flags:
ephemeral || this.defaultEphemeral
? MessageFlags.Ephemeral
: undefined
}
} satisfies RESTPostAPIInteractionCallbackJSONBody
}
)
}
Expand All @@ -198,7 +206,7 @@ export abstract class BaseInteraction<T extends APIInteraction> extends Base {
body: {
type: InteractionResponseType.Modal,
data: modal.serialize()
} as RESTPostAPIInteractionCallbackJSONBody
} satisfies RESTPostAPIInteractionCallbackJSONBody
}
)
}
Expand All @@ -214,7 +222,8 @@ export abstract class BaseInteraction<T extends APIInteraction> extends Base {
body: {
...serialized,
flags: options.ephemeral
? 64 | ("flags" in serialized ? (serialized.flags ?? 0) : 0)
? MessageFlags.Ephemeral |
("flags" in serialized ? (serialized.flags ?? 0) : 0)
: undefined
} as RESTPostAPIInteractionFollowupJSONBody,
files: options.files
Expand Down
24 changes: 16 additions & 8 deletions packages/carbon/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,20 @@ export function concatUint8Arrays(
return merged
}

export const serializePayload = (payload: MessagePayload) => {
return typeof payload === "string"
? { content: payload }
: {
...payload,
embeds: payload.embeds?.map((embed) => embed.serialize()),
components: payload.components?.map((row) => row.serialize())
}
export const serializePayload = (
payload: MessagePayload,
defaultEphemeral = false
) => {
if (typeof payload === "string") {
return { content: payload, flags: defaultEphemeral ? 64 : undefined }
}
const data = {
...payload,
embeds: payload.embeds?.map((embed) => embed.serialize()),
components: payload.components?.map((row) => row.serialize())
}
if (defaultEphemeral) {
data.flags = 64
}
return data
}

0 comments on commit f923485

Please sign in to comment.