Skip to content

Commit

Permalink
Revert "Adds a delay between clearing and setting of discord presence"
Browse files Browse the repository at this point in the history
This reverts commit 3cc6ad7.
  • Loading branch information
digimezzo committed Dec 13, 2024
1 parent 0291d9e commit bdaf66f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/app/services/discord/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class DiscordService {

private updatePresence(): void {
if (this.playbackService.currentTrack == undefined) {
this.logger.info(`No currentTrack was found. Not setting Discord Rich Presence.`, 'DiscordService', 'updatePresence');
this.logger.info(`No currentTrack was found. Not setting Discord Rich Presence.`, 'DiscordService', 'setPresence');

return;
}
Expand Down
101 changes: 46 additions & 55 deletions src/app/services/discord/presence-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { Logger } from '../../common/logger';

@Injectable()
export class PresenceUpdater {
private _discordClient: any;
private _discordClientIsReady: boolean = false;
private discordClient: any;

public constructor(private logger: Logger) {}

Expand All @@ -24,14 +23,13 @@ export class PresenceUpdater {
shouldSendTimestamps: boolean,
startTime: number,
): void {
if (this._discordClient == undefined || !this._discordClientIsReady) {
if (this.discordClient == undefined || !(<boolean>this.discordClient.discordClientIsReady)) {
const clientId: string = SensitiveInformation.discordClientId;
this._discordClient = new Client({ transport: 'ipc' });
this._discordClient.login({ clientId }).catch(console.error);
this.discordClient = new Client({ transport: 'ipc' });
this.discordClient.login({ clientId }).catch(console.error);

// 'Ready' is emitted when the client is connected to Discord
this._discordClient.on('ready', () => {
this._discordClientIsReady = true;
this.discordClient.on('ready', () => {
this.discordClient.discordClientIsReady = true;
this.logger.info(`Discord client is ready`, 'DiscordService', 'updatePresence');

this.setPresence(
Expand All @@ -46,29 +44,16 @@ export class PresenceUpdater {
);
});

// 'Disconnected' is emitted when the client has disconnected from Discord
this._discordClient.on('disconnected', () => {
this._discordClientIsReady = false;
this.discordClient.on('disconnected', () => {
this.discordClient.discordClientIsReady = false;
this.logger.info(`Discord client has disconnected`, 'DiscordService', 'updatePresence');
});
} else {
this.setPresence(details, state, smallImageKey, smallImageText, largeImageKey, largeImageText, shouldSendTimestamps, startTime);
}
}

public clearPresence(): void {
if (this._discordClient == undefined || !this._discordClientIsReady) {
return;
}

try {
this._discordClient.clearActivity();
} catch (e: unknown) {
this.logger.error(e, 'Could not clear Discord Rich Presence', 'DiscordService', 'clearPresence');
}
}

private setPresence(
public setPresence(
details: string,
state: string,
smallImageKey: string,
Expand All @@ -79,40 +64,46 @@ export class PresenceUpdater {
startTime: number,
): void {
try {
this._discordClient.clearActivity();
// It seems required to clear the activity before setting a new one
// Otherwise the activity will not be updated most of the time
this.discordClient.clearActivity();

if (shouldSendTimestamps) {
this.discordClient.setActivity({
details: details,
state: state,
startTimestamp: startTime,
largeImageKey: largeImageKey,
largeImageText: largeImageText,
smallImageKey: smallImageKey,
smallImageText: smallImageText,
instance: false,
});
} else {
this.discordClient.setActivity({
details: details,
state: state,
largeImageKey: largeImageKey,
largeImageText: largeImageText,
smallImageKey: smallImageKey,
smallImageText: smallImageText,
instance: false,
});
}

this.logger.info(`Set Discord Rich Presence`, 'DiscordService', 'setPresence');
} catch (e: unknown) {
this.logger.error(e, 'Could not clear Discord Rich Presence', 'DiscordService', 'clearPresence');
this.logger.error(e, 'Could not set Discord Rich Presence', 'DiscordService', 'setPresence');
}
}

setTimeout(() => {
try {
if (shouldSendTimestamps) {
this._discordClient.setActivity({
details: details,
state: state,
startTimestamp: startTime,
largeImageKey: largeImageKey,
largeImageText: largeImageText,
smallImageKey: smallImageKey,
smallImageText: smallImageText,
instance: false,
});
} else {
this._discordClient.setActivity({
details: details,
state: state,
largeImageKey: largeImageKey,
largeImageText: largeImageText,
smallImageKey: smallImageKey,
smallImageText: smallImageText,
instance: false,
});
}

this.logger.info(`Set Discord Rich Presence`, 'DiscordService', 'setPresence');
} catch (e: unknown) {
this.logger.error(e, 'Could not set Discord Rich Presence', 'DiscordService', 'setPresence');
public clearPresence(): void {
try {
if (this.discordClient != undefined && (this.discordClient.discordClientIsReady as boolean)) {
this.discordClient.clearActivity();
}
}, 500);
} catch (e: unknown) {
this.logger.error(e, 'Could not clear Discord Rich Presence', 'DiscordService', 'clearPresence');
}
}
}

0 comments on commit bdaf66f

Please sign in to comment.