Skip to content

Commit 156f46d

Browse files
committed
fix(RPC): fix rpc not starting
1 parent 269552f commit 156f46d

File tree

10 files changed

+44
-20
lines changed

10 files changed

+44
-20
lines changed

environments/core/extensions/PresenceHandler.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ const RPC = require("../rpc/RPC");
66
*/
77
module.exports = (mainWindow) => {
88
ipcMain.on("setActivity", async (ev, name) => {
9-
console.log("Activity Call");
109
if (!RPC.ready) await RPC.login();
11-
RPC.setActivity(name);
10+
if (RPC.ready) RPC.setActivity(name);
1211
});
1312

1413
ipcMain.on("destroyRPC", (ev) => {
15-
RPC.logout();
14+
if (RPC.ready) RPC.logout();
1615
});
1716

18-
ipcMain.on("reconnectRPC", (ev) => {
19-
RPC.login();
17+
ipcMain.on("reconnectRPC", async (ev) => {
18+
if (RPC.ready) return;
19+
const success = await RPC.login();
20+
if (success) RPC.setActivity();
2021
});
2122
};

environments/core/rpc/RPC.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RichPresence {
1717

1818
login() {
1919
console.log("Enabled?", this.enabled());
20-
if (this.enabled()) return Promise.resolve(false);
20+
if (!this.enabled()) return Promise.resolve(false);
2121

2222
return new Promise((resolve) => {
2323
this.client.on("ready", () => {
@@ -29,37 +29,42 @@ class RichPresence {
2929
.login({
3030
clientId: this.id
3131
})
32-
.catch(() => resolve(false));
32+
.catch((e) => {
33+
console.error(e);
34+
resolve(false);
35+
});
3336
});
3437
}
3538

3639
setActivity(title) {
3740
this.client
3841
.setActivity({
3942
details: title || "Scratch For Discord",
40-
timestamps: {
41-
start: this.startedAt
42-
},
43-
assets: {
44-
large_image: "large",
45-
large_text: `Scratch For Discord - v${packageMeta.version}`
46-
},
43+
startTimestamp: this.startedAt,
4744
buttons: [
4845
{
4946
label: "Download",
5047
url: "https://androz2091.github.io/scratch-for-discord/download/index.html"
5148
}
52-
]
49+
],
50+
largeImageKey: "large",
51+
largeImageText: `Scratch For Discord - v${packageMeta.version}`,
52+
smallImageKey: "small",
53+
smallImageText: "Scratch For Discord"
5354
})
54-
.catch(() => {});
55+
.catch((e) => {
56+
console.error(e);
57+
});
5558
}
5659

5760
logout() {
5861
this.client.destroy().then(
5962
() => {
6063
this.ready = false;
6164
},
62-
() => {}
65+
(e) => {
66+
console.error(e);
67+
}
6368
);
6469
}
6570
}

environments/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const isDev = require("electron-is-dev");
33
const path = require("path");
44
const Updater = require("./updates/window");
55
const updater = new Updater();
6+
const rpc = require("./core/rpc/RPC");
67
require("./core/storage/database");
78
const S4D_PROTOCOL = "s4d";
89
let tray = null,
@@ -90,8 +91,7 @@ async function createWindow() {
9091
if (mainWindow.maximizable) mainWindow.maximize();
9192
// load extensions
9293
new (require("./core/ExtensionsLoader"))(mainWindow);
93-
const rpc = require("./core/rpc/RPC");
94-
rpc.login().then(() => rpc.setActivity());
94+
rpc.login().then((success) => (success ? rpc.setActivity() : console.log("Could not start RPC")));
9595
});
9696

9797
mainWindow.webContents.setWindowOpenHandler((handler) => {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scratch-for-discord",
3-
"version": "2.0.0-dev.10",
3+
"version": "2.0.0-dev.11",
44
"private": true,
55
"main": "environments/main.js",
66
"description": "Build your own discord bot with scratch blocks.",
@@ -33,6 +33,7 @@
3333
"@fortawesome/free-solid-svg-icons": "^5.15.4",
3434
"@fortawesome/react-fontawesome": "^0.1.15",
3535
"@tailwindcss/forms": "^0.3.3",
36+
"@types/discord-rpc": "^4.0.0",
3637
"@types/http-server": "^0.12.1",
3738
"@types/react": "^17.0.17",
3839
"@types/react-router-dom": "^5.1.8",

src/components/ExtensionStore/ExtensionStore.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import { useEffect } from "react";
2+
13
export default function ExtensionStore() {
4+
useEffect(() => {
5+
window.ScratchNative?.sendMessage("setActivity", "on S4D Store");
6+
}, []);
7+
28
return (
39
<div className="dark:bg-gray-900 bg-white h-screen w-full overflow-scroll">
410
<div className="pt-5 px-20">

src/components/HomeScreen/HomeScreen.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default function HomeScreen() {
2525
const routeHistory = useHistory();
2626

2727
useEffect(() => {
28+
window.ScratchNative?.sendMessage("setActivity", "Scratch For Discord");
2829
console.log("[DEBUG] Loading recent workspace data...");
2930
window.ScratchNative?.onceMessage("recentWorkspace", (ev, data) => {
3031
setWorkspaces(Array.isArray(data) ? data : []);

src/components/Settings/Settings.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function Settings() {
88
const [settings, setSettings] = useState(null);
99

1010
function refresh() {
11+
window.ScratchNative?.sendMessage("setActivity", "on settings");
1112
console.log("[DEBUG] Loading settings...");
1213
const scratch = window.ScratchNative;
1314

@@ -41,6 +42,8 @@ export default function Settings() {
4142
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"
4243
onChange={(e) => {
4344
window.ScratchNative?.sendMessage("toggleRPC", e.target.value === "on");
45+
if (e.target.value !== "on") window.ScratchNative?.sendMessage("destroyRPC");
46+
else if (e.target.value === "on") window.ScratchNative?.sendMessage("reconnectRPC");
4447
refresh();
4548
}}
4649
>

src/components/Workspace/SlashWorkspace.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class Workspace extends Component {
1414

1515
componentDidMount() {
1616
console.log("[DEBUG] Loading workspace...");
17+
window.ScratchNative?.sendMessage("setActivity", "on Slash Commands GUI");
1718
window.ScratchNative?.onceMessage("connection", (ev, status) => {
1819
this.setState({
1920
ready: status,

src/components/Workspace/Workspace.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default class Workspace extends Component {
1919

2020
componentDidMount() {
2121
console.log("[DEBUG] Loading workspace...");
22+
window.ScratchNative?.sendMessage("setActivity", "on S4D workspace");
2223
window.ScratchNative?.onceMessage("connectFallbackServer", (ev, port) => {
2324
if (!port) return;
2425
this.setState({

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,11 @@
19181918
dependencies:
19191919
"@types/ms" "*"
19201920

1921+
"@types/discord-rpc@^4.0.0":
1922+
version "4.0.0"
1923+
resolved "https://registry.yarnpkg.com/@types/discord-rpc/-/discord-rpc-4.0.0.tgz#29080812b9092996046af1a21138fb488d180f87"
1924+
integrity sha512-a5HiKOcBkB43g/lN6fBYw8FyGc6Ue9CYucxxHxXlELXpb1CxCa2NA2pGK2Ub88pi4uY5+HQeSFbYtH6DJtV3Qw==
1925+
19211926
"@types/eslint@^7.2.6":
19221927
version "7.28.0"
19231928
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-7.28.0.tgz#7e41f2481d301c68e14f483fe10b017753ce8d5a"

0 commit comments

Comments
 (0)