fixed discord rpc on anime

This commit is contained in:
2025-12-18 15:17:43 +01:00
parent 1ebac7ee15
commit c7ed97a452
5 changed files with 235 additions and 103 deletions

View File

@@ -1,16 +1,18 @@
// @ts-ignore
import { DiscordRPCClient } from "@ryuziii/discord-rpc";
import { Client } from "@xhayper/discord-rpc";
let rpcClient: DiscordRPCClient | null = null;
let rpcClient: Client | null = null;
let reconnectTimer: NodeJS.Timeout | null = null;
let connected: boolean = false;
let connected = false;
type RPCMode = "watching" | "reading" | string;
interface RPCData {
details?: string;
state?: string;
mode?: RPCMode;
mode?: string;
startTimestamp?: number;
endTimestamp?: number;
paused?: boolean;
version?: string;
}
@@ -30,91 +32,68 @@ function attemptReconnect(clientId: string) {
}
export function initRPC(clientId: string) {
if (rpcClient) {
try { rpcClient.destroy(); } catch (e) {}
try { rpcClient.destroy(); } catch {}
rpcClient = null;
}
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
console.log(`Discord RPC: Starting with id ...${clientId.slice(-4)}`);
try {
rpcClient = new DiscordRPCClient({
clientId: clientId,
transport: 'ipc'
});
} catch (err) {
console.error('Discord RPC:', err);
return;
}
rpcClient = new Client({ clientId });
rpcClient.on("ready", () => {
connected = true;
const user = rpcClient?.user ? rpcClient.user.username : 'User';
console.log(`Discord RPC: Authenticated for: ${user}`);
console.log("Discord RPC conectado");
setTimeout(() => {
setActivity({ details: "Browsing", state: "In App", mode: "idle" });
setActivity({ details: "Browsing", state: "In App" });
}, 1000);
});
rpcClient.on('disconnected', () => {
console.log('Discord RPC: Desconexión detectada.');
rpcClient.on("disconnected", () => {
connected = false;
attemptReconnect(clientId);
});
rpcClient.on('error', (err: { message: any; }) => {
console.error('[Discord RPC] Error:', err.message);
if (connected) {
attemptReconnect(clientId);
}
rpcClient.on("error", () => {
if (connected) attemptReconnect(clientId);
});
try {
rpcClient.connect().catch((err: { message: any; }) => {
console.error('Discord RPC: Error al conectar', err.message);
attemptReconnect(clientId);
});
} catch (err) {
console.error('Discord RPC: Error al iniciar la conexión', err);
rpcClient.login().catch(() => {
attemptReconnect(clientId);
}
});
}
export function setActivity(data: RPCData = {}) {
if (!rpcClient || !connected) return;
let type;
let state = data.state;
let details = data.details;
let type = 0;
if (data.mode === "watching") type = 3;
if (data.mode === "reading") type = 0;
if (data.mode === "watching") {
type = 3
} else if (data.mode === "reading") {
type = 0
const activity: any = {
details: data.details,
state: data.state,
type,
instance: false
};
if (data.paused) {
activity.largeImageText = "⏸ ";
delete activity.startTimestamp;
delete activity.endTimestamp;
} else {
type = 0
activity.largeImageKey = "bigpicture";
activity.largeImageText = data.version ?? "v2.0.0";
if (data.startTimestamp && data.endTimestamp) {
activity.startTimestamp = data.startTimestamp;
activity.endTimestamp = data.endTimestamp;
}
}
try {
rpcClient.setActivity({
details: details,
state: state,
type: type,
startTimestamp: new Date(),
largeImageKey: "bigpicture",
largeImageText: "v2.0.0",
instance: false
});
} catch (error) {
console.error("Discord RPC: Failed to set activity", error);
}
rpcClient.user?.setActivity(activity);
}

View File

@@ -11,16 +11,29 @@ export function init() {
}
export async function setRPC(request: FastifyRequest, reply: FastifyReply) {
const { details, state, mode } = request.body as {
const {
details,
state,
mode,
startTimestamp,
endTimestamp,
paused
} = request.body as {
details?: string;
state?: string;
mode?: "watching" | "reading" | string;
startTimestamp?: number;
endTimestamp?: number;
paused?: boolean;
};
setActivity({
details,
state,
mode
mode,
startTimestamp,
endTimestamp,
paused
});
return reply.send({ ok: true });