From 24f6af35a105bfd8338df8ede13c59787fe5bf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Ku=C5=BEela?= Date: Mon, 26 Feb 2024 19:02:31 +0100 Subject: [PATCH] Added Discord Rich Presence for The Launcher --- main.js | 66 ++++++++++++++++++++++++++++- package-lock.json | 104 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 9 ++-- 3 files changed, 169 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index 42b14ab..04431a2 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,13 @@ const { app, BrowserWindow, shell } = require('electron'); require('@electron/remote/main').initialize() +const DiscordRPC = require('discord-rpc'); +const path = require('node:path'); +const fs = require('fs'); +let mainWindow; const createWindow = () => { - const mainWindow = new BrowserWindow({ + mainWindow = new BrowserWindow({ minWidth: 400, minHeight: 300, height: 600, @@ -28,7 +32,7 @@ const createWindow = () => { return { action: 'deny' }; // Prevent opening a new window in Electron }); } -app.whenReady().then(() => {-7 +app.whenReady().then(() => { createWindow(); app.on('activate', function () { @@ -39,3 +43,61 @@ app.whenReady().then(() => {-7 app.on('window-all-closed', function () { if (process.platform !== 'darwin') app.quit(); }); + +var version, clientID = "1211721853324890143", rpc = new DiscordRPC.Client({ transport: 'ipc' }), startTime = new Date(); +fs.readFile(path.join(__dirname, 'package.json'), 'utf8', (err, data) => { + if (err) { + console.error(err); + return; + } + const JSONData = JSON.parse(data) + // handle the json data here + console.log(JSONData.version); + version = JSONData.version; +}); + +/*function setRPC() { + console.log("setting activity"); + rpc.setActivity({ + details: `running version v0.0.0-RPC`, + startTimestamp: startTime, + largeImageKey: 'deadcodelogo', + largeImageText: 'made by deadcode', + }); +} + +rpc.on('ready', () => { + setRPC(); +}); + +rpc.login({ clientID }).catch(console.error);*/ + + + +async function setActivity() { + if (!rpc || !mainWindow) { + return; + } + + // You'll need to have snek_large and snek_small assets uploaded to + // https://discord.com/developers/applications//rich-presence/assets + rpc.setActivity({ + details: `the launcher by deadcode.`, + state: `running version ${version}.`, + startTimestamp: startTime, + largeImageKey: 'deadcodelogo', + largeImageText: 'made by deadcode', + instance: false, + }); +} + +rpc.on('ready', () => { + setActivity(); + + // activity can only be set every 15 seconds + setInterval(() => { + setActivity(); + }, 15e3); +}); + +rpc.login({ clientId: clientID }).catch(console.error); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7037d09..4de91ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "deadforge", - "version": "v0.0.0Fix", + "version": "v0.0.0RPC", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "deadforge", - "version": "v0.0.0Fix", + "version": "v0.0.0RPC", "license": "BSD-3-Clause", "dependencies": { - "@electron/remote": "^2.1.2" + "@electron/remote": "^2.1.2", + "discord-rpc": "^4.0.1" }, "devDependencies": { "electron": "^29.0.1", @@ -847,6 +848,15 @@ } ] }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "optional": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -1375,6 +1385,18 @@ "node": "*" } }, + "node_modules/discord-rpc": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/discord-rpc/-/discord-rpc-4.0.1.tgz", + "integrity": "sha512-HOvHpbq5STRZJjQIBzwoKnQ0jHplbEWFWlPDwXXKm/bILh4nzjcg7mNqll0UY7RsjFoaXA7e/oYb/4lvpda2zA==", + "dependencies": { + "node-fetch": "^2.6.1", + "ws": "^7.3.1" + }, + "optionalDependencies": { + "register-scheme": "github:devsnek/node-register-scheme" + } + }, "node_modules/dmg-builder": { "version": "24.12.0", "resolved": "https://registry.npmjs.org/dmg-builder/-/dmg-builder-24.12.0.tgz", @@ -1742,6 +1764,12 @@ "pend": "~1.2.0" } }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "optional": true + }, "node_modules/filelist": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/filelist/-/filelist-1.0.4.tgz", @@ -2499,9 +2527,27 @@ "version": "1.7.2", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-1.7.2.tgz", "integrity": "sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg==", - "dev": true, "optional": true }, + "node_modules/node-fetch": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", + "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, "node_modules/normalize-url": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-6.1.0.tgz", @@ -2667,6 +2713,17 @@ "node": ">=12.0.0" } }, + "node_modules/register-scheme": { + "version": "0.0.2", + "resolved": "git+ssh://git@github.com/devsnek/node-register-scheme.git#e7cc9a63a1f512565da44cb57316d9fb10750e17", + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "dependencies": { + "bindings": "^1.3.0", + "node-addon-api": "^1.3.0" + } + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -3063,6 +3120,11 @@ "tmp": "^0.2.0" } }, + "node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" + }, "node_modules/truncate-utf8-bytes": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz", @@ -3140,6 +3202,20 @@ "node": ">=0.6.0" } }, + "node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" + }, + "node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -3195,6 +3271,26 @@ "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, + "node_modules/ws": { + "version": "7.5.9", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.5.9.tgz", + "integrity": "sha512-F+P9Jil7UiSKSkppIiD94dN07AwvFixvLIj1Og1Rl9GGMuNipJnV9JzjD6XuqmAeiswGvUmNLjr5cFuXwNS77Q==", + "engines": { + "node": ">=8.3.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xmlbuilder": { "version": "15.1.1", "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.1.1.tgz", diff --git a/package.json b/package.json index 1e0b312..21ee9d4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "deadforge", - "version": "v0.0.0Fix", + "version": "v0.0.0RPC", "description": "An Electron Launcher for DeadCode Projects.", "main": "main.js", "scripts": { @@ -18,8 +18,8 @@ "appId": "dev.deadcode.forge", "productName": "DeadForge", "win": { - "target": "nsis", - "artifactName": "dev.deadcode.forge-${version}.${ext}" + "target": "nsis", + "artifactName": "dev.deadcode.forge-${version}.${ext}" }, "mac": { "target": "dmg", @@ -31,6 +31,7 @@ } }, "dependencies": { - "@electron/remote": "^2.1.2" + "@electron/remote": "^2.1.2", + "discord-rpc": "^4.0.1" } }