Skip to content

Commit

Permalink
Merge pull request #18 from asoji/discord-rpc
Browse files Browse the repository at this point in the history
Discord Rich Presence
  • Loading branch information
Kyle Edwards authored Jul 17, 2022
2 parents 349f41c + c32f856 commit 6859dd0
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 4 deletions.
52 changes: 52 additions & 0 deletions assets/drpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const DiscordRPC = require(`discord-rpc`);
const pkg = require(`../package.json`);

const clientId = `997378700611952660`;

let client = null;

let projectTimestamp = new Date();
let projectDetails = `Loading...`;

module.exports._test = () => {
console.log(projectDetails);
};

module.exports.setDetails = text => {
projectDetails = text;

if (client == null) setup();
else setActivity();
};

module.exports.resetTime = () => {
projectTimestamp = new Date();

if (client == null) setup();
else setActivity();
};

module.exports.destroy = () => {
if (client == null) return;
client.destroy();
};

function setup() {
client = new DiscordRPC.Client({ transport: `ipc` });

client.on(`ready`, () => {
setActivity();
});

client.login({ clientId }).catch(console.error);
}

function setActivity() {
if (client != null) client.setActivity({
details: projectDetails,
startTimestamp: projectTimestamp,
largeImageKey: `logo`,
largeImageText: `v${pkg.version}`,
instance: false
});
}
7 changes: 5 additions & 2 deletions assets/preload.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const { contextBridge, ipcRenderer } = require(`electron`);
const pkg = require(`../package.json`);
const drpc = require(`./drpc.js`);

const appVersion = `Node Studio ${pkg.version}`;

function setWinTitle(text) {
function setProjectTitle(text) {
document.title = `${text} | ${appVersion}`;
drpc.setDetails(text);
}

window.addEventListener(`DOMContentLoaded`, () => {
document.querySelector(`#version`).innerHTML = appVersion;
});

contextBridge.exposeInMainWorld(`memUsage`, process.memoryUsage);
contextBridge.exposeInMainWorld(`setWinTitle`, setWinTitle);
contextBridge.exposeInMainWorld(`setProjectTitle`, setProjectTitle);
contextBridge.exposeInMainWorld(`DRPCResetTime`, drpc.resetTime);
4 changes: 2 additions & 2 deletions assets/scripts/core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ setInterval(() => {

const pTitle = document.querySelector(`#projectTitle`);

window.setWinTitle(pTitle.value || pTitle.placeholder);
window.setProjectTitle(pTitle.value || pTitle.placeholder);

import(`./graphs.mjs`);
import(`./piano.mjs`);
Expand All @@ -37,7 +37,7 @@ window.addEventListener(`keydown`, (e) => {

if (e.target.id === `projectTitle`) {
const newVal = e.target.value || e.target.placeholder;
window.setWinTitle(newVal);
window.setProjectTitle(newVal);
memory.project.title = newVal;
console.log(JSON.stringify(memory.project, null, 2));
}
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require(`fs`);
const path = require(`path`);
const { app, BrowserWindow, nativeImage, shell } = require(`electron`);
const drpc = require(`./assets/drpc`);

let config = {};

Expand Down Expand Up @@ -102,6 +103,9 @@ app.on(`ready`, () => {
app.on(`window-all-closed`, () => {
fs.writeFileSync(`./userdata/config.json`, JSON.stringify(config), { encoding: `utf8` });

drpc._test();
drpc.destroy();

// end program when all windows are closed
// except macOS because its ✨ not like other girls ✨ or something
if (process.platform !== `darwin`) app.quit();
Expand Down
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"dependencies": {
"adm-zip": "^0.4.13",
"discord-rpc": "^4.0.1",
"jaro-winkler": "^0.2.8",
"node-fetch": "^2.6.1",
"string-similarity": "^3.0.0"
Expand Down

0 comments on commit 6859dd0

Please sign in to comment.