From 5e9f3cd532e325f869b26d3a8e74c011b1415b40 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 11 Sep 2026 12:51:29 +1200 Subject: [PATCH] Add retries back to PluralKit API --- src/d2m/actions/register-pk-user.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/d2m/actions/register-pk-user.js b/src/d2m/actions/register-pk-user.js index e68d2cc..bb8335b 100644 --- a/src/d2m/actions/register-pk-user.js +++ b/src/d2m/actions/register-pk-user.js @@ -3,6 +3,7 @@ const assert = require("assert").strict const {reg} = require("../../matrix/read-registration") const Ty = require("../../types") +const {scheduler} = require("timers/promises") const passthrough = require("../../passthrough") const {sync, db, select, from} = passthrough @@ -14,14 +15,21 @@ const file = sync.require("../../matrix/file") const registerUser = sync.require("./register-user") /** @returns {Promise} */ -async function fetchMessage(messageID) { +async function fetchMessage(messageID, attempt = 1) { try { var res = await fetch(`https://api.pluralkit.me/v2/messages/${messageID}`) } catch (networkError) { // Network issue, raise a more readable message throw new Error(`Failed to connect to PK API: ${networkError.toString()}`) } - if (!res.ok) throw new Error(`PK API returned an error: ${await res.text()}`) + if (!res.ok) { + if (attempt < 2) { + await scheduler.wait(5000) + return fetchMessage(messageID, attempt + 1) + } else { + throw new Error(`PK API returned an error: ${await res.text()}`) + } + } /** @type {any} */ const root = await res.json() if (!root.member) throw new Error(`PK API didn't return member data: ${JSON.stringify(root)}`)