added rpc
This commit is contained in:
120
src/api/rpc/rp.service.ts
Normal file
120
src/api/rpc/rp.service.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
// @ts-ignore
|
||||
import { DiscordRPCClient } from "@ryuziii/discord-rpc";
|
||||
|
||||
let rpcClient: DiscordRPCClient | null = null;
|
||||
let reconnectTimer: NodeJS.Timeout | null = null;
|
||||
let connected: boolean = false;
|
||||
|
||||
type RPCMode = "watching" | "reading" | string;
|
||||
|
||||
interface RPCData {
|
||||
details?: string;
|
||||
state?: string;
|
||||
mode?: RPCMode;
|
||||
version?: string;
|
||||
}
|
||||
|
||||
function attemptReconnect(clientId: string) {
|
||||
connected = false;
|
||||
|
||||
if (reconnectTimer) {
|
||||
clearTimeout(reconnectTimer);
|
||||
reconnectTimer = null;
|
||||
}
|
||||
|
||||
console.log('Discord RPC: Trying to reconnect...');
|
||||
|
||||
reconnectTimer = setTimeout(() => {
|
||||
initRPC(clientId);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
export function initRPC(clientId: string) {
|
||||
|
||||
if (rpcClient) {
|
||||
try { rpcClient.destroy(); } catch (e) {}
|
||||
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.on("ready", () => {
|
||||
connected = true;
|
||||
const user = rpcClient?.user ? rpcClient.user.username : 'User';
|
||||
console.log(`Discord RPC: Authenticated for: ${user}`);
|
||||
|
||||
setTimeout(() => {
|
||||
setActivity({ details: "Browsing", state: "In App", mode: "idle" });
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
rpcClient.on('disconnected', () => {
|
||||
console.log('Discord RPC: Desconexión detectada.');
|
||||
attemptReconnect(clientId);
|
||||
});
|
||||
|
||||
rpcClient.on('error', (err: { message: any; }) => {
|
||||
console.error('[Discord RPC] Error:', err.message);
|
||||
|
||||
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);
|
||||
attemptReconnect(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
export function setActivity(data: RPCData = {}) {
|
||||
if (!rpcClient || !connected) return;
|
||||
|
||||
let type;
|
||||
let state = data.state;
|
||||
let details = data.details;
|
||||
|
||||
if (data.mode === "watching") {
|
||||
type = 3
|
||||
} else if (data.mode === "reading") {
|
||||
type = 0
|
||||
} else {
|
||||
type = 0
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
27
src/api/rpc/rpc.controller.ts
Normal file
27
src/api/rpc/rpc.controller.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { FastifyRequest, FastifyReply } from "fastify";
|
||||
import { setActivity, initRPC } from "./rp.service";
|
||||
|
||||
let initialized = false;
|
||||
|
||||
export function init() {
|
||||
if (!initialized) {
|
||||
initRPC(process.env.DISCORD_CLIENT_ID!);
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
export async function setRPC(request: FastifyRequest, reply: FastifyReply) {
|
||||
const { details, state, mode } = request.body as {
|
||||
details?: string;
|
||||
state?: string;
|
||||
mode?: "watching" | "reading" | string;
|
||||
};
|
||||
|
||||
setActivity({
|
||||
details,
|
||||
state,
|
||||
mode
|
||||
});
|
||||
|
||||
return reply.send({ ok: true });
|
||||
}
|
||||
8
src/api/rpc/rpc.routes.ts
Normal file
8
src/api/rpc/rpc.routes.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import * as controller from "./rpc.controller";
|
||||
|
||||
async function rpcRoutes(fastify: FastifyInstance) {
|
||||
fastify.post("/rpc", controller.setRPC);
|
||||
}
|
||||
|
||||
export default rpcRoutes;
|
||||
@@ -66,6 +66,16 @@ async function loadMetadata() {
|
||||
document.getElementById('anime-title-details2').innerText = title;
|
||||
document.title = `Watching ${title} - Ep ${currentEpisode}`;
|
||||
|
||||
fetch("/api/rpc", {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({
|
||||
details: title,
|
||||
state: `Episode ${currentEpisode}`,
|
||||
mode: "watching"
|
||||
})
|
||||
});
|
||||
|
||||
const tempDiv = document.createElement('div');
|
||||
tempDiv.innerHTML = description;
|
||||
document.getElementById('detail-description').innerText = tempDiv.textContent || tempDiv.innerText || 'No description available.';
|
||||
|
||||
@@ -10,8 +10,6 @@ const nextBtn = document.getElementById('next-chapter');
|
||||
const lnSettings = document.getElementById('ln-settings');
|
||||
const mangaSettings = document.getElementById('manga-settings');
|
||||
|
||||
const hasQuery = window.location.search.length > 0;
|
||||
|
||||
const config = {
|
||||
ln: {
|
||||
fontSize: 18,
|
||||
@@ -145,6 +143,18 @@ async function loadChapter() {
|
||||
document.title = `Chapter ${chapter}`;
|
||||
}
|
||||
|
||||
const res2 = await fetch(`/api/book/${bookId}?source=${source}`);
|
||||
const data2 = await res2.json();
|
||||
|
||||
fetch("/api/rpc", {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({
|
||||
details: data2.title.romaji ?? data2.title,
|
||||
state: `Chapter ${data.title}`,
|
||||
mode: "reading"
|
||||
})
|
||||
});
|
||||
if (data.error) {
|
||||
reader.innerHTML = `
|
||||
<div class="loading-container">
|
||||
@@ -476,7 +486,16 @@ nextBtn.addEventListener('click', () => {
|
||||
|
||||
function updateURL(newChapter) {
|
||||
chapter = newChapter;
|
||||
const newUrl = `/read/${provider}/${chapter}/${bookId}`;
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
let source = urlParams.get('source');
|
||||
|
||||
let src;
|
||||
if (source === 'anilist') {
|
||||
src= "?source=anilist"
|
||||
} else {
|
||||
src= `?source=${source}`
|
||||
}
|
||||
const newUrl = `/read/${provider}/${chapter}/${bookId}${src}`;
|
||||
window.history.pushState({}, '', newUrl);
|
||||
}
|
||||
|
||||
|
||||
9
src/scripts/rpc-inapp.js
Normal file
9
src/scripts/rpc-inapp.js
Normal file
@@ -0,0 +1,9 @@
|
||||
fetch("/api/rpc", {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({
|
||||
details: "Browsing",
|
||||
state: `In App`,
|
||||
mode: "idle"
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user