From 59ddf55b99f2eb14cba7ada1fc21e0541e4ee84d Mon Sep 17 00:00:00 2001 From: Vendicated Date: Tue, 13 May 2025 22:39:56 +0200 Subject: [PATCH] updater: periodically check for updates while open --- src/Vencord.ts | 71 +++++++++++++++++++------------ src/plugins/_api/badges/index.tsx | 16 ++++++- src/utils/updater.ts | 11 +++-- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/Vencord.ts b/src/Vencord.ts index 63508eb0..48ecce97 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -33,7 +33,7 @@ import { openUpdaterModal } from "@components/VencordSettings/UpdaterTab"; import { StartAt } from "@utils/types"; import { get as dsGet } from "./api/DataStore"; -import { showNotification } from "./api/Notifications"; +import { NotificationData, showNotification } from "./api/Notifications"; import { PlainSettings, Settings } from "./api/Settings"; import { patches, PMLogger, startAllPlugins } from "./plugins"; import { localStorage } from "./utils/localStorage"; @@ -86,6 +86,46 @@ async function syncSettings() { } } +let notifiedForUpdatesThisSession = false; + +async function runUpdateCheck() { + const notify = (data: NotificationData) => { + if (notifiedForUpdatesThisSession) return; + notifiedForUpdatesThisSession = true; + + setTimeout(() => showNotification({ + permanent: true, + noPersist: true, + ...data + }), 10_000); + }; + + try { + const isOutdated = await checkForUpdates(); + if (!isOutdated) return; + + if (Settings.autoUpdate) { + await update(); + if (Settings.autoUpdateNotification) { + notify({ + title: "Vencord has been updated!", + body: "Click here to restart", + onClick: relaunch + }); + } + return; + } + + notify({ + title: "A Vencord update is available!", + body: "Click here to view the update", + onClick: openUpdaterModal! + }); + } catch (err) { + UpdateLogger.error("Failed to check for updates", err); + } +} + async function init() { await onceReady; startAllPlugins(StartAt.WebpackReady); @@ -93,33 +133,8 @@ async function init() { syncSettings(); if (!IS_WEB && !IS_UPDATER_DISABLED) { - try { - const isOutdated = await checkForUpdates(); - if (!isOutdated) return; - - if (Settings.autoUpdate) { - await update(); - if (Settings.autoUpdateNotification) - setTimeout(() => showNotification({ - title: "Vencord has been updated!", - body: "Click here to restart", - permanent: true, - noPersist: true, - onClick: relaunch - }), 10_000); - return; - } - - setTimeout(() => showNotification({ - title: "A Vencord update is available!", - body: "Click here to view the update", - permanent: true, - noPersist: true, - onClick: openUpdaterModal! - }), 10_000); - } catch (err) { - UpdateLogger.error("Failed to check for updates", err); - } + runUpdateCheck(); + setInterval(runUpdateCheck, 1000 * 60 * 30); // 30 minutes } if (IS_DEV) { diff --git a/src/plugins/_api/badges/index.tsx b/src/plugins/_api/badges/index.tsx index 52bede44..a30f41ce 100644 --- a/src/plugins/_api/badges/index.tsx +++ b/src/plugins/_api/badges/index.tsx @@ -46,8 +46,6 @@ const ContributorBadge: ProfileBadge = { let DonorBadges = {} as Record>>; async function loadBadges(noCache = false) { - DonorBadges = {}; - const init = {} as RequestInit; if (noCache) init.cache = "no-cache"; @@ -56,6 +54,8 @@ async function loadBadges(noCache = false) { .then(r => r.json()); } +let intervalId: any; + export default definePlugin({ name: "BadgeAPI", description: "API to add badges to users.", @@ -89,6 +89,11 @@ export default definePlugin({ } ], + // for access from the console or other plugins + get DonorBadges() { + return DonorBadges; + }, + toolboxActions: { async "Refetch Badges"() { await loadBadges(true); @@ -104,6 +109,13 @@ export default definePlugin({ async start() { await loadBadges(); + + clearInterval(intervalId); + intervalId = setInterval(loadBadges, 1000 * 60 * 30); // 30 minutes + }, + + async stop() { + clearInterval(intervalId); }, getBadges(props: { userId: string; user?: User; guildId: string; }) { diff --git a/src/utils/updater.ts b/src/utils/updater.ts index f99c6ca1..25fe6ee8 100644 --- a/src/utils/updater.ts +++ b/src/utils/updater.ts @@ -39,10 +39,15 @@ async function Unwrap(p: Promise>) { export async function checkForUpdates() { changes = await Unwrap(VencordNative.updater.getUpdates()); - if (changes.some(c => c.hash === gitHash)) { - isNewer = true; - return (isOutdated = false); + + // we only want to check this for the git updater, not the http updater + if (!IS_STANDALONE) { + if (changes.some(c => c.hash === gitHash)) { + isNewer = true; + return (isOutdated = false); + } } + return (isOutdated = changes.length > 0); }