CustomRPC custom timestamp looping (#1023)

* Refactor timestamp handling and loop management

* Added gemini changes

* Update index.tsx

* Update index.tsx

---------

Co-authored-by: thororen <78185467+thororen1234@users.noreply.github.com>
This commit is contained in:
aliceenight 2026-04-29 00:19:58 +02:00 • committed by GitHub
parent 2609cbe99d
commit 25502c6590
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -138,8 +138,14 @@ async function createActivity(): Promise<Activity | undefined> {
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<typeof setInterval> | 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: [