-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
85 lines (72 loc) · 2.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const clientId = '536162780303196161';
const RPC = require('discord-rpc');
const activeWin = require('active-win');
const client = new RPC.Client({ transport: 'ipc' });
const browsers = ["chrome", "firefox", "opera", "iexplore", "safari"];
var video = null;
var artist = null;
function getOccurrence(array, value) {
return array.filter((v) => (v === value)).length;
}
async function youtube() {
let window = activeWin.sync();
try {
let appExtension = isWin() ? ".exe" : ".app";
let processName = window.owner.name;
let windowTitle = window.title;
if (browsers.includes(processName.toLowerCase().split('.')[0])) {
if (windowTitle.includes('YouTube')) {
let title = windowTitle.split('YouTube');
if (title[0] !== video) {
video = title[0].slice(0,-2);
var splitEachChar = video.split('');
var count = getOccurrence(splitEachChar,'-'); // prevent it from splittin to much, certain youtubers loves to put "-" in title
if(count <= 1){
artist = video.split("-")[0]; //for music channels get artist
video = video.split("-")[1]; //and song name
}
if(video == null) {
video = artist;
artist = null;
}
updateRP(video,artist);
}
} else {
if (video !== "Idling") {
video = 'Idling';
updateRP(video,null);
}
}
}
} catch(e) {
console.log(e);
}
}
function updateRP(status,artist) {
if (status == null || status == "" || !status) {
status = "Idling";
return;
}
if (artist == null) artist = "Watching";
client.setActivity({
details: artist,
state: status,
largeImageKey: 'logo',
largeImageText: 'Youtube', //todo : make it show thumbnails & maybe a timer
instance: false
}).catch(err => {
console.log(err);
});
console.log(`Status Updated: ${status}`);
}
function isWin() {
if (process.platform == "win32") return true;
return false;
}
client.on('ready', () => {
console.log("Connected to Discord!");
setInterval(() => {
youtube();
}, 10000);
});
client.login({clientId}).catch(console.error);