feat(structures): add url getters for image assets#11406
feat(structures): add url getters for image assets#11406Qjuh wants to merge 2 commits intodiscordjs:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #11406 +/- ##
==========================================
- Coverage 32.07% 32.04% -0.04%
==========================================
Files 375 375
Lines 13711 13726 +15
Branches 1077 1084 +7
==========================================
Hits 4398 4398
- Misses 9179 9194 +15
Partials 134 134
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
📝 WalkthroughWalkthroughThis PR adds public methods across multiple Discord structure classes to generate CDN URLs for various assets. Emoji, Sticker, AvatarDecorationData, and User classes receive new URL accessor methods that return asset CDN URLs or null when assets are unavailable. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@packages/structures/src/stickers/Sticker.ts`:
- Around line 74-80: The Sticker.url method currently defaults the format
parameter to ImageFormat.PNG which causes Lottie/GIF stickers to generate
incorrect URLs; change url(format?: StickerFormat) so the parameter is optional
and, inside the method, if format is undefined resolve it using the sticker's
formatType via the extension map (e.g.,
StickerFormatExtensionMap[this.formatType]) before building the CDN URL (keep
the isIdSet(this[kData].id) guard and CDNRoutes.sticker call). Ensure you
reference the Sticker.url method, the format parameter, this.formatType (or the
equivalent stored on this[kData]), StickerFormatExtensionMap, and
CDNRoutes.sticker when making the change.
In `@packages/structures/src/users/User.ts`:
- Around line 91-97: The defaultAvatarURL getter currently converts a snowflake
to Number before modulo, causing precision loss; update the id-based branch to
perform the shift and modulo using BigInt and only cast the small remainder to
Number. In the defaultAvatarURL getter (referencing this[kData], isIdSet,
isFieldSet, and CDNRoutes.defaultUserAvatar), change the id path to compute
(BigInt(this[kData].id) >> 22n) % 6n and then Number(...) that result so the
remainder is accurate, leaving the discriminator branch unchanged.
📜 Review details
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (4)
packages/structures/src/emojis/Emoji.tspackages/structures/src/stickers/Sticker.tspackages/structures/src/users/AvatarDecorationData.tspackages/structures/src/users/User.ts
🧰 Additional context used
🧬 Code graph analysis (3)
packages/structures/src/stickers/Sticker.ts (2)
packages/structures/src/utils/type-guards.ts (1)
isIdSet(1-3)packages/structures/src/utils/symbols.ts (1)
kData(1-1)
packages/structures/src/users/AvatarDecorationData.ts (2)
packages/structures/src/utils/type-guards.ts (1)
isFieldSet(25-32)packages/structures/src/utils/symbols.ts (1)
kData(1-1)
packages/structures/src/users/User.ts (2)
packages/structures/src/utils/type-guards.ts (2)
isIdSet(1-3)isFieldSet(25-32)packages/structures/src/utils/symbols.ts (1)
kData(1-1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: Tests
🔇 Additional comments (3)
packages/structures/src/emojis/Emoji.ts (1)
89-96: Looks good: emoji URL helper is safe and consistent.The ID guard and CDN route composition align well with the rest of the structures helpers.
packages/structures/src/users/AvatarDecorationData.ts (1)
42-50: Asset URL helper looks good.The field guard + CDN route composition is clean and safe.
packages/structures/src/users/User.ts (1)
82-86: Avatar/banner URL helpers look good.The guard patterns and fallback to
defaultAvatarURLare clean and consistent.Also applies to: 104-106, 145-148
✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.
| import { isFieldSet, isIdSet } from '../utils/type-guards.js'; | ||
| import type { Partialize } from '../utils/types.js'; | ||
|
|
||
| const StickerFormatExtensionMap = { |
There was a problem hiding this comment.
This should be exported to the consumer.
Maybe this should be moved to discord-api-types?
| * | ||
| * @returns the URL to the asset of this avatar decoration |
There was a problem hiding this comment.
redundant
| * | |
| * @returns the URL to the asset of this avatar decoration |
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user avatar |
There was a problem hiding this comment.
| * Get the URL to the user avatar | |
| * Get the URL to the user's avatar |
| */ | ||
| 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)}` |
There was a problem hiding this comment.
Not particularly fond of this cast... isEnumValue(this[kData].format_type, StickerFormatType) time? 😅
| ? `${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, | ||
| )}` |
There was a problem hiding this comment.
Too much logic here IMO. I'd rather split this up a bit
| } | ||
|
|
||
| /** | ||
| * Get the URL to the user banner |
There was a problem hiding this comment.
| * Get the URL to the user banner | |
| * Get the URL to the user's banner |
Adds
urlgetters or*URL(format)methods for getting the URLs to image assets associated with image hashs or ids of structures.