Implemented the logic in the discord.ts file as mentioned in earlier commits

This commit is contained in:
TheRockYT 2024-01-08 21:13:02 +01:00
parent 7c6d2df16a
commit 36a2367397

View File

@ -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) {
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<string, string>(settings.discord.idleText) ?? "Browsing Tidal";
} else {
const showSong = settingsStore.get<string, boolean>(settings.discord.showSong) ?? false;
if (showSong) {
const includeTimestamps =
settingsStore.get<string, boolean>(settings.discord.includeTimestamps) ?? true;
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));
const detailsPrefix =
settingsStore.get<string, string>(settings.discord.detailsPrefix) ?? "Listening to ";
const buttonText =
settingsStore.get<string, string>(settings.discord.buttonText) ?? "Play on TIDAL";
const includeTimestamps =
settingsStore.get<string, boolean>(settings.discord.includeTimestamps) ?? true;
presence.startTimestamp = now;
presence.endTimestamp = remaining;
}
const detailsPrefix = settingsStore.get<string, string>(settings.discord.detailsPrefix) ?? "Listening to ";
const buttonText = settingsStore.get<string, string>(settings.discord.buttonText) ?? "Play on TIDAL";
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 }],
},
};
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 {
activity = {
...activity,
...{
details: `Watching ${mediaInfo.title}`,
state: mediaInfo.artists,
},
};
presence.details = `Watching ${mediaInfo.title}`;
presence.state = mediaInfo.artists;
}
rpc.setActivity(activity);
} else {
presence.details = settingsStore.get<string, string>(settings.discord.listeningText) ?? "Listening Tidal";
}
};
const idleStatus = {
details: `Browsing Tidal`,
largeImageKey: "tidal-hifi-icon",
largeImageText: `TIDAL Hi-Fi ${app.getVersion()}`,
instance: false,
}
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);
},