add basic discord rpc, showing osu status

This commit is contained in:
HorizonCode 2022-10-21 00:57:42 +02:00
parent 1de9fce504
commit c8a07fbb0d
4 changed files with 113 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules/ node_modules/
release/ release/
yarn.lock yarn.lock
pnpm-lock.yaml

39
app.js
View File

@ -5,8 +5,11 @@ const osuUtil = require('./osuUtil');
const ezppUtil = require('./ezppUtil'); const ezppUtil = require('./ezppUtil');
const config = require('./config'); const config = require('./config');
const fs = require('fs'); const fs = require('fs');
const rpc = require('./discordPresence');
const windowName = require('get-window-by-name');
let tempOsuPath; let tempOsuPath;
let osuWindowInfo;
const run = () => { const run = () => {
const gotTheLock = app.requestSingleInstanceLock() const gotTheLock = app.requestSingleInstanceLock()
@ -15,8 +18,43 @@ const run = () => {
return; return;
} }
setInterval(() => {
osuWindowInfo = windowName.getWindowText("osu!.exe");
const firstInstance = osuWindowInfo[0];
if (firstInstance) {
if (firstInstance.processTitle && firstInstance.processTitle.length > 0) {
const windowTitle = firstInstance.processTitle;
let rpcOsuVersion = "";
let currentMap = undefined;
if (!windowTitle.includes("-")) {
rpcOsuVersion = windowTitle;
rpc.updateState("Idle...");
} else {
var string = windowTitle;
var components = string.split(' - ');
const splitArray = [components.shift(), components.join(' - ')];
rpcOsuVersion = splitArray[0];
currentMap = splitArray[1];
rpc.updateState("Playing...");
}
rpc.updateStatus(currentMap, rpcOsuVersion);
} else {
rpc.updateState("Idle in Launcher...");
rpc.updateStatus(undefined, undefined);
}
} else {
rpc.updateState("Idle in Launcher...");
rpc.updateStatus(undefined, undefined);
}
console.log(osuWindowInfo);
}, 2000);
setupTitlebar(); setupTitlebar();
rpc.connect();
let mainWindow; let mainWindow;
app.whenReady().then(() => { app.whenReady().then(() => {
@ -46,6 +84,7 @@ const run = () => {
await osuUtil.setConfigValue(osuConfig.path, "Username", ""); await osuUtil.setConfigValue(osuConfig.path, "Username", "");
await osuUtil.setConfigValue(osuConfig.path, "Password", ""); await osuUtil.setConfigValue(osuConfig.path, "Password", "");
} }
rpc.updateState("Launching osu!...");
const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => { const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => {
await doUpdateCheck(mainWindow); await doUpdateCheck(mainWindow);
}); });

69
discordPresence.js Normal file
View File

@ -0,0 +1,69 @@
const appInfo = require('./appInfo.js');
const DiscordAutoRPC = require("discord-auto-rpc");
const { app } = require('electron');
const DiscordRPC = require("discord-rpc").default;
const clientId = "1032772293220384808";
let client = undefined;
let lastState = "Idle in Launcher...";
let presenceEnabled = true;
let startDate = new Date();
let lastActivity = {
details: " ",
state: lastState,
startTimestamp: startDate,
largeImageKey: "ezppfarm",
largeImageText: appInfo.appName + " " + appInfo.appVersion,
buttons: [
{
label: "Download the Launcher",
url: "https://ez-pp.farm/download"
},
{
label: "Join EZPPFarm",
url: "https://discord.com/invite/g8Bh7RaKPg"
}
],
instance: false,
};
module.exports = {
connect: () => {
if (client === undefined) {
client = new DiscordAutoRPC.AutoClient({ transport: "ipc" });
client.endlessLogin({ clientId: clientId });
client.once("ready", () => {
setInterval(() => {
if (lastActivity !== undefined)
lastActivity.state = lastState;
client.setActivity(presenceEnabled ? lastActivity : undefined);
}, 2500);
});
}
},
enablePresence: () => presenceEnabled = true,
disablePresence: () => presenceEnabled = false,
updateStartDate: () => startDate = new Date(),
updateState: (state) => lastState = state,
updateStatus: (details, osuVersion) => {
lastActivity = {
details: details ? details : " ",
state: lastState,
startTimestamp: startDate,
smallImageKey: osuVersion ? "osu" : " ",
smallImageText: osuVersion ? osuVersion : " ",
largeImageKey: "ezppfarm",
largeImageText: appInfo.appName + " " + appInfo.appVersion,
buttons: [
{
label: "Download the Launcher",
url: "https://ez-pp.farm/download"
},
{
label: "Join EZPPFarm",
url: "https://discord.com/invite/g8Bh7RaKPg"
}
],
instance: false,
}
}
}

View File

@ -50,6 +50,9 @@
"dependencies": { "dependencies": {
"axios": "^0.27.2", "axios": "^0.27.2",
"custom-electron-titlebar": "^4.1.1", "custom-electron-titlebar": "^4.1.1",
"discord-auto-rpc": "^1.0.17",
"discord-rpc": "^4.0.1",
"get-window-by-name": "^2.0.0",
"jquery": "^3.6.0", "jquery": "^3.6.0",
"node-downloader-helper": "^2.1.4", "node-downloader-helper": "^2.1.4",
"sweetalert2": "^11.5.2", "sweetalert2": "^11.5.2",