-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat: add shared client theme support #11454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kshitijanurag
wants to merge
1
commit into
discordjs:main
Choose a base branch
from
kshitijanurag:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+374
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||
| import { Buffer } from 'node:buffer'; | ||||||
| import { AllowedMentionsTypes, ComponentType, MessageFlags, MessageReferenceType } from 'discord-api-types/v10'; | ||||||
| import { AllowedMentionsTypes, BaseThemeType, ComponentType, MessageFlags, MessageReferenceType } from 'discord-api-types/v10'; | ||||||
| import { z } from 'zod'; | ||||||
| import { snowflakePredicate } from '../Assertions.js'; | ||||||
| import { embedPredicate } from './embed/Assertions.js'; | ||||||
|
|
@@ -79,12 +79,20 @@ const basicActionRowPredicate = z.object({ | |||||
| .array(), | ||||||
| }); | ||||||
|
|
||||||
| export const sharedClientThemePredicate = z.object({ | ||||||
| colors: z.array(z.string().regex(/^[\da-f]{6}$/i)).min(1).max(5), | ||||||
| gradient_angle: z.int().min(0).max(360), | ||||||
| base_mix: z.int().min(0).max(100), | ||||||
| base_theme: z.enum(BaseThemeType).nullish(), | ||||||
| }); | ||||||
|
|
||||||
| const messageNoComponentsV2Predicate = baseMessagePredicate | ||||||
| .extend({ | ||||||
| content: z.string().max(2_000).optional(), | ||||||
| embeds: embedPredicate.array().max(10).optional(), | ||||||
| sticker_ids: z.array(z.string()).max(3).optional(), | ||||||
| poll: pollPredicate.optional(), | ||||||
| shared_client_theme: sharedClientThemePredicate.nullish(), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
shared_client_theme is not nullable.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is nullable in the spec |
||||||
| components: basicActionRowPredicate.array().max(5).optional(), | ||||||
| flags: z | ||||||
| .int() | ||||||
|
|
@@ -100,8 +108,9 @@ const messageNoComponentsV2Predicate = baseMessagePredicate | |||||
| data.poll !== undefined || | ||||||
| (data.attachments !== undefined && data.attachments.length > 0) || | ||||||
| (data.components !== undefined && data.components.length > 0) || | ||||||
| (data.sticker_ids !== undefined && data.sticker_ids.length > 0), | ||||||
| { error: 'Messages must have content, embeds, a poll, attachments, components or stickers' }, | ||||||
| (data.sticker_ids !== undefined && data.sticker_ids.length > 0) || | ||||||
| data.shared_client_theme !== undefined, | ||||||
| { error: 'Messages must have content, embeds, a poll, attachments, components, stickers, or a shared client theme' }, | ||||||
| ); | ||||||
|
|
||||||
| const allTopLevelComponentsPredicate = z | ||||||
|
|
@@ -134,6 +143,7 @@ const messageComponentsV2Predicate = baseMessagePredicate.extend({ | |||||
| embeds: z.array(z.never()).nullish(), | ||||||
| sticker_ids: z.array(z.never()).nullish(), | ||||||
| poll: z.null().optional(), | ||||||
| shared_client_theme: z.null().optional(), | ||||||
| }); | ||||||
|
|
||||||
| export const messagePredicate = z.union([messageNoComponentsV2Predicate, messageComponentsV2Predicate]); | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import type { JSONEncodable } from '@discordjs/util'; | ||
| import type { APIMessageSharedClientTheme, BaseThemeType } from 'discord-api-types/v10'; | ||
| import { validate } from '../util/validation.js'; | ||
| import { sharedClientThemePredicate } from './Assertions.js'; | ||
|
|
||
| /** | ||
| * A builder that creates API-compatible JSON data for shared client themes. | ||
| * | ||
| * @see {@link https://discord.com/developers/docs/resources/message#shared-client-theme-object} | ||
| */ | ||
| export class SharedClientThemeBuilder implements JSONEncodable<APIMessageSharedClientTheme> { | ||
| /** | ||
| * The API data associated with this shared client theme. | ||
| */ | ||
| private readonly data: Partial<APIMessageSharedClientTheme>; | ||
|
|
||
| /** | ||
| * Creates a new shared client theme builder. | ||
| * | ||
| * @param data - The API data to create this shared client theme with | ||
| */ | ||
| public constructor(data: Partial<APIMessageSharedClientTheme> = {}) { | ||
| this.data = structuredClone(data); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the colors of this theme. | ||
| * | ||
| * @remarks | ||
| * A maximum of 5 hexadecimal-encoded colors may be provided. | ||
| * @param colors - The hexadecimal-encoded colors to set (e.g. `'5865F2'`) | ||
| */ | ||
| public setColors(colors: readonly string[]): this { | ||
| this.data.colors = [...colors]; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the gradient angle of this theme. | ||
| * | ||
| * @remarks | ||
| * The value must be between `0` and `360` (inclusive). | ||
| * @param angle - The gradient angle (direction of theme colors) | ||
| */ | ||
| public setGradientAngle(angle: number): this { | ||
| this.data.gradient_angle = angle; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the base mix (intensity) of this theme. | ||
| * | ||
| * @remarks | ||
| * The value must be between `0` and `100` (inclusive). | ||
| * @param baseMix - The base mix intensity | ||
| */ | ||
| public setBaseMix(baseMix: number): this { | ||
| this.data.base_mix = baseMix; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the base theme (mode) of this theme. | ||
| * | ||
| * @param baseTheme - The base theme mode, or `null` to clear | ||
| */ | ||
| public setBaseTheme(baseTheme: BaseThemeType | null): this { | ||
| this.data.base_theme = baseTheme; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Clears the base theme of this theme. | ||
| */ | ||
| public clearBaseTheme(): this { | ||
| this.data.base_theme = undefined; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes this builder to API-compatible JSON data. | ||
| * | ||
| * Note that by disabling validation, there is no guarantee that the resulting object will be valid. | ||
| * | ||
| * @param validationOverride - Force validation to run/not run regardless of your global preference | ||
| */ | ||
| public toJSON(validationOverride?: boolean): APIMessageSharedClientTheme { | ||
| const data = structuredClone(this.data); | ||
| validate(sharedClientThemePredicate, data, validationOverride); | ||
| return data as APIMessageSharedClientTheme; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.