Let trusted apps add an embed and a link button to Discord direct messages
All checks were successful
Deploy Palico Bot / deploy (push) Successful in 7s
All checks were successful
Deploy Palico Bot / deploy (push) Successful in 7s
- POST /api/notifications/dm accepts optional embed (title, description, url, color, thumbnail, fields, footer, timestamp) and link fields; plain text stays required for phone notifications. - Every value is checked against Discord's limits, and urls and thumbnails must use https; a refused embed answers 400 INVALID_EMBED. - Sokko TCG uses it to show the destination, the result and the loot of its expedition alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2471b5f6f7
commit
6ca17264e0
4 changed files with 131 additions and 5 deletions
|
|
@ -44,3 +44,50 @@ test("sendDirectMessage maps unknown users and closed messages to client errors"
|
|||
await assert.rejects(sendDirectMessage(fakeClient({ sendError: { code: 50007 } }), { userId: USER_ID, content: "x" }), (error) => error.status === 422 && error.code === "DM_CLOSED");
|
||||
await assert.rejects(sendDirectMessage(fakeClient({ sendError: new Error("network") }), { userId: USER_ID, content: "x" }), /network/);
|
||||
});
|
||||
|
||||
test("sendDirectMessage adds an embed and a link button when given", async () => {
|
||||
const client = fakeClient();
|
||||
await sendDirectMessage(client, {
|
||||
userId: USER_ID,
|
||||
content: "Votre expédition est de retour !",
|
||||
embed: {
|
||||
title: "Votre expédition est de retour !",
|
||||
description: "Les Glaces de la Longue Nuit · ★★★ · 6 h",
|
||||
url: "https://sokko.example/#/cards/expeditions/abc",
|
||||
color: 0xf6c453,
|
||||
thumbnail: "https://sokko.example/static/img/cards/x.webp",
|
||||
fields: [{ name: "Butin", value: "+16 Éclats" }],
|
||||
footer: "Sokko TCG",
|
||||
timestamp: 1_800_000_000_000,
|
||||
},
|
||||
link: { label: "Voir l'expédition", url: "https://sokko.example/#/cards/expeditions/abc" },
|
||||
});
|
||||
const [message] = client.sent;
|
||||
assert.equal(message.embeds[0].title, "Votre expédition est de retour !");
|
||||
assert.deepEqual(message.embeds[0].thumbnail, { url: "https://sokko.example/static/img/cards/x.webp" });
|
||||
assert.deepEqual(message.embeds[0].fields, [{ name: "Butin", value: "+16 Éclats", inline: false }]);
|
||||
assert.deepEqual(message.embeds[0].footer, { text: "Sokko TCG" });
|
||||
assert.equal(message.embeds[0].timestamp, new Date(1_800_000_000_000).toISOString());
|
||||
assert.deepEqual(message.components, [{ type: 1, components: [{ type: 2, style: 5, label: "Voir l'expédition", url: "https://sokko.example/#/cards/expeditions/abc" }] }]);
|
||||
assert.deepEqual(message.allowedMentions, { parse: [] });
|
||||
});
|
||||
|
||||
test("sendDirectMessage refuses embeds and links that Discord would reject", async () => {
|
||||
const client = fakeClient();
|
||||
const send = (extra) => sendDirectMessage(client, { userId: USER_ID, content: "x", ...extra });
|
||||
for (const extra of [
|
||||
{ embed: { description: "sans titre" } },
|
||||
{ embed: { title: "x".repeat(257) } },
|
||||
{ embed: { title: "t", url: "http://sokko.example" } },
|
||||
{ embed: { title: "t", thumbnail: "javascript:alert(1)" } },
|
||||
{ embed: { title: "t", color: 0x1000000 } },
|
||||
{ embed: { title: "t", fields: Array.from({ length: 26 }, () => ({ name: "n", value: "v" })) } },
|
||||
{ embed: { title: "t", fields: [{ name: "n", value: "" }] } },
|
||||
{ embed: { title: "t", description: "x".repeat(4096), fields: Array.from({ length: 2 }, () => ({ name: "n", value: "x".repeat(1024) })) } },
|
||||
{ link: { label: "Voir", url: "ftp://sokko.example" } },
|
||||
{ link: { url: "https://sokko.example" } },
|
||||
]) {
|
||||
await assert.rejects(send(extra), (error) => error instanceof DirectMessageError && error.status === 400 && error.code === "INVALID_EMBED");
|
||||
}
|
||||
assert.equal(client.sent.length, 0);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue