Skip to content

Commit

Permalink
fix: some changes for better compatibility with DiscordGameSDK
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsontheWolf committed Dec 10, 2023
1 parent 56d7dba commit 549d944
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 43 deletions.
11 changes: 11 additions & 0 deletions daemon/ClientHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ const defaultDispatch = {
cdn_host: 'cdn.discordapp.com',
api_endpoint: '//discord.com/api',
environment: 'production'
},
user: {
id: '0',
username: 'anon',
discriminator: '0',
global_name: 'Anon',
avatar: null,
avatar_decoration_data: null,
bot: false,
flags: 0,
premium_type: 0,
}
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/ProxyHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ProxyHandler extends EventEmitter {
clearInterval(this.#interval);
this.emit('close');
}
if(this.#clientUp) {
if (this.#clientUp) {
this.#client.destroy();
this.#clientUp = false;
}
Expand Down
16 changes: 15 additions & 1 deletion daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const config = getConfig();
const activityCache = new Map();

const newHandler = (socket) => {
if(config.passthrough) return new ProxyHandler(socket, config.passthrough);
if (config.passthrough) return new ProxyHandler(socket, config.passthrough);
return new ClientHandler(socket);
};

Expand Down Expand Up @@ -69,6 +69,20 @@ server.on("connection", (socket) => {
if (data.cmd === 'SET_ACTIVITY') {
updateActivity(handler, data.args);
}
if (config.passthrough) return;
// Some clients (cough cough DiscordGameSDK) don't send activity data
// unless i tell them I'm inform them of different events.
// This is a workaround for that.
if (data.cmd === 'SUBSCRIBE') {
const nonce = data.nonce;
const evt = data.evt;
handler.send({
cmd: 'SUBSCRIBE',
data: { evt },
evt: null,
nonce,
});
}
});

});
Expand Down
8 changes: 4 additions & 4 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"main": "src/index.js",
"license": "MIT",
"dependencies": {
"@koa/router": "^12.0.0",
"enmap": "^5.9.5",
"koa": "^2.14.1",
"koa-bodyparser": "^4.4.0"
"@koa/router": "^12.0.1",
"enmap": "^5.9.8",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.1"
},
"type": "module"
}
8 changes: 5 additions & 3 deletions server/src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ const handleDuration = (duration) => {

const calcDuration = (time, type) => {
if (!time) return null;
const now = Date.now();
if (type === 'start') return Math.max(Math.floor((now - time) / 1000), 0);
if (type === 'end') return Math.max(Math.floor((time - now) / 1000), 0);
// Apparently this can be a UNIX timestamp or a JS timestamp
if(`${time}`.length > 10) time = Math.floor(time / 1000);
const now = Math.floor(Date.now() / 1000);
if (type === 'start') return Math.max(now - time, 0);
if (type === 'end') return Math.max(time - now, 0);
}

class ActivityView extends HTMLElement {
Expand Down
66 changes: 34 additions & 32 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# yarn lockfile v1


"@koa/router@^12.0.0":
version "12.0.0"
resolved "https://registry.yarnpkg.com/@koa/router/-/router-12.0.0.tgz#2ae7937093fd392761c0e5833c368379d4a35737"
integrity sha512-cnnxeKHXlt7XARJptflGURdJaO+ITpNkOHmQu7NHmCoRinPbyvFzce/EG/E8Zy81yQ1W9MoSdtklc3nyaDReUw==
"@koa/router@^12.0.1":
version "12.0.1"
resolved "https://registry.yarnpkg.com/@koa/router/-/router-12.0.1.tgz#1a66f92a630c02832cf5bbf0db06c9e53e423468"
integrity sha512-ribfPYfHb+Uw3b27Eiw6NPqjhIhTpVFzEWLwyc/1Xp+DCdwRRyIlAUODX+9bPARF6aQtUu1+/PHzdNvRzcs/+Q==
dependencies:
debug "^4.3.4"
http-errors "^2.0.0"
koa-compose "^4.1.0"
methods "^1.1.2"
Expand All @@ -25,13 +26,13 @@ base64-js@^1.3.1:
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

better-sqlite3@^7.5.1:
version "7.6.2"
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-7.6.2.tgz#47cd8cad5b9573cace535f950ac321166bc31384"
integrity sha512-S5zIU1Hink2AH4xPsN0W43T1/AJ5jrPh7Oy07ocuW/AKYYY02GWzz9NH0nbSMn/gw6fDZ5jZ1QsHt1BXAwJ6Lg==
better-sqlite3@^8.4.0:
version "8.7.0"
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-8.7.0.tgz#bcc341856187b1d110a8a47234fa89c48c8ef538"
integrity sha512-99jZU4le+f3G6aIl6PmmV0cxUIWqKieHxsiF7G34CVFiE+/UabpYqkU0NJIkY/96mQKikHeBjtR27vFfs5JpEw==
dependencies:
bindings "^1.5.0"
prebuild-install "^7.1.0"
prebuild-install "^7.1.1"

bindings@^1.5.0:
version "1.5.0"
Expand Down Expand Up @@ -123,7 +124,7 @@ copy-to@^2.0.1:
resolved "https://registry.yarnpkg.com/copy-to/-/copy-to-2.0.1.tgz#2680fbb8068a48d08656b6098092bdafc906f4a5"
integrity sha512-3DdaFaU/Zf1AnpLiFDeNCD4TOWe3Zl2RZaTzUvWiIk5ERzcCodOE20Vqq4fzCbNoHURFHT4/us/Lfq+S2zyY4w==

debug@^4.3.2:
debug@^4.3.2, debug@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
Expand Down Expand Up @@ -189,15 +190,15 @@ end-of-stream@^1.1.0, end-of-stream@^1.4.1:
dependencies:
once "^1.4.0"

enmap@^5.9.5:
version "5.9.5"
resolved "https://registry.yarnpkg.com/enmap/-/enmap-5.9.5.tgz#4a4deab23286ec56a9337eb5d96c696bd6a31702"
integrity sha512-ooaXd+PWEHCo8FBaBzHAkj4RP1qfHfd0S8c0X87q9QXhE6yUfVznFJyEP3m54HlCSkPrIArFlxmPcGj/uUG4hw==
enmap@^5.9.8:
version "5.9.8"
resolved "https://registry.yarnpkg.com/enmap/-/enmap-5.9.8.tgz#ed593553cba62d62672671e9cc14fac5fd0275b0"
integrity sha512-6hLGXpZ3KjiR35JkmwdHSM316TTznSl9QslsS4p7DNZNoKDGqmKnyIiP8pcKNk8BnNgcUBmPOic5siHWqCaIVA==
dependencies:
better-sqlite3 "^7.5.1"
better-sqlite3 "^8.4.0"
lodash "^4.17.21"
on-change "^2.2.3"
serialize-javascript "^6.0.0"
on-change "^3.0.2"
serialize-javascript "^6.0.1"

escape-html@^1.0.3:
version "1.0.3"
Expand Down Expand Up @@ -333,13 +334,14 @@ keygrip@~1.1.0:
dependencies:
tsscmp "1.0.6"

koa-bodyparser@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/koa-bodyparser/-/koa-bodyparser-4.4.0.tgz#2271cd7d603a08c47a47e82d2c00630379672e6c"
integrity sha512-AXPY7wwKZUmbgb8VkTEUFoRNOlx6aWRJwEnQD+zfNf33/7KSAkN4Oo9BqlIk80D+5TvuqlhpQT5dPVcyxl5Zsw==
koa-bodyparser@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/koa-bodyparser/-/koa-bodyparser-4.4.1.tgz#a908d848e142cc57d9eece478e932bf00dce3029"
integrity sha512-kBH3IYPMb+iAXnrxIhXnW+gXV8OTzCu8VPDqvcDHW9SQrbkHmqPQtiZwrltNmSq6/lpipHnT7k7PsjlVD7kK0w==
dependencies:
co-body "^6.0.0"
copy-to "^2.0.1"
type-is "^1.6.18"

koa-compose@^4.1.0:
version "4.1.0"
Expand All @@ -354,10 +356,10 @@ koa-convert@^2.0.0:
co "^4.6.0"
koa-compose "^4.1.0"

koa@^2.14.1:
version "2.14.1"
resolved "https://registry.yarnpkg.com/koa/-/koa-2.14.1.tgz#defb9589297d8eb1859936e777f3feecfc26925c"
integrity sha512-USJFyZgi2l0wDgqkfD27gL4YGno7TfUkcmOe6UOLFOVuN+J7FwnNu4Dydl4CUQzraM1lBAiGed0M9OVJoT0Kqw==
koa@^2.14.2:
version "2.14.2"
resolved "https://registry.yarnpkg.com/koa/-/koa-2.14.2.tgz#a57f925c03931c2b4d94b19d2ebf76d3244863fc"
integrity sha512-VFI2bpJaodz6P7x2uyLiX6RLYpZmOJqNmoCst/Yyd7hQlszyPwG/I9CQJ63nOtKSxpt5M7NH67V6nJL2BwCl7g==
dependencies:
accepts "^1.3.5"
cache-content-type "^1.0.0"
Expand Down Expand Up @@ -459,10 +461,10 @@ object-inspect@^1.9.0:
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.12.3.tgz#ba62dffd67ee256c8c086dfae69e016cd1f198b9"
integrity sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==

on-change@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/on-change/-/on-change-2.2.3.tgz#256d40d890e511151766e6dfc44b27953ccdafb8"
integrity sha512-yx48YQW3XsMHYWJ5n8oOgonrxsIJJNn1fqE3QlQpYS/I6XHvzTARHzaVbwFyJoSaZ4g7UTZheaaxHVtFKcNXgg==
on-change@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/on-change/-/on-change-3.0.2.tgz#e40eb57b455e71ad3144aab06337d91e91d4a7d6"
integrity sha512-rdt5YfIfo86aFNwvQqzzHMpaPPyVQ/XjcGK01d46chZh47G8Xzvoao79SgFb03GZfxRGREzNQVJuo31drqyIlA==

on-finished@^2.3.0:
version "2.4.1"
Expand Down Expand Up @@ -493,7 +495,7 @@ path-to-regexp@^6.2.1:
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.2.1.tgz#d54934d6798eb9e5ef14e7af7962c945906918e5"
integrity sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==

prebuild-install@^7.1.0:
prebuild-install@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.1.1.tgz#de97d5b34a70a0c81334fd24641f2a1702352e45"
integrity sha512-jAXscXWMcCK8GgCoHOfIr0ODh5ai8mj63L2nWrjuAgXE6tDyYGnx4/8o/rCgU+B4JSyZBKbeZqzhtwtC3ovxjw==
Expand Down Expand Up @@ -579,7 +581,7 @@ semver@^7.3.5:
dependencies:
lru-cache "^6.0.0"

serialize-javascript@^6.0.0:
serialize-javascript@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-6.0.1.tgz#b206efb27c3da0b0ab6b52f48d170b7996458e5c"
integrity sha512-owoXEFjWRllis8/M1Q+Cw5k8ZH40e3zhp/ovX+Xr/vi1qj6QesbyXXViFbpNvWvPNAD62SutwEXavefrLJWj7w==
Expand Down Expand Up @@ -674,7 +676,7 @@ tunnel-agent@^0.6.0:
dependencies:
safe-buffer "^5.0.1"

type-is@^1.6.16:
type-is@^1.6.16, type-is@^1.6.18:
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==
Expand Down
4 changes: 2 additions & 2 deletions shared/Activity.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Activity {
if (!['object', 'undefined'].includes(typeof this.party)) throw new TypeError('Activity party must be an object');
if (!['string', 'undefined'].includes(typeof this.party?.id)) throw new TypeError('Activity party id must be a string');
if (!['undefined'].includes(typeof this.party?.size) && (!Array.isArray(this.party?.size) || this.party?.size !== 2)) throw new TypeError('Activity party must be an array of length 2');
if (!['number', 'undefined'].includes(typeof this.party?.size[0])) throw new TypeError('Activity party size[0] must be a number');
if (!['number', 'undefined'].includes(typeof this.party?.size[1])) throw new TypeError('Activity party size[1] must be a number');
if (!['number', 'undefined'].includes(typeof this.party?.size?.[0])) throw new TypeError('Activity party size[0] must be a number');
if (!['number', 'undefined'].includes(typeof this.party?.size?.[1])) throw new TypeError('Activity party size[1] must be a number');
if (!['boolean', 'undefined'].includes(typeof this.instance)) throw new TypeError('Activity instance must be a boolean');
if (!['undefined'].includes(typeof this.buttons) && !Array.isArray(this.buttons)) throw new TypeError('Activity buttons must be an array');
if (this.buttons?.some(b => b?.label === undefined || b?.url === undefined)) throw new TypeError('Activity buttons must have a label and a url');
Expand Down
14 changes: 14 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="http://localhost:3000/embed/script.js"></script>
</head>
<body>
<activity-view bg-dark-color="#261e2f" bg-light-color="#433553"></activity-view>

<activity-view base-url="https://activity.shorty.systems/" bg-dark-color="#261e2f" bg-light-color="#433553"></activity-view>
</body>
</html>

0 comments on commit 549d944

Please sign in to comment.