Files
SlimeVR-Server/gui/electron/main/presence.ts
lucas lelievre a891203204 Switch to Electron (#1747)
Co-authored-by: Hannah Lindrob <hannahlindrob@ourlook.com>
Co-authored-by: Sapphire <imsapphire0@gmail.com>
2026-03-10 21:38:02 +01:00

50 lines
1.1 KiB
TypeScript

import { Client } from '@xhayper/discord-rpc';
import { logger } from './logger';
export const richPresence = () => {
const initialState = () => ({ ready: false, start: Date.now() });
const state = initialState();
const client = new Client({
clientId: '1237970689009647639',
transport: { type: 'ipc' },
});
client.on('ready', () => {
state.ready = true;
});
client.on('disconnected', () => {
state.ready = false;
});
return {
state,
connect: async () => {
try {
await client.login();
} catch (e) {
logger.error(e, 'unable to connect to discord rpc');
}
},
updateActivity: (content: string) => {
if (!state.ready) return;
client.user
?.setActivity({
state: content,
largeImageKey: 'icon',
startTimestamp: state.start,
})
.catch((e) => {
logger.error(e, 'unable to update rpc activity');
});
},
destroy: () => {
client.destroy();
Object.assign(state, initialState());
},
};
};
export const discordPresence = richPresence();