From e0abc4a43bbf51ea8569109ff36f14457faa9a40 Mon Sep 17 00:00:00 2001 From: MindLated Date: Thu, 8 May 2025 21:34:05 +0200 Subject: [PATCH] fix(discord): Improve state verification before sending activity updates - Enhanced the throttling mechanism to ensure that activity updates are only sent if the song state has not changed. - Added checks for video ID and pause state to prevent unnecessary updates. - Improved handling of elapsed time during updates. --- src/plugins/discord/main.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/plugins/discord/main.ts b/src/plugins/discord/main.ts index afedc9b1..e0fd9a1f 100644 --- a/src/plugins/discord/main.ts +++ b/src/plugins/discord/main.ts @@ -201,7 +201,7 @@ export const backend = createBackend< const pauseChanged = songInfo.isPaused !== lastPausedState; const seeked = isSeek(lastElapsedSeconds, songInfo.elapsedSeconds ?? 0); if (songChanged || pauseChanged || seeked) { - // Immediate update + // Always cancel any pending throttle on important event if (updateTimeout) clearTimeout(updateTimeout); updateTimeout = null; sendActivityToDiscord(songInfo, config); @@ -218,10 +218,19 @@ export const backend = createBackend< lastElapsedSeconds = songInfo.elapsedSeconds ?? 0; } else { if (updateTimeout) clearTimeout(updateTimeout); + // Capture current state for verification before sending + const expectedVideoId = songInfo.videoId; + const expectedPaused = songInfo.isPaused; updateTimeout = setTimeout(() => { - sendActivityToDiscord(songInfo, config); - lastProgressUpdate = Date.now(); - lastElapsedSeconds = songInfo.elapsedSeconds ?? 0; + // Only send if state hasn't changed + if ( + info.lastSongInfo?.videoId === expectedVideoId && + info.lastSongInfo?.isPaused === expectedPaused + ) { + sendActivityToDiscord(info.lastSongInfo, config); + lastProgressUpdate = Date.now(); + lastElapsedSeconds = info.lastSongInfo.elapsedSeconds ?? 0; + } }, PROGRESS_THROTTLE_MS - (now - lastProgressUpdate)); } if (songInfo.isPaused && config.activityTimeoutEnabled) {