-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bugfix/discord tray icon incompatibility (#741)
* Fixes false positive virus detection problems with Windows build * Initial fix * Adds logging * Refactoring * More refactoring * Smalle changes * Some renames * Works * Works well * Major rewrite
- Loading branch information
Showing
120 changed files
with
504 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum DiscordApiCommandType { | ||
SetPresence, | ||
ClearPresence, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PresenceArgs } from './presence-args'; | ||
import { DiscordApiCommandType } from './discord-api-command-type'; | ||
|
||
export class DiscordApiCommand { | ||
public constructor( | ||
public commandType: DiscordApiCommandType, | ||
public args: PresenceArgs | undefined, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Client, Presence } from 'discord-rpc'; | ||
import log from 'electron-log'; | ||
import { PresenceArgs } from './presence-args'; | ||
|
||
export class DiscordApi { | ||
private readonly _clientId: string; | ||
private _client: Client; | ||
private _isReady: boolean; | ||
private _presenceToSetWhenReady: PresenceArgs | undefined; | ||
private _isReconnecting: boolean = false; | ||
|
||
public constructor(clientId: string) { | ||
this._clientId = clientId; | ||
} | ||
|
||
private reconnect(): void { | ||
if (this._isReconnecting) { | ||
log.info('[DiscordApi] [reconnect] Already attempting to reconnect. Skipping...'); | ||
return; | ||
} | ||
|
||
this._isReconnecting = true; | ||
this._isReady = false; | ||
this._client = new Client({ transport: 'ipc' }); | ||
|
||
this._client.on('ready', () => { | ||
log.info('[DiscordApi] [ready] Discord client is ready!'); | ||
this._isReady = true; | ||
|
||
if (this._presenceToSetWhenReady) { | ||
this.setPresence(this._presenceToSetWhenReady); | ||
this._presenceToSetWhenReady = undefined; | ||
} | ||
}); | ||
|
||
this._client.on('disconnected', () => { | ||
log.info('[DiscordApi] [disconnected] Discord client disconnected. Attempting to reconnect...'); | ||
this._isReady = false; | ||
this.login(); | ||
}); | ||
|
||
this.login(); | ||
} | ||
|
||
private async login(): Promise<void> { | ||
try { | ||
await this._client.login({ clientId: this._clientId }); | ||
log.info('[DiscordApi] [login] Successfully logged into Discord client'); | ||
} catch (error) { | ||
log.error(`[DiscordApi] [login] Failed to log into Discord client: ${error}`); | ||
} finally { | ||
this._isReconnecting = false; | ||
} | ||
} | ||
|
||
public setPresence(args: PresenceArgs): void { | ||
if (!this._isReady) { | ||
log.warn('[DiscordApi] [setPresence] Discord client is not ready. Attempting reconnect.'); | ||
this._presenceToSetWhenReady = args; | ||
this.reconnect(); | ||
|
||
return; | ||
} | ||
|
||
const presence: Presence = { | ||
details: `${args.title}`, | ||
state: `${args.artists}`, | ||
largeImageKey: args.largeImageKey, | ||
largeImageText: args.largeImageText, | ||
smallImageKey: args.smallImageKey, | ||
smallImageText: args.smallImageText, | ||
}; | ||
|
||
if (args.shouldSendTimestamps && args.startTime) { | ||
presence.startTimestamp = Math.floor(args.startTime / 1000); | ||
} | ||
|
||
this._client.setActivity(presence); | ||
log.info(`[DiscordApi] [setPresence] Rich Presence updated: ${presence.state} - ${presence.details} (${presence.smallImageKey})`); | ||
} | ||
|
||
public clearPresence(): void { | ||
this._presenceToSetWhenReady = undefined; | ||
|
||
if (!this._isReady) { | ||
log.warn('[DiscordApi] [clearPresence] Discord client is not ready. Cannot clear presence.'); | ||
return; | ||
} | ||
|
||
this._client.clearActivity(); | ||
log.info('[DiscordApi] [clearPresence] Rich Presence cleared.'); | ||
} | ||
|
||
public shutdown(): void { | ||
this._presenceToSetWhenReady = undefined; | ||
|
||
this._client.destroy(); | ||
log.info('[DiscordApi] [shutdown] Discord client destroyed.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface PresenceArgs { | ||
title: string; | ||
artists: string; | ||
smallImageKey?: string; | ||
smallImageText?: string; | ||
largeImageKey?: string; | ||
largeImageText?: string; | ||
shouldSendTimestamps?: boolean; | ||
startTime?: number; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export class SensitiveInformation { | ||
public static readonly discordClientId: string = '826521040275636325'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum DiscordApiCommandType { | ||
SetPresence, | ||
ClearPresence, | ||
} |
Oops, something went wrong.