|
| 1 | +import DiscordRpc from 'discord-rpc' |
| 2 | + |
| 3 | +const CLIENT_ID = '1215618460231401482' |
| 4 | + |
| 5 | +const IDLE_STATE = { |
| 6 | + details: 'In Library', |
| 7 | + state: 'xCloud & Console Streaming', |
| 8 | + largeImageKey: 'xstreaming_logo', |
| 9 | + largeImageText: 'XStreaming Desktop', |
| 10 | + instance: false, |
| 11 | +} |
| 12 | + |
| 13 | +export default class DiscordRpcHelper { |
| 14 | + private _client: DiscordRpc.Client | null = null |
| 15 | + private _connected = false |
| 16 | + private _appStartTime: Date |
| 17 | + private _playStartTime: Date | null = null |
| 18 | + private _reconnectTimer: NodeJS.Timeout | null = null |
| 19 | + private _currentGameName = '' |
| 20 | + private _currentGameImage = '' |
| 21 | + private _isPlaying = false |
| 22 | + |
| 23 | + private _enabled = false |
| 24 | + |
| 25 | + private _targetEnabled = false |
| 26 | + private _debounceTimer: NodeJS.Timeout | null = null |
| 27 | + |
| 28 | + private _opQueue: Promise<void> = Promise.resolve() |
| 29 | + |
| 30 | + constructor() { |
| 31 | + this._appStartTime = new Date() |
| 32 | + try { DiscordRpc.register(CLIENT_ID) } catch {} |
| 33 | + } |
| 34 | + |
| 35 | + private _connect() { |
| 36 | + if (!this._enabled) return |
| 37 | + |
| 38 | + try { |
| 39 | + const client = new DiscordRpc.Client({ transport: 'ipc' }) |
| 40 | + this._client = client |
| 41 | + |
| 42 | + client.on('ready', () => { |
| 43 | + if (this._client !== client) return |
| 44 | + this._connected = true |
| 45 | + this._cancelReconnect() |
| 46 | + if (this._isPlaying) { |
| 47 | + this._applyPlaying(this._currentGameName, this._currentGameImage) |
| 48 | + } else { |
| 49 | + this._applyIdle() |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + client.on('disconnected' as any, () => { |
| 54 | + if (this._client !== client) return |
| 55 | + this._connected = false |
| 56 | + if (this._enabled) this._scheduleReconnect() |
| 57 | + }) |
| 58 | + |
| 59 | + client |
| 60 | + .login({ clientId: CLIENT_ID }) |
| 61 | + .catch(() => { |
| 62 | + if (this._client !== client) return |
| 63 | + this._connected = false |
| 64 | + if (this._enabled) this._scheduleReconnect() |
| 65 | + }) |
| 66 | + } catch { |
| 67 | + if (this._enabled) this._scheduleReconnect() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private _scheduleReconnect() { |
| 72 | + if (this._reconnectTimer) return |
| 73 | + this._reconnectTimer = setTimeout(() => { |
| 74 | + this._reconnectTimer = null |
| 75 | + if (this._enabled) this._connect() |
| 76 | + }, 15_000) |
| 77 | + } |
| 78 | + |
| 79 | + private _cancelReconnect() { |
| 80 | + if (this._reconnectTimer) { |
| 81 | + clearTimeout(this._reconnectTimer) |
| 82 | + this._reconnectTimer = null |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private _destroyClient(): Promise<void> { |
| 87 | + this._cancelReconnect() |
| 88 | + const clientToDestroy = this._client |
| 89 | + this._client = null |
| 90 | + this._connected = false |
| 91 | + |
| 92 | + if (!clientToDestroy) return Promise.resolve() |
| 93 | + |
| 94 | + return clientToDestroy |
| 95 | + .clearActivity() |
| 96 | + .catch(() => { }) |
| 97 | + .finally(() => clientToDestroy.destroy().catch(() => { })) |
| 98 | + } |
| 99 | + |
| 100 | + private _applyIdle() { |
| 101 | + if (!this._connected || !this._client) return |
| 102 | + this._client |
| 103 | + .setActivity({ |
| 104 | + ...IDLE_STATE, |
| 105 | + startTimestamp: this._appStartTime, |
| 106 | + }) |
| 107 | + .catch(() => { }) |
| 108 | + } |
| 109 | + |
| 110 | + private _applyPlaying(gameName: string, gameImageUrl: string) { |
| 111 | + if (!this._connected || !this._client) return |
| 112 | + |
| 113 | + const largeImage = |
| 114 | + gameImageUrl && gameImageUrl.startsWith('http') |
| 115 | + ? gameImageUrl |
| 116 | + : 'xstreaming_logo' |
| 117 | + |
| 118 | + this._client |
| 119 | + .setActivity({ |
| 120 | + details: gameName || 'Playing a game', |
| 121 | + state: 'Streaming via xCloud', |
| 122 | + largeImageKey: largeImage, |
| 123 | + largeImageText: gameName || 'Game', |
| 124 | + smallImageKey: 'xstreaming_logo', |
| 125 | + smallImageText: 'XStreaming Desktop', |
| 126 | + startTimestamp: this._playStartTime!, |
| 127 | + instance: false, |
| 128 | + }) |
| 129 | + .catch(() => { }) |
| 130 | + } |
| 131 | + |
| 132 | + enable() { |
| 133 | + this._targetEnabled = true |
| 134 | + this._scheduleUpdate() |
| 135 | + } |
| 136 | + |
| 137 | + disable() { |
| 138 | + this._targetEnabled = false |
| 139 | + this._scheduleUpdate() |
| 140 | + } |
| 141 | + |
| 142 | + private _scheduleUpdate() { |
| 143 | + if (this._debounceTimer) clearTimeout(this._debounceTimer) |
| 144 | + |
| 145 | + this._debounceTimer = setTimeout(() => { |
| 146 | + this._debounceTimer = null |
| 147 | + |
| 148 | + if (this._targetEnabled && !this._enabled) { |
| 149 | + this._enabled = true |
| 150 | + this._opQueue = this._opQueue.then(() => { |
| 151 | + if (this._enabled) this._connect() |
| 152 | + }) |
| 153 | + } else if (!this._targetEnabled && this._enabled) { |
| 154 | + this._enabled = false |
| 155 | + this._opQueue = this._opQueue.then(() => this._destroyClient()) |
| 156 | + } |
| 157 | + }, 500) |
| 158 | + } |
| 159 | + |
| 160 | + setIdle() { |
| 161 | + if (!this._targetEnabled) return |
| 162 | + this._isPlaying = false |
| 163 | + this._playStartTime = null |
| 164 | + this._applyIdle() |
| 165 | + } |
| 166 | + |
| 167 | + setPlaying(gameName: string, gameImageUrl: string) { |
| 168 | + if (!this._targetEnabled) return |
| 169 | + this._isPlaying = true |
| 170 | + this._playStartTime = new Date() |
| 171 | + this._currentGameName = gameName |
| 172 | + this._currentGameImage = gameImageUrl |
| 173 | + this._applyPlaying(gameName, gameImageUrl) |
| 174 | + } |
| 175 | + |
| 176 | + clearPlaying() { |
| 177 | + if (!this._targetEnabled) return |
| 178 | + this._isPlaying = false |
| 179 | + this._playStartTime = null |
| 180 | + this._applyIdle() |
| 181 | + } |
| 182 | + |
| 183 | + destroy() { |
| 184 | + this._targetEnabled = false |
| 185 | + this._enabled = false |
| 186 | + if (this._debounceTimer) clearTimeout(this._debounceTimer) |
| 187 | + this._debounceTimer = null |
| 188 | + this._opQueue = this._opQueue.then(() => this._destroyClient()) |
| 189 | + } |
| 190 | +} |
0 commit comments