Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Discord Rich Presence #18

Merged
merged 9 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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