mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-04-05 18:01:56 +02:00
Co-authored-by: Hannah Lindrob <hannahlindrob@ourlook.com> Co-authored-by: Sapphire <imsapphire0@gmail.com>
50 lines
1.1 KiB
TypeScript
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();
|