diff --git a/environments/core/extensions/PresenceHandler.js b/environments/core/extensions/PresenceHandler.js index d47b6b6..b6b5362 100644 --- a/environments/core/extensions/PresenceHandler.js +++ b/environments/core/extensions/PresenceHandler.js @@ -6,16 +6,17 @@ const RPC = require("../rpc/RPC"); */ module.exports = (mainWindow) => { ipcMain.on("setActivity", async (ev, name) => { - console.log("Activity Call"); if (!RPC.ready) await RPC.login(); - RPC.setActivity(name); + if (RPC.ready) RPC.setActivity(name); }); ipcMain.on("destroyRPC", (ev) => { - RPC.logout(); + if (RPC.ready) RPC.logout(); }); - ipcMain.on("reconnectRPC", (ev) => { - RPC.login(); + ipcMain.on("reconnectRPC", async (ev) => { + if (RPC.ready) return; + const success = await RPC.login(); + if (success) RPC.setActivity(); }); }; diff --git a/environments/core/rpc/RPC.js b/environments/core/rpc/RPC.js index bdf5eb8..3becaab 100644 --- a/environments/core/rpc/RPC.js +++ b/environments/core/rpc/RPC.js @@ -17,7 +17,7 @@ class RichPresence { login() { console.log("Enabled?", this.enabled()); - if (this.enabled()) return Promise.resolve(false); + if (!this.enabled()) return Promise.resolve(false); return new Promise((resolve) => { this.client.on("ready", () => { @@ -29,7 +29,10 @@ class RichPresence { .login({ clientId: this.id }) - .catch(() => resolve(false)); + .catch((e) => { + console.error(e); + resolve(false); + }); }); } @@ -37,21 +40,21 @@ class RichPresence { this.client .setActivity({ details: title || "Scratch For Discord", - timestamps: { - start: this.startedAt - }, - assets: { - large_image: "large", - large_text: `Scratch For Discord - v${packageMeta.version}` - }, + startTimestamp: this.startedAt, buttons: [ { label: "Download", url: "https://androz2091.github.io/scratch-for-discord/download/index.html" } - ] + ], + largeImageKey: "large", + largeImageText: `Scratch For Discord - v${packageMeta.version}`, + smallImageKey: "small", + smallImageText: "Scratch For Discord" }) - .catch(() => {}); + .catch((e) => { + console.error(e); + }); } logout() { @@ -59,7 +62,9 @@ class RichPresence { () => { this.ready = false; }, - () => {} + (e) => { + console.error(e); + } ); } } diff --git a/environments/main.js b/environments/main.js index 6455b10..0dd0d99 100644 --- a/environments/main.js +++ b/environments/main.js @@ -3,6 +3,7 @@ const isDev = require("electron-is-dev"); const path = require("path"); const Updater = require("./updates/window"); const updater = new Updater(); +const rpc = require("./core/rpc/RPC"); require("./core/storage/database"); const S4D_PROTOCOL = "s4d"; let tray = null, @@ -90,8 +91,7 @@ async function createWindow() { if (mainWindow.maximizable) mainWindow.maximize(); // load extensions new (require("./core/ExtensionsLoader"))(mainWindow); - const rpc = require("./core/rpc/RPC"); - rpc.login().then(() => rpc.setActivity()); + rpc.login().then((success) => (success ? rpc.setActivity() : console.log("Could not start RPC"))); }); mainWindow.webContents.setWindowOpenHandler((handler) => { diff --git a/package.json b/package.json index 5e8d4e2..87e0ade 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scratch-for-discord", - "version": "2.0.0-dev.10", + "version": "2.0.0-dev.11", "private": true, "main": "environments/main.js", "description": "Build your own discord bot with scratch blocks.", @@ -33,6 +33,7 @@ "@fortawesome/free-solid-svg-icons": "^5.15.4", "@fortawesome/react-fontawesome": "^0.1.15", "@tailwindcss/forms": "^0.3.3", + "@types/discord-rpc": "^4.0.0", "@types/http-server": "^0.12.1", "@types/react": "^17.0.17", "@types/react-router-dom": "^5.1.8", diff --git a/src/components/ExtensionStore/ExtensionStore.jsx b/src/components/ExtensionStore/ExtensionStore.jsx index c102e78..1c9e62f 100644 --- a/src/components/ExtensionStore/ExtensionStore.jsx +++ b/src/components/ExtensionStore/ExtensionStore.jsx @@ -1,4 +1,10 @@ +import { useEffect } from "react"; + export default function ExtensionStore() { + useEffect(() => { + window.ScratchNative?.sendMessage("setActivity", "on S4D Store"); + }, []); + return (
diff --git a/src/components/HomeScreen/HomeScreen.jsx b/src/components/HomeScreen/HomeScreen.jsx index 7696358..2266fa7 100644 --- a/src/components/HomeScreen/HomeScreen.jsx +++ b/src/components/HomeScreen/HomeScreen.jsx @@ -25,6 +25,7 @@ export default function HomeScreen() { const routeHistory = useHistory(); useEffect(() => { + window.ScratchNative?.sendMessage("setActivity", "Scratch For Discord"); console.log("[DEBUG] Loading recent workspace data..."); window.ScratchNative?.onceMessage("recentWorkspace", (ev, data) => { setWorkspaces(Array.isArray(data) ? data : []); diff --git a/src/components/Settings/Settings.jsx b/src/components/Settings/Settings.jsx index 069494c..8be283a 100644 --- a/src/components/Settings/Settings.jsx +++ b/src/components/Settings/Settings.jsx @@ -8,6 +8,7 @@ export default function Settings() { const [settings, setSettings] = useState(null); function refresh() { + window.ScratchNative?.sendMessage("setActivity", "on settings"); console.log("[DEBUG] Loading settings..."); const scratch = window.ScratchNative; @@ -41,6 +42,8 @@ export default function Settings() { className="form-select px-4 py-1 w-1/2 mt-1 rounded-md bg-gray-100 border-transparent focus:border-gray-500 dark:focus:bg-white focus:ring-0" onChange={(e) => { window.ScratchNative?.sendMessage("toggleRPC", e.target.value === "on"); + if (e.target.value !== "on") window.ScratchNative?.sendMessage("destroyRPC"); + else if (e.target.value === "on") window.ScratchNative?.sendMessage("reconnectRPC"); refresh(); }} > diff --git a/src/components/Workspace/SlashWorkspace.jsx b/src/components/Workspace/SlashWorkspace.jsx index a9e225a..fc21d8c 100644 --- a/src/components/Workspace/SlashWorkspace.jsx +++ b/src/components/Workspace/SlashWorkspace.jsx @@ -14,6 +14,7 @@ export default class Workspace extends Component { componentDidMount() { console.log("[DEBUG] Loading workspace..."); + window.ScratchNative?.sendMessage("setActivity", "on Slash Commands GUI"); window.ScratchNative?.onceMessage("connection", (ev, status) => { this.setState({ ready: status, diff --git a/src/components/Workspace/Workspace.jsx b/src/components/Workspace/Workspace.jsx index 8a6327d..9cde723 100644 --- a/src/components/Workspace/Workspace.jsx +++ b/src/components/Workspace/Workspace.jsx @@ -19,6 +19,7 @@ export default class Workspace extends Component { componentDidMount() { console.log("[DEBUG] Loading workspace..."); + window.ScratchNative?.sendMessage("setActivity", "on S4D workspace"); window.ScratchNative?.onceMessage("connectFallbackServer", (ev, port) => { if (!port) return; this.setState({ diff --git a/yarn.lock b/yarn.lock index 3b5eb81..dd9c5ab 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1918,6 +1918,11 @@ dependencies: "@types/ms" "*" +"@types/discord-rpc@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@types/discord-rpc/-/discord-rpc-4.0.0.tgz#29080812b9092996046af1a21138fb488d180f87" + integrity sha512-a5HiKOcBkB43g/lN6fBYw8FyGc6Ue9CYucxxHxXlELXpb1CxCa2NA2pGK2Ub88pi4uY5+HQeSFbYtH6DJtV3Qw== + "@types/eslint@^7.2.6": version "7.28.0" resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-7.28.0.tgz#7e41f2481d301c68e14f483fe10b017753ce8d5a"