Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Listening to" Discord RPC #488

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@types/swagger-jsdoc": "^6.0.4",
"axios": "^1.7.7",
"cors": "^2.8.5",
"discord-rpc": "^4.0.1",
"discord-rpc": "github:sKiLdUsT/RPC#types",
3top1a marked this conversation as resolved.
Show resolved Hide resolved
"electron-store": "^8.2.0",
"express": "^4.21.1",
"hotkeys-js": "^3.13.7",
Expand All @@ -56,7 +56,6 @@
"devDependencies": {
"@mastermindzh/prettier-config": "^1.0.0",
"@types/cors": "^2.8.17",
"@types/discord-rpc": "^4.0.8",
"@types/express": "^4.17.21",
"@types/node": "^20.14.10",
"@types/request": "^2.48.12",
Expand Down
24 changes: 15 additions & 9 deletions src/scripts/discord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Presence } from "discord-rpc";
import * as DRPC from "discord-rpc";
import { app, ipcMain } from "electron";
import { globalEvents } from "../constants/globalEvents";
import { settings } from "../constants/settings";
Expand All @@ -10,7 +10,9 @@ import { settingsStore } from "./settings";

const clientId = "833617820704440341";

export let rpc: Client;
export let rpc: DRPC.Client;

const ACTIVITY_LISTENING = 2;

const observer = () => {
if (rpc) {
Expand All @@ -33,8 +35,10 @@ const updateActivity = () => {
}
};

const getActivity = (): Presence => {
const presence: Presence = { ...defaultPresence };
const getActivity = (): DRPC.Presence => {
const presence: DRPC.Presence = { ...defaultPresence };

presence.type = ACTIVITY_LISTENING;

if (mediaInfo.status === MediaStatus.paused) {
presence.details =
Expand All @@ -50,6 +54,7 @@ const getActivity = (): Presence => {
settingsStore.get<string, string>(settings.discord.usingText) ?? "Playing media on TIDAL";
}
}

return presence;

function getFromStore() {
Expand Down Expand Up @@ -98,10 +103,11 @@ const getActivity = (): Presence => {
const currentSeconds = convertDurationToSeconds(mediaInfo.current);
const durationSeconds = convertDurationToSeconds(mediaInfo.duration);
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 now = Math.floor(date.getTime() / 1000);
const startTimestamp = now - currentSeconds;
const endTimestamp = startTimestamp + durationSeconds;
presence.startTimestamp = startTimestamp;
presence.endTimestamp = endTimestamp;
3top1a marked this conversation as resolved.
Show resolved Hide resolved
}
}
};
Expand All @@ -110,7 +116,7 @@ const getActivity = (): Presence => {
* Set up the discord rpc and listen on globalEvents.updateInfo
*/
export const initRPC = () => {
rpc = new Client({ transport: "ipc" });
rpc = new DRPC.Client({ transport: "ipc" });
rpc.login({ clientId }).then(
() => {
rpc.on("ready", () => {
Expand Down
39 changes: 39 additions & 0 deletions src/types/discord-rpc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
declare module 'discord-rpc' {
export class Client {
constructor(options: { transport: 'ipc' });
on(event: string, listener: (...args: any[]) => void): this;
login(options: { clientId: string }): Promise<void>;
setActivity(presence: Presence): Promise<void>;
clearActivity(): Promise<void>;
destroy(): Promise<void>;
}

export interface Presence {
state?: string;
details?: string;
startTimestamp?: number;
endTimestamp?: number;
largeImageKey?: string;
largeImageText?: string;
smallImageKey?: string;
smallImageText?: string;
partyId?: string;
partySize?: number;
partyMax?: number;
matchSecret?: string;
spectateSecret?: string;
joinSecret?: string;
instance?: boolean;
type?: number;
buttons?: Array<{ label: string; url: string }>;
}

export const ActivityTypes: {
PLAYING: 0,
STREAMING: 1,
LISTENING: 2,
WATCHING: 3,
CUSTOM: 4,
COMPETING: 5
};
}