Skip to content

Commit

Permalink
fix(RPC): fix rpc not starting
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Aug 25, 2021
1 parent 269552f commit 156f46d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 20 deletions.
11 changes: 6 additions & 5 deletions environments/core/extensions/PresenceHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};
29 changes: 17 additions & 12 deletions environments/core/rpc/RPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -29,37 +29,42 @@ class RichPresence {
.login({
clientId: this.id
})
.catch(() => resolve(false));
.catch((e) => {
console.error(e);
resolve(false);
});
});
}

setActivity(title) {
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() {
this.client.destroy().then(
() => {
this.ready = false;
},
() => {}
(e) => {
console.error(e);
}
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions environments/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/components/ExtensionStore/ExtensionStore.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useEffect } from "react";

export default function ExtensionStore() {
useEffect(() => {
window.ScratchNative?.sendMessage("setActivity", "on S4D Store");
}, []);

return (
<div className="dark:bg-gray-900 bg-white h-screen w-full overflow-scroll">
<div className="pt-5 px-20">
Expand Down
1 change: 1 addition & 0 deletions src/components/HomeScreen/HomeScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 : []);
Expand Down
3 changes: 3 additions & 0 deletions src/components/Settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}}
>
Expand Down
1 change: 1 addition & 0 deletions src/components/Workspace/SlashWorkspace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/components/Workspace/Workspace.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 156f46d

Please sign in to comment.