- Each queued alert keeps an embed and a link button: the destination illustration as thumbnail, the tier, score and loot for a return, the difficulty and automatic departure for a new group. - The button and the message link open #/cards/expeditions/<id>, which scrolls to the player's expedition or group and highlights it; Discord login now returns to that link. - Without an https CARDS_PUBLIC_ORIGIN the link stays in the text, without button or thumbnail, which Discord would refuse. - Delivery docs name palico-bot's NOTIFICATIONS_API_KEY and its local-only route on port 3101. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
38 lines
1.8 KiB
JavaScript
38 lines
1.8 KiB
JavaScript
// Rôle : envoie les alertes Discord des Expéditions par l'API de messages privés de palico-bot, depuis la file tenue par le service.
|
|
const TIMEOUT_MS = 10_000;
|
|
|
|
// Réponses définitives de palico-bot : corps refusé, compte Discord inconnu, messages privés impossibles.
|
|
const PERMANENT_STATUSES = new Set([400, 404, 422]);
|
|
|
|
// `url` : adresse complète de la route palico-bot (…/api/notifications/dm) ; `key` : sa clé d'API (WEB_API_KEY).
|
|
export function createDiscordAlertSender({ url, key, fetchImpl = fetch, timeoutMs = TIMEOUT_MS }) {
|
|
// `extra` : { embed?, link? } que palico-bot ajoute au texte (embed de la destination, bouton vers l'expédition).
|
|
return async (discordId, content, extra = {}) => {
|
|
const response = await fetchImpl(url, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}` },
|
|
body: JSON.stringify({ userId: discordId, content, ...(extra.embed ? { embed: extra.embed } : {}), ...(extra.link ? { link: extra.link } : {}) }),
|
|
signal: AbortSignal.timeout(timeoutMs)
|
|
});
|
|
if (response.ok) return;
|
|
const error = new Error(`palico-bot answered ${response.status}`);
|
|
error.permanent = PERMANENT_STATUSES.has(response.status);
|
|
throw error;
|
|
};
|
|
}
|
|
|
|
// Un passage : annonce les expéditions rentrées, puis envoie les messages en attente, un par un.
|
|
export async function dispatchAlerts(expeditions, send, { limit = 20 } = {}) {
|
|
expeditions.queueReturnAlerts();
|
|
let sent = 0;
|
|
for (const alert of expeditions.pendingAlerts(limit)) {
|
|
try {
|
|
await send(alert.discordId, alert.content, alert.extra);
|
|
expeditions.markAlertSent(alert.id);
|
|
sent += 1;
|
|
} catch (error) {
|
|
expeditions.markAlertFailed(alert.id, error.message, { permanent: Boolean(error.permanent) });
|
|
}
|
|
}
|
|
return sent;
|
|
}
|