-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(structures): add url getters for image assets #11406
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
Qjuh
wants to merge
3
commits into
discordjs:main
Choose a base branch
from
Qjuh:feat/structures-url-methods
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.
+100
−5
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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,6 +1,7 @@ | ||||||
| import type { APIAvatarDecorationData } from 'discord-api-types/v10'; | ||||||
| import { CDNRoutes, RouteBases, type APIAvatarDecorationData } from 'discord-api-types/v10'; | ||||||
| import { Structure } from '../Structure.js'; | ||||||
| import { kData } from '../utils/symbols.js'; | ||||||
| import { isFieldSet } from '../utils/type-guards.js'; | ||||||
| import type { Partialize } from '../utils/types.js'; | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -37,4 +38,15 @@ export class AvatarDecorationData<Omitted extends keyof APIAvatarDecorationData | |||||
| public get asset() { | ||||||
| return this[kData].asset; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Get the URL to the asset of this avatar decoration | ||||||
| * | ||||||
| * @returns the URL to the asset of this avatar decoration | ||||||
|
Comment on lines
+44
to
+45
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. redundant
Suggested change
|
||||||
| */ | ||||||
| public assetURL() { | ||||||
| return isFieldSet(this[kData], 'asset', 'string') | ||||||
| ? `${RouteBases.cdn}${CDNRoutes.avatarDecoration(this[kData].asset)}` | ||||||
| : null; | ||||||
| } | ||||||
| } | ||||||
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,8 +1,16 @@ | ||
| import { DiscordSnowflake } from '@sapphire/snowflake'; | ||
| import type { APIUser } from 'discord-api-types/v10'; | ||
| import { | ||
| CDNRoutes, | ||
| ImageFormat, | ||
| RouteBases, | ||
| type APIUser, | ||
| type DefaultUserAvatarAssets, | ||
| type UserAvatarFormat, | ||
| type UserBannerFormat, | ||
| } from 'discord-api-types/v10'; | ||
| import { Structure } from '../Structure.js'; | ||
| import { kData } from '../utils/symbols.js'; | ||
| import { isIdSet } from '../utils/type-guards.js'; | ||
| import { isFieldSet, isIdSet } from '../utils/type-guards.js'; | ||
| import type { Partialize } from '../utils/types.js'; | ||
|
|
||
| /** | ||
|
|
@@ -66,6 +74,37 @@ export class User<Omitted extends keyof APIUser | '' = ''> extends Structure<API | |
| return this[kData].avatar; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user avatar | ||
Qjuh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param format - the file format to use | ||
| */ | ||
| public avatarURL(format: UserAvatarFormat = ImageFormat.WebP) { | ||
| return isIdSet(this[kData].id) && isFieldSet(this[kData], 'avatar', 'string') | ||
| ? `${RouteBases.cdn}${CDNRoutes.userAvatar(this[kData].id.toString(), this[kData].avatar, format)}` | ||
| : null; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user's default avatar | ||
| */ | ||
| public get defaultAvatarURL() { | ||
| return isIdSet(this[kData].id) | ||
| ? `${RouteBases.cdn}${CDNRoutes.defaultUserAvatar( | ||
| (isFieldSet(this[kData], 'discriminator', 'string') && this[kData].discriminator.length === 4 | ||
| ? Number(this[kData].discriminator) % 5 | ||
| : Number(BigInt(this[kData].id) >> 22n) % 6) as DefaultUserAvatarAssets, | ||
| )}` | ||
Qjuh marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+93
to
+97
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. Too much logic here IMO. I'd rather split this up a bit |
||
| : null; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user avatar or their default avatar, if none is set | ||
| */ | ||
| public displayAvatarURL(format: UserAvatarFormat = ImageFormat.WebP) { | ||
| return this.avatarURL(format) ?? this.defaultAvatarURL; | ||
| } | ||
|
|
||
| /** | ||
| * Whether the user is a bot | ||
| */ | ||
|
|
@@ -98,6 +137,17 @@ export class User<Omitted extends keyof APIUser | '' = ''> extends Structure<API | |
| return this[kData].banner; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user banner | ||
Qjuh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param format - the file format to use | ||
| */ | ||
| public bannerURL(format: UserBannerFormat = ImageFormat.WebP) { | ||
| return isIdSet(this[kData].id) && isFieldSet(this[kData], 'banner', 'string') | ||
| ? `${RouteBases.cdn}${CDNRoutes.userBanner(this[kData].id.toString(), this[kData].banner, format)}` | ||
| : null; | ||
| } | ||
|
|
||
| /** | ||
| * The base 10 accent color of the user's banner | ||
| * | ||
|
|
||
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.