-
-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,23 @@ | ||
| import type { APISticker } from 'discord-api-types/v10'; | ||
| import { | ||
| CDNRoutes, | ||
| ImageFormat, | ||
| RouteBases, | ||
| StickerFormatType, | ||
| type APISticker, | ||
| type StickerFormat, | ||
| } from 'discord-api-types/v10'; | ||
| import { Structure } from '../Structure.js'; | ||
| import { kData } from '../utils/symbols.js'; | ||
| import { isFieldSet, isIdSet } from '../utils/type-guards.js'; | ||
| import type { Partialize } from '../utils/types.js'; | ||
|
|
||
| const StickerFormatExtensionMap = { | ||
| [StickerFormatType.PNG]: ImageFormat.PNG, | ||
| [StickerFormatType.APNG]: ImageFormat.PNG, | ||
| [StickerFormatType.Lottie]: ImageFormat.Lottie, | ||
| [StickerFormatType.GIF]: ImageFormat.GIF, | ||
| } satisfies Record<StickerFormatType, StickerFormat>; | ||
|
|
||
| /** | ||
| * Represents a sticker on Discord. | ||
| * | ||
|
|
@@ -69,4 +84,13 @@ export class Sticker<Omitted extends keyof APISticker | '' = ''> extends Structu | |
| public get type() { | ||
| return this[kData].type; | ||
| } | ||
|
|
||
| /** | ||
| * Get the URL to the sticker | ||
| */ | ||
| public get url() { | ||
| return isIdSet(this[kData].id) && isFieldSet(this[kData], 'format_type', 'number') | ||
| ? `${RouteBases.cdn}${CDNRoutes.sticker(this[kData].id.toString(), StickerFormatExtensionMap[this[kData].format_type] as StickerFormat)}` | ||
|
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. Not particularly fond of this cast... |
||
| : null; | ||
| } | ||
| } | ||
| 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; | ||||||
| } | ||||||
| } | ||||||
| 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 | ||
| * | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be exported to the consumer.
Maybe this should be moved to discord-api-types?