diff --git a/@types/globals.d.ts b/@types/globals.d.ts index 601988615..43859c597 100644 --- a/@types/globals.d.ts +++ b/@types/globals.d.ts @@ -36,5 +36,6 @@ declare var sb: { Config: core.Config, Got: core.Got, + Cache: core.Cache, // TODO add others } diff --git a/crons/fetch-twitch-subscriber-list/index.mjs b/crons/fetch-twitch-subscriber-list/index.mts similarity index 76% rename from crons/fetch-twitch-subscriber-list/index.mjs rename to crons/fetch-twitch-subscriber-list/index.mts index 78f202a19..8e3018ce7 100644 --- a/crons/fetch-twitch-subscriber-list/index.mjs +++ b/crons/fetch-twitch-subscriber-list/index.mts @@ -1,15 +1,33 @@ +import { Error as SupiError } from "supi-core"; import sharedKeys from "../../utils/shared-cache-keys.json" with { type: "json" }; +import { sb } from "../../@types/globals.js"; + const { TWITCH_ADMIN_SUBSCRIBER_LIST } = sharedKeys; let tooManySubsWarningSent = false; +type SubscriberData = { + broadcaster_id: string, + broadcaster_login: string, + broadcaster_name: string, + gifter_id: string, // Empty string if not a gifted sub + gifter_login: string, // Empty string if not a gifted sub + gifter_name: string, // Empty string if not a gifted sub + is_gift: boolean, + plan_name: string, + tier: string, + user_id: string, + user_login: string, + user_name: string, +}; + export const definition = { name: "fetch-twitch-subscriber-list", expression: "0 0 0 * * *", description: "Fetches the current subscriber list, then saves it to sb.Cache", code: (async function fetchTwitchSubscriberList () { if (!process.env.TWITCH_READ_SUBSCRIPTIONS_USER_ID) { - throw new sb.Error({ + throw new SupiError({ message: "No Twitch user ID configured for Twitch subscriptions" }); } @@ -17,7 +35,7 @@ export const definition = { const cacheRefreshToken = await sb.Cache.getByPrefix("TWITCH_READ_SUBSCRIPTIONS_REFRESH_TOKEN") const envRefreshToken = process.env.TWITCH_READ_SUBSCRIPTIONS_REFRESH_TOKEN; if (!cacheRefreshToken && !envRefreshToken) { - throw new sb.Error({ + throw new SupiError({ message: "No refresh token configured for Twitch subscriptions" }); } @@ -59,8 +77,7 @@ export const definition = { return; } - /** @type {SubscriberData[]} */ - const data = subsResponse.body.data; + const data: SubscriberData[] = subsResponse.body.data; if (data.length >= 100 && !tooManySubsWarningSent) { console.warn("Maximum subscribers reached for a single Helix call! Update this module to use pagination", { data }); tooManySubsWarningSent = true; @@ -70,20 +87,5 @@ export const definition = { expiry: 864e5 // 1 day }); - /** - * @typedef {Object} SubscriberData - * @property {string} broadcaster_id - * @property {string} broadcaster_login - * @property {string} broadcaster_name - * @property {string} gifter_id Empty string if not a gifted sub - * @property {string} gifter_login Empty string if not a gifted sub - * @property {string} gifter_name Empty string if not a gifted sub - * @property {boolean} is_gift - * @property {string} plan_name - * @property {string} tier - * @property {string} user_id - * @property {string} user_login - * @property {string} user_name - */ }) };