Skip to content

Commit 12638cd

Browse files
fix(embed): Allow attachment protocols for thumbnails and images (#10795)
fix(embed): allow attachment protocols for thumbnails and images Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent c78407e commit 12638cd

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

packages/builders/__tests__/messages/embed.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ describe('Embed', () => {
207207
expect(embed.toJSON()).toStrictEqual({ ...base, thumbnail: { url: 'https://discord.js.org/static/logo.svg' } });
208208
});
209209

210+
test('GIVEN an embed using Embed#setThumbnail with attachment protocol THEN returns valid toJSON data', () => {
211+
const embed = new EmbedBuilder();
212+
embed.setThumbnail('attachment://discordjs.webp');
213+
expect(embed.toJSON()).toStrictEqual({ ...base, thumbnail: { url: 'attachment://discordjs.webp' } });
214+
});
215+
210216
test('GIVEN an embed with a pre-defined thumbnail THEN unset thumbnail THEN return valid toJSON data', () => {
211217
const embed = new EmbedBuilder({ thumbnail: { url: 'https://discord.js.org/static/logo.svg' }, ...dummy });
212218
embed.clearThumbnail();
@@ -228,6 +234,12 @@ describe('Embed', () => {
228234
expect(embed.toJSON()).toStrictEqual({ ...base, image: { url: 'https://discord.js.org/static/logo.svg' } });
229235
});
230236

237+
test('GIVEN an embed using Embed#setImage with attachment protocol THEN returns valid toJSON data', () => {
238+
const embed = new EmbedBuilder();
239+
embed.setImage('attachment://discordjs.webp');
240+
expect(embed.toJSON()).toStrictEqual({ ...base, image: { url: 'attachment://discordjs.webp' } });
241+
});
242+
231243
test('GIVEN an embed using Embed#setImage THEN returns valid toJSON data', () => {
232244
const embed = new EmbedBuilder();
233245
embed.setImage('https://discord.js.org/static/logo.svg');

packages/builders/src/messages/embed/Assertions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { embedLength } from '../../util/componentUtil.js';
44

55
const namePredicate = z.string().max(256);
66

7-
const iconURLPredicate = z
7+
const URLPredicate = z
88
.string()
99
.url()
10-
.refine(refineURLPredicate(['http:', 'https:', 'attachment:']), {
11-
message: 'Invalid protocol for icon URL. Must be http:, https:, or attachment:',
12-
});
10+
.refine(refineURLPredicate(['http:', 'https:']), { message: 'Invalid protocol for URL. Must be http: or https:' });
1311

14-
const URLPredicate = z
12+
const URLWithAttachmentProtocolPredicate = z
1513
.string()
1614
.url()
17-
.refine(refineURLPredicate(['http:', 'https:']), { message: 'Invalid protocol for URL. Must be http: or https:' });
15+
.refine(refineURLPredicate(['http:', 'https:', 'attachment:']), {
16+
message: 'Invalid protocol for URL. Must be http:, https:, or attachment:',
17+
});
1818

1919
export const embedFieldPredicate = z.object({
2020
name: namePredicate,
@@ -24,13 +24,13 @@ export const embedFieldPredicate = z.object({
2424

2525
export const embedAuthorPredicate = z.object({
2626
name: namePredicate.min(1),
27-
icon_url: iconURLPredicate.optional(),
27+
icon_url: URLWithAttachmentProtocolPredicate.optional(),
2828
url: URLPredicate.optional(),
2929
});
3030

3131
export const embedFooterPredicate = z.object({
3232
text: z.string().min(1).max(2_048),
33-
icon_url: iconURLPredicate.optional(),
33+
icon_url: URLWithAttachmentProtocolPredicate.optional(),
3434
});
3535

3636
export const embedPredicate = z
@@ -41,8 +41,8 @@ export const embedPredicate = z
4141
timestamp: z.string().optional(),
4242
color: z.number().int().min(0).max(0xffffff).optional(),
4343
footer: embedFooterPredicate.optional(),
44-
image: z.object({ url: URLPredicate }).optional(),
45-
thumbnail: z.object({ url: URLPredicate }).optional(),
44+
image: z.object({ url: URLWithAttachmentProtocolPredicate }).optional(),
45+
thumbnail: z.object({ url: URLWithAttachmentProtocolPredicate }).optional(),
4646
author: embedAuthorPredicate.optional(),
4747
fields: z.array(embedFieldPredicate).max(25).optional(),
4848
})

0 commit comments

Comments
 (0)