Skip to content

Commit

Permalink
Merge branch 'command-permissions' of https://github.com/mezotv/carbon
Browse files Browse the repository at this point in the history
…into command-permissions
  • Loading branch information
mezotv committed Oct 8, 2024
2 parents a5d8606 + 8a2db06 commit 7c4a3e4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 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
5 changes: 5 additions & 0 deletions .changeset/short-pumpkins-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@buape/carbon": minor
---

feat: add icon_url property to embed author object
2 changes: 1 addition & 1 deletion .github/workflows/renovate-changeset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
if: ${{ github.actor == 'renovate[bot]' }}
steps:
- uses: mscharley/dependency-changesets-action@v1.0.11
- uses: mscharley/dependency-changesets-action@v1.1.0
with:
token: ${{ secrets.BUAPEBOT_SECRET }}
changeset-folder: ".changeset"
Expand Down
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
1 change: 1 addition & 0 deletions packages/carbon/src/classes/Embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class Embed {
author?: {
name: string
url?: string
icon_url?: string
}

fields?: {
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 7c4a3e4

Please sign in to comment.