-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
167 lines (144 loc) · 4.78 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* File: index.js
* Project: discord-rpc-client
* File Created: Sunday, 2nd July 2023 1:46:25 pm
* Author: Tee ([email protected])
* Github: https://github.com/teeteeteeteetee
* Discord: Tee#0001
*
* Last Modified: Saturday, 25th November 2023 3:36:16 pm
* Modified By: Tee ([email protected])
*
* Copyright (c) 2023 Tee, Demon Cat
*/
"use strict";
import RichPresence from "./rpc";
import isEqual from "lodash/isEqual";
import clone from "lodash/clone";
import { extensionConfigurationTemplate, getConfiguration, getConfigurations, resetConfiguration, rpcConfigurationTemplate, setConfiguration } from "./localstorage";
const csInterface = new CSInterface();
const client = require("./client.js")[csInterface.getApplicationID()];
const rpc = new RichPresence(client);
const activity = new CSEvent("com.tee.rpc.activity", "APPLICATION");
const user = new CSEvent("com.tee.rpc.user", "APPLICATION");
const config = new CSEvent("com.tee.rpc.config", "APPLICATION");
const logger = new CSEvent("com.tee.rpc.logger", "APPLICATION");
const props = {
state: undefined,
details: undefined,
startTimestamp: new Date(),
largeImageKey: 'logo',
largeImageText: undefined,
smallImageKey: undefined,
smallImageText: undefined,
partySize: 0,
partyMax: 0,
}
let presence = {}
let configurations = getConfigurations()
let status = true;
let interval = 1000;
// do not initialize if its ran through dynamic link (after effects)
if (csInterface.getApplicationID() === "AEFT") {
let isDynamicLink = false
csInterface.evalScript("app.activeViewer", (x) => {
// set dynamic link icon
if (x === "null")
isDynamicLink = true
return;
});
if (isDynamicLink)
// TODO: handle dunamic link
throw new Error("Started as dynamic link");
}
csInterface.addEventListener('com.tee.rpc.reset', () => {
console.log("reset")
rpc.destroy()
resetConfiguration()
window.location.reload()
})
csInterface.addEventListener('com.tee.rpc.config', (e) => {
console.log(e.data)
for (const [key, value] of Object.entries(e.data)){
console.log(key)
setConfiguration(key, value)
}
configurations = getConfigurations()
presence = {}
})
if (!configurations.rpc) {
setConfiguration("rpc", rpcConfigurationTemplate);
configurations.rpc = getConfiguration("rpc");
}
if (!configurations.extension) {
setConfiguration("extension", extensionConfigurationTemplate);
configurations.extension = getConfiguration("extension");
}
csInterface.dispatchEvent(configurations)
rpc.login()
.then(() => {
csInterface.addEventListener('com.tee.rpc.update', (e) => {
presence = {}
if (rpc.getStatus()) {
user.data = rpc.getUser()
console.log(rpc.getUser())
csInterface.dispatchEvent(user)
}
console.log(configurations)
csInterface.dispatchEvent(configurations)
})
})
.then(() => main())
.catch(console.error)
function main() {
try {
if (rpc.getStatus()) {
if (!status) {
presence = {}
status = true
}
callScript('state()')
callScript('details()')
callScript('smallImageKey()')
callScript('smallImageText()')
// callScript('largeImageKey()')
callScript('largeImageText()')
callScriptNumber('partySize()')
callScriptNumber('partyMax()')
props.largeImageKey = 'logo'
console.log(props)
if (!isEqual(presence, props)) {
rpc.setActivity(props)
presence = clone(props)
activity.data = {
...presence,
name: client.name
}
csInterface.dispatchEvent(activity)
}
} else {
status = false;
}
} catch (err) {
console.error(err);
}
setTimeout(main, interval);
function callScript(func) {
if (configurations["rpc"][func.replace('()', '')]["enabled"]) {
csInterface.evalScript(func, (e) => {
props[func.replace('()', '')] = e
})
} else {
props[func.replace('()', '')] = undefined
}
}
function callScriptNumber(func) {
if (configurations["rpc"][func.replace('()', '')]["enabled"]) {
csInterface.evalScript(func, (e) => {
props[func.replace('()', '')] = parseInt(e)
})
}else{
props[func.replace('()', '')] = 0
}
}
}