diff --git a/src/plugins/customRPC/index.tsx b/src/plugins/customRPC/index.tsx index bafff2c2e..774cba0e1 100644 --- a/src/plugins/customRPC/index.tsx +++ b/src/plugins/customRPC/index.tsx @@ -138,8 +138,14 @@ async function createActivity(): Promise { case TimestampMode.CUSTOM: if (startTime || endTime) { activity.timestamps = {}; - if (startTime) activity.timestamps.start = startTime; - if (endTime) activity.timestamps.end = endTime; + if (startTime && endTime && endTime > startTime) { + const anchor = getLoopAnchor(); + activity.timestamps.start = anchor; + activity.timestamps.end = anchor + (endTime - startTime); + } else { + if (startTime) activity.timestamps.start = startTime; + if (endTime) activity.timestamps.end = endTime; + } } break; case TimestampMode.NONE: @@ -212,6 +218,39 @@ export async function setRpc(disable?: boolean) { }); } +let loopInterval: ReturnType | undefined; +let loopAnchor = 0; + +function getLoopAnchor() { + return loopAnchor; +} + +function startTimestampLoop() { + const { timestampMode, startTime, endTime } = settings.store; + if (timestampMode !== TimestampMode.CUSTOM || !startTime || !endTime) return; + const duration = endTime - startTime; + if (duration <= 0) return; + + stopTimestampLoop(); + loopAnchor = Date.now(); + + loopInterval = setInterval(() => { + + if (Date.now() >= loopAnchor + duration) { + loopAnchor = Date.now(); + setRpc(); + } + }, 1000); +} + +function stopTimestampLoop() { + if (loopInterval !== undefined) { + clearInterval(loopInterval); + loopInterval = undefined; + } + loopAnchor = 0; +} + export default definePlugin({ name: "CustomRPC", description: "Add a fully customisable Rich Presence (Game status) to your Discord profile", @@ -222,8 +261,14 @@ export default definePlugin({ requiresRestart: false, settings, - start: setRpc, - stop: () => setRpc(true), + start() { + startTimestampLoop(); + setRpc(); + }, + stop() { + setRpc(true); + stopTimestampLoop(); + }, // Discord hides buttons on your own Rich Presence for some reason. This patch disables that behaviour patches: [