Skip to content

Commit

Permalink
Add new RPC methods (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore authored Aug 31, 2024
1 parent 73483b8 commit aa5fd40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
3 changes: 0 additions & 3 deletions apps/desktop/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useSocket } from "./rpc/manager";
import { Routes, Route } from "react-router-dom";
import { MainView } from "./views/main";
import { ChannelView } from "./views/channel";
Expand All @@ -15,7 +14,6 @@ import { Toaster } from "./components/ui/toaster";
import { useEffect } from "react";

function App() {
useSocket();
useDisableWebFeatures();

useEffect(() => {
Expand All @@ -41,7 +39,6 @@ function App() {
setAlignDirection={setHorizontalDirection}
/>
)}

<Toaster />
<Routes>
<Route path="/" Component={MainView} />
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/rpc/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ export enum RPCCommand {
GET_VOICE_SETTINGS = "GET_VOICE_SETTINGS",
SET_VOICE_SETTINGS_2 = "SET_VOICE_SETTINGS_2",
SET_VOICE_SETTINGS = "SET_VOICE_SETTINGS",
PLAY_SOUNDBOARD_SOUND = "PLAY_SOUNDBOARD_SOUND",
GET_SOUNDBOARD_SOUNDS = "GET_SOUNDBOARD_SOUNDS",
}
6 changes: 6 additions & 0 deletions apps/desktop/src/rpc/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export enum RPCEvent {
VOICE_CHANNEL_SELECT = "VOICE_CHANNEL_SELECT",
/** sent when the client's voice connection status changes */
VOICE_CONNECTION_STATUS = "VOICE_CONNECTION_STATUS",
/** not quite sure */
VIDEO_STATE_UPDATE = "VIDEO_STATE_UPDATE",
/** not quite sure */
SCREENSHARE_STATE_UPDATE = "SCREENSHARE_STATE_UPDATE",
/** sent when the you get a message that tags your or a dm */
NOTIFICATION_CREATE = "NOTIFICATION_CREATE",
}

// TODO: move this somewhere
Expand Down
42 changes: 36 additions & 6 deletions apps/desktop/src/rpc/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class SocketManager {
public _navigate: NavigateFunction | null = null;
public isConnected = false;
public version: string | undefined;
// @ts-expect-error need better types
public soundBoardItemsResolver = Promise.withResolvers();

private navigate(url: string) {
if (window.location.hash.includes("#settings")) return;
Expand Down Expand Up @@ -151,7 +153,21 @@ class SocketManager {
this.send({
args: {
client_id: APP_ID,
scopes: ["rpc", "identify"],
scopes: [
"identify",
"rpc",
// TODO: when we need soundboard we can enable these scopes
// "guilds",
// "rpc.notifications.read",
// TODO: how do you use other scopes 🤔
// "rpc.activities.write",
// "rpc.voice.read",
// "rpc.voice.write",
// "rpc.video.read",
// "rpc.video.write",
// "rpc.screenshare.read",
// "rpc.screenshare.write",
],
},
cmd: RPCCommand.AUTHORIZE,
});
Expand Down Expand Up @@ -193,6 +209,7 @@ class SocketManager {
}

const payload: DiscordPayload = JSON.parse(event.data);
console.log(payload);

// either the token is good and valid and we can login otherwise prompt them approve
if (payload.evt === RPCEvent.READY) {
Expand Down Expand Up @@ -241,6 +258,11 @@ class SocketManager {
this.store.updateUser(payload.data);
}

if (payload.cmd === RPCCommand.GET_SOUNDBOARD_SOUNDS) {
// update the Promise
this.soundBoardItemsResolver.resolve(payload.data);
}

// VOICE_CHANNEL_SELECT sent when the client joins a voice channel
if (payload.evt === RPCEvent.VOICE_CHANNEL_SELECT) {
if (payload.data.channel_id === null) {
Expand Down Expand Up @@ -338,10 +360,10 @@ class SocketManager {
// try to find the user
this.requestUserChannel();

// subscribe to get notified when the user changes channels
// sub to any otifs
this.send({
cmd: RPCCommand.SUBSCRIBE,
evt: RPCEvent.VOICE_CHANNEL_SELECT,
evt: RPCEvent.NOTIFICATION_CREATE,
});

this.userdataStore.setAccessTokenExpiry(payload.data.expires);
Expand Down Expand Up @@ -376,12 +398,21 @@ class SocketManager {
});
}

/** Get the soundboard items */
public async getSoundBoardItems() {
await this.send({
cmd: RPCCommand.GET_SOUNDBOARD_SOUNDS,
});

return this.soundBoardItemsResolver.promise;
}

/**
* Send a message to discord
* @param payload {DiscordPayload} the payload to send
*/
private send(payload: DiscordPayload) {
this.socket?.send(
public send(payload: DiscordPayload) {
return this.socket?.send(
JSON.stringify({
...payload,
nonce: uuid.v4(),
Expand All @@ -401,7 +432,6 @@ class SocketManager {
cmd,
args: { channel_id: channelId },
evt: eventName,
nonce: uuid.v4(),
})
);
}
Expand Down

0 comments on commit aa5fd40

Please sign in to comment.