Skip to content

Commit a19f9eb

Browse files
authored
New: Support Discord Rich Presence
1 parent 392c0f9 commit a19f9eb

13 files changed

Lines changed: 246 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1212
- Improved rotation and support for rotating in PDF [`cb90771`](https://github.com/ollm/OpenComic/commit/cb907714ca7d713bcbf3471564e48a3e67c8ca40)
1313
- PDFs are rendered to a blob image instead of a canvas [`554c1ef`](https://github.com/ollm/OpenComic/commit/554c1ef88adf358acae14f9bf28f0e1f4f626d8f)
1414
- Optimized sidebar thumbnails [`02b67c6`](https://github.com/ollm/OpenComic/commit/02b67c620b6b13810c778beca1052a9b28829548)
15+
- Support Discord Rich Presence
1516

1617
##### 🐛 Bug Fixes
1718

‎languages/ca.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@
255255
"mouseWheelSensitivityInZoom": "Sensibilitat de la roda del ratolí en fer zoom",
256256
"rotateHorizontalsAnticlockwise": "Al rotar imatges horitzontals fer-ho en sentit antihorari",
257257
"startReadingInFullScreen": "Començar la lectura a pantalla completa",
258-
"trackingAtTheEnd": "Seguiment al final del capítol/tom"
258+
"trackingAtTheEnd": "Seguiment al final del capítol/tom",
259+
"discordRcp": "Discord Rich Presence"
259260
},
260261
"imageInterpolation": {
261262
"main": "Interpolació d'imatge",

‎languages/en.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@
255255
"mouseWheelSensitivityInZoom": "Mouse wheel sensitivity when zooming",
256256
"rotateHorizontalsAnticlockwise": "When rotating horizontal images, do so counterclockwise",
257257
"startReadingInFullScreen": "Start reading in full screen",
258-
"trackingAtTheEnd": "Tracking at the end of the chapter/volume"
258+
"trackingAtTheEnd": "Tracking at the end of the chapter/volume",
259+
"discordRcp": "Discord Rich Presence"
259260
},
260261
"imageInterpolation": {
261262
"main": "Image interpolation",

‎languages/es.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@
255255
"mouseWheelSensitivityInZoom": "Sensibilidad de la rueda del ratón al hacer zoom",
256256
"rotateHorizontalsAnticlockwise": "Al rotar imágenes horizontales hacerlo en sentido antihorario",
257257
"startReadingInFullScreen": "Empezar la lectura en pantalla completa",
258-
"trackingAtTheEnd": "Seguimiento al final del capítulo/tomo"
258+
"trackingAtTheEnd": "Seguimiento al final del capítulo/tomo",
259+
"discordRcp": "Discord Rich Presence"
259260
},
260261
"imageInterpolation": {
261262
"main": "Interpolación de imagen",

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"basic-ftp": "^5.0.5",
8686
"bezier-js": "^6.1.4",
8787
"detect-libc": "^2.0.4",
88+
"discord-rpc": "^4.0.1",
8889
"electron-json-storage": "^4.6.0",
8990
"electron-window-state": "^5.0.3",
9091
"epubjs": "^0.3.93",

‎scripts/dom.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ async function loadIndexPage(animation = true, path = false, content = false, ke
10401040

10411041
shortcuts.register(isOpds || _indexLabel.opds ? 'opds' : 'browse');
10421042
gamepad.updateBrowsableItems(path ? sha1(path) : 'library');
1043+
reading.discord.update();
10431044
}
10441045

10451046
function loadIndexContentLeft(animation)
@@ -2742,6 +2743,7 @@ async function openComic(animation = true, path = true, mainPath = true, end = f
27422743
reading.onLoad(function(){
27432744

27442745
cache.resumeQueue();
2746+
reading.discord.update();
27452747

27462748
});
27472749

‎scripts/reading.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const render = require(p.join(appDir, 'scripts/reading/render.js')),
44
contextMenu = require(p.join(appDir, 'scripts/reading/context-menu.js')),
55
pageTransitions = require(p.join(appDir, 'scripts/reading/page-transitions.js')),
66
readingEbook = require(p.join(appDir, 'scripts/reading/ebook.js')),
7-
sidebar = require(p.join(appDir, 'scripts/reading/sidebar.js'));
7+
sidebar = require(p.join(appDir, 'scripts/reading/sidebar.js')),
8+
discord = require(p.join(appDir, 'scripts/reading/discord.js'));
89

9-
var images = {}, imagesData = {}, imagesDataClip = {}, imagesPath = {}, imagesNum = 0, contentNum = 0, imagesNumLoad = 0, currentIndex = 1, imagesPosition = {}, imagesFullPosition = {}, prevImagesFullPosition = {}, foldersPosition = {}, indexNum = 0, imagesDistribution = [], currentPageXY = {x: 0, y: 0}, currentMousePosition = {pageX: 0, pageY: 0};
10+
var images = {}, imagesData = {}, imagesDataClip = {}, imagesPath = {}, imagesNum = 0, contentNum = 0, imagesNumLoad = 0, currentIndex = 1, imagesPosition = {}, imagesFullPosition = {}, prevImagesFullPosition = {}, foldersPosition = {}, indexNum = 0, imagesDistribution = [], currentPageXY = {x: 0, y: 0}, currentMousePosition = {pageX: 0, pageY: 0}, currentPage = 0;
1011

1112
//Calculates whether to add a blank image (If the reading is in double page and do not apply to the horizontals)
1213
function blankPage(index)
@@ -1009,6 +1010,9 @@ function goToImageCL(index, animation = true, fromScroll = false, fromPageRange
10091010
{
10101011
let input = contentLeft.querySelector('.simple-slider input');
10111012
if(input) events.goRange(input, index, false);
1013+
1014+
currentPage = index;
1015+
reading.discord.updateThrottle();
10121016
}
10131017

10141018
const readingLeft = contentLeft.querySelector('.reading-left-images');
@@ -4600,6 +4604,7 @@ async function generateEbookPages(end = false, reset = false, fast = false, imag
46004604
handlebarsContext.ebookLandmarks = ebookPages.landmarks;
46014605
handlebarsContext.ebookToc = ebookPages.toc;
46024606
handlebarsContext.ebookPages = imagesNum;
4607+
46034608
template.loadContentLeft('reading.content.left.ebook.html', true);
46044609
template._contentLeft().firstElementChild.style.height = 'calc(100% - 66px)';
46054610
events.eventRange();
@@ -5737,7 +5742,7 @@ module.exports = {
57375742
read: read,
57385743
reload: reload,
57395744
images: function(){return images},
5740-
imagesNum: imagesNum,
5745+
imagesNum: function(){return imagesNum},
57415746
indexNum: function(){return indexNum},
57425747
contentNum: function(){return contentNum},
57435748
imagesNumLoad: imagesNumLoad,
@@ -5800,6 +5805,8 @@ module.exports = {
58005805
currentImagePage: currentImagePage,
58015806
currentImageIndex: currentImageIndex,
58025807
currentPageVisibility: function(){return currentPageVisibility},
5808+
totalPages: function(){return imagesNum},
5809+
currentPage: function(){return currentPage},
58035810
loadBookmarks: loadBookmarks,
58045811
loadTrackigSites: loadTrackigSites,
58055812
loadReadingPages: loadReadingPages,
@@ -5851,4 +5858,5 @@ module.exports = {
58515858
pageTransitions: pageTransitions,
58525859
render: render,
58535860
sidebar: sidebar,
5861+
discord: discord,
58545862
};

‎scripts/reading/discrod.js‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
const rpc = require('discord-rpc');
2+
const clientId = '1387113291838525571';
3+
4+
var discord = {
5+
logged: false,
6+
client: false,
7+
promise: false,
8+
};
9+
10+
function reset()
11+
{
12+
discord = {
13+
logged: false,
14+
client: false,
15+
promise: false,
16+
};
17+
}
18+
19+
async function login()
20+
{
21+
if(discord.promise)
22+
return discord.promise;
23+
24+
start();
25+
26+
return discord.promise = new Promise(function(resolve, reject) {
27+
28+
discord.client = new rpc.Client({transport: 'ipc'});
29+
30+
discord.client.on('ready', function() {
31+
32+
console.log('Discord RPC ready');
33+
34+
discord.logged = true;
35+
resolve();
36+
37+
});
38+
39+
discord.client.login({
40+
clientId: '1387113291838525571',
41+
}).catch(function(error){
42+
43+
reset();
44+
45+
console.log(error);
46+
reject(error);
47+
48+
});
49+
50+
});
51+
52+
}
53+
54+
function logout()
55+
{
56+
if(!discord.client || !discord.logged)
57+
return;
58+
59+
discord.client.clearActivity();
60+
//discord.client.destroy();
61+
//reset();
62+
}
63+
64+
var times = {}, currentTime = {}, cache = {};
65+
66+
function getState(data)
67+
{
68+
const chapter = data.chapter = data.chapter ?? tracking.getChapter();
69+
const volume = data.volume = data.volume ?? tracking.getVolume();
70+
71+
let state = (chapter !== false ? language.reading.tracking.chapter+' '+chapter : '')+(volume !== false ? (chapter !== false ? ' · ' : '')+language.reading.tracking.volume+' '+volume : '');
72+
73+
const percent = Math.round(reading.currentPage() / reading.totalPages() * 100);
74+
const pages = language.global.pageAndNumber.replace(/\$1/, reading.currentPage()+' / '+reading.totalPages());
75+
76+
if(!state)
77+
state = pages+' · '+percent+'%';
78+
else
79+
state += ' · '+pages+' · '+percent+'%';
80+
81+
return state;
82+
}
83+
84+
var status = false;
85+
86+
async function update(focused = true)
87+
{
88+
if(!config.readingDiscordRcp)
89+
return;
90+
91+
await login();
92+
93+
if(!discord.client || !discord.logged)
94+
return;
95+
96+
const mainPath = onReading && focused ? dom.history.mainPath : false;
97+
const path = onReading && focused ? dom.history.path : false;
98+
const now = Date.now();
99+
100+
const time = times[mainPath] ?? {
101+
elapsed: 0,
102+
current: 0,
103+
};
104+
105+
const data = cache[path] ?? {};
106+
107+
const title = data.title = data.title ?? (onReading ? tracking.getTitle(true) : language.global.library);
108+
const state = onReading ? getState(data)+' ' : ' ';
109+
110+
if(focused)
111+
{
112+
status = {
113+
details: title,
114+
state: state,
115+
startTimestamp: new Date(now - time.elapsed),
116+
largeImageKey: 'opencomic',
117+
largeImageText: 'OpenComic',
118+
instance: false,
119+
};
120+
121+
discord.client.setActivity(status);
122+
}
123+
else
124+
{
125+
status = false;
126+
discord.client.clearActivity();
127+
}
128+
129+
currentTime.elapsed += now - currentTime.current;
130+
131+
time.current = now;
132+
currentTime = time;
133+
134+
times[mainPath] = time;
135+
}
136+
137+
function updateThrottle()
138+
{
139+
app.setThrottle('discord', update, 2000, 5000);
140+
}
141+
142+
var focused = true;
143+
144+
function events()
145+
{
146+
const currentWindow = electronRemote.getCurrentWindow();
147+
148+
currentWindow.on('focus', function() {
149+
150+
focused = true;
151+
update(true);
152+
153+
});
154+
155+
currentWindow.on('blur', function() {
156+
157+
focused = false;
158+
update(false);
159+
160+
});
161+
}
162+
163+
function interval()
164+
{
165+
if(!config.readingDiscordRcp)
166+
return;
167+
168+
if(!discord.client || !discord.logged || !onReading || !focused)
169+
return;
170+
171+
update();
172+
}
173+
174+
var started = false;
175+
176+
function start()
177+
{
178+
if(started)
179+
return;
180+
181+
events();
182+
// setInterval(interval, 30000); // 30 seconds
183+
184+
started = true;
185+
}
186+
187+
async function set(value)
188+
{
189+
await app.sleep(50);
190+
191+
if(value)
192+
update();
193+
else
194+
logout();
195+
}
196+
197+
module.exports = {
198+
update,
199+
updateThrottle,
200+
get client() {return discord.client},
201+
get status() {return status},
202+
set,
203+
};

‎scripts/reading/sidebar.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ async function goToImage(index)
117117
const thumbnail = thumbnails[image.sha];
118118

119119
const img = contentLeft.querySelector('.sha-image-'+image.sha);
120+
if(!img) continue;
120121

121122
if(thumbnail.cache)
122123
{
@@ -177,4 +178,4 @@ module.exports = {
177178
sizes,
178179
goToImage,
179180
disableEvent,
180-
};
181+
};

‎scripts/settings.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,12 @@ function set(key, value, save = true)
14691469

14701470
dom.queryAll('.settings-body .settings-tap-zones, .settings-body .settings-invert-tap-zones-in-manga').class(value, 'disable-pointer');
14711471

1472+
break;
1473+
1474+
case 'readingDiscordRcp':
1475+
1476+
reading.discord.set(value);
1477+
14721478
break;
14731479
}
14741480

0 commit comments

Comments
 (0)