diff --git a/src/constants/settings.ts b/src/constants/settings.ts index 8750dad..43c5d97 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -24,6 +24,9 @@ export const settings = { detailsPrefix: "discord.detailsPrefix", buttonText: "discord.buttonText", includeTimestamps: "discord.includeTimestamps", + showSong: "discord.showSong", + idleText: "discord.idleText", + listeningText: "discord.listeningText", }, ListenBrainz: { root: "ListenBrainz", diff --git a/src/pages/settings/preload.ts b/src/pages/settings/preload.ts index 7898a3d..53bfc98 100644 --- a/src/pages/settings/preload.ts +++ b/src/pages/settings/preload.ts @@ -20,6 +20,11 @@ const switchesWithSettings = { classToHide: "discord_options", settingsKey: settings.enableDiscord, }, + discord_show_song: { + switch: "discord_show_song", + classToHide: "discord_show_song_options", + settingsKey: settings.discord.showSong, + } }; let adBlock: HTMLInputElement, @@ -49,7 +54,10 @@ let adBlock: HTMLInputElement, enableWaylandSupport: HTMLInputElement, discord_details_prefix: HTMLInputElement, discord_include_timestamps: HTMLInputElement, - discord_button_text: HTMLInputElement; + discord_button_text: HTMLInputElement, + discord_show_song: HTMLInputElement, + discord_idle_text: HTMLInputElement, + discord_listening_text: HTMLInputElement; addCustomCss(app); @@ -138,6 +146,9 @@ function refreshSettings() { discord_details_prefix.value = settingsStore.get(settings.discord.detailsPrefix); discord_include_timestamps.checked = settingsStore.get(settings.discord.includeTimestamps); discord_button_text.value = settingsStore.get(settings.discord.buttonText); + discord_show_song.checked = settingsStore.get(settings.discord.showSong); + discord_idle_text.value = settingsStore.get(settings.discord.idleText); + discord_listening_text.value = settingsStore.get(settings.discord.listeningText); // set state of all switches with additional settings Object.values(switchesWithSettings).forEach((settingSwitch) => { @@ -251,6 +262,9 @@ window.addEventListener("DOMContentLoaded", () => { discord_include_timestamps = get("discord_include_timestamps"); listenbrainz_delay = get("listenbrainz_delay"); discord_button_text = get("discord_button_text"); + discord_show_song = get("discord_show_song"); + discord_listening_text = get("discord_listening_text"); + discord_idle_text = get("discord_idle_text") refreshSettings(); addInputListener(adBlock, settings.adBlock); @@ -285,4 +299,7 @@ window.addEventListener("DOMContentLoaded", () => { addInputListener(discord_details_prefix, settings.discord.detailsPrefix); addInputListener(discord_include_timestamps, settings.discord.includeTimestamps); addInputListener(discord_button_text, settings.discord.buttonText); + addInputListener(discord_show_song, settings.discord.showSong, switchesWithSettings.discord_show_song); + addInputListener(discord_idle_text, settings.discord.idleText); + addInputListener(discord_listening_text, settings.discord.listeningText); }); diff --git a/src/pages/settings/settings.html b/src/pages/settings/settings.html index b250a89..41033ce 100644 --- a/src/pages/settings/settings.html +++ b/src/pages/settings/settings.html @@ -216,32 +216,64 @@
+
-

Include timestamps

-

Show current/end playtime in the Discord client

+

Idle Text

+

The text displayed on Discord's rich presence while idling in the app.

+ +
+
+ +
+
+

Listening Text

+

The text displayed on Discord's rich presence while listening to a song.

+ +
+
+ +
+
+

Show song

+

Show the current song in the Discord client

-
-
-

Details prefix

-

Prefix for the "details" field of Discord's rich presence.

- + -
-
-

Button text

-

Text to display on the button below the song information.

- -
-
@@ -422,4 +454,4 @@
- \ No newline at end of file + diff --git a/src/scripts/discord.ts b/src/scripts/discord.ts index 31088c7..c12dd76 100644 --- a/src/scripts/discord.ts +++ b/src/scripts/discord.ts @@ -1,4 +1,4 @@ -import { Client } from "discord-rpc"; +import { Client, Presence } from "discord-rpc"; import { app, ipcMain } from "electron"; import { globalEvents } from "../constants/globalEvents"; import { settings } from "../constants/settings"; @@ -18,58 +18,54 @@ function timeToSeconds(timeArray: string[]) { export let rpc: Client; const observer = () => { - if (mediaInfo.status === MediaStatus.paused && rpc) { - rpc.setActivity(idleStatus); - } else if (rpc) { - const currentSeconds = timeToSeconds(mediaInfo.current.split(":")); - const durationSeconds = timeToSeconds(mediaInfo.duration.split(":")); - const date = new Date(); - const now = (date.getTime() / 1000) | 0; - const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds)); - const detailsPrefix = - settingsStore.get(settings.discord.detailsPrefix) ?? "Listening to "; - const buttonText = - settingsStore.get(settings.discord.buttonText) ?? "Play on TIDAL"; - const includeTimestamps = - settingsStore.get(settings.discord.includeTimestamps) ?? true; - - let activity = { - ...idleStatus, - ...{ - startTimestamp: includeTimestamps ? now : undefined, - endTimestamp: includeTimestamps ? remaining : undefined, - }, - }; - if (mediaInfo.url) { - activity = { - ...activity, - ...{ - details: `${detailsPrefix}${mediaInfo.title}`, - state: mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)", - largeImageKey: mediaInfo.image, - largeImageText: mediaInfo.album ? mediaInfo.album : `${idleStatus.largeImageText}`, - buttons: [{ label: buttonText, url: mediaInfo.url }], - }, - }; - } else { - activity = { - ...activity, - ...{ - details: `Watching ${mediaInfo.title}`, - state: mediaInfo.artists, - }, - }; - } - - rpc.setActivity(activity); + if (rpc) { + rpc.setActivity(getActivity()); } }; +const getActivity = (): Presence | undefined => { + const presence: Presence | undefined = { + largeImageKey: "tidal-hifi-icon", + largeImageText: `TIDAL Hi-Fi ${app.getVersion()}`, + instance: false, + }; + if (mediaInfo.status === MediaStatus.paused) { + presence.details = settingsStore.get(settings.discord.idleText) ?? "Browsing Tidal"; + } else { + const showSong = settingsStore.get(settings.discord.showSong) ?? false; + if (showSong) { + const includeTimestamps = + settingsStore.get(settings.discord.includeTimestamps) ?? true; -const idleStatus = { - details: `Browsing Tidal`, - largeImageKey: "tidal-hifi-icon", - largeImageText: `TIDAL Hi-Fi ${app.getVersion()}`, - instance: false, + if (includeTimestamps) { + const currentSeconds = timeToSeconds(mediaInfo.current.split(":")); + const durationSeconds = timeToSeconds(mediaInfo.duration.split(":")); + const date = new Date(); + const now = (date.getTime() / 1000) | 0; + const remaining = date.setSeconds(date.getSeconds() + (durationSeconds - currentSeconds)); + presence.startTimestamp = now; + presence.endTimestamp = remaining; + } + + const detailsPrefix = settingsStore.get(settings.discord.detailsPrefix) ?? "Listening to "; + const buttonText = settingsStore.get(settings.discord.buttonText) ?? "Play on TIDAL"; + + if (mediaInfo.url) { + presence.details = `${detailsPrefix}${mediaInfo.title}`; + presence.state = mediaInfo.artists ? mediaInfo.artists : "unknown artist(s)"; + presence.largeImageKey = mediaInfo.image; + if (mediaInfo.album) { + presence.largeImageText = mediaInfo.album; + } + presence.buttons = [{ label: buttonText, url: mediaInfo.url }]; + } else { + presence.details = `Watching ${mediaInfo.title}`; + presence.state = mediaInfo.artists; + } + } else { + presence.details = settingsStore.get(settings.discord.listeningText) ?? "Listening Tidal"; + } + } + return presence; }; /** @@ -80,7 +76,7 @@ export const initRPC = () => { rpc.login({ clientId }).then( () => { rpc.on("ready", () => { - rpc.setActivity(idleStatus); + rpc.setActivity(getActivity()); }); ipcMain.on(globalEvents.updateInfo, observer); }, diff --git a/src/scripts/settings.ts b/src/scripts/settings.ts index 10e08bc..de2cd79 100644 --- a/src/scripts/settings.ts +++ b/src/scripts/settings.ts @@ -19,6 +19,9 @@ export const settingsStore = new Store({ enableCustomHotkeys: false, enableDiscord: false, discord: { + showSong: false, + idleText: "Browsing Tidal", + listeningText: "Listening Tidal", detailsPrefix: "Listening to ", buttonText: "Play on Tidal", includeTimestamps: true,