Skip to content

Commit

Permalink
Add new RPC methods, at least soundboard works ❤️ @spdermn02
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore committed Jul 28, 2024
1 parent cab4052 commit 180b5f7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
23 changes: 22 additions & 1 deletion apps/desktop/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import { useUpdate } from "./hooks/use-update";
import { useAppStore } from "./store";
import { Toaster } from "./components/ui/toaster";
import { useEffect } from "react";
import { Button } from "./components/ui/button";
import { RPCCommand } from "./rpc/command";

function App() {
useSocket();
const socket = useSocket();
useDisableWebFeatures();

useEffect(() => {
Expand All @@ -42,6 +44,25 @@ function App() {
/>
)}

<Button
onClick={async () => {

const allSounds = await socket?.getSoundBoardItems();
console.log({ allSounds })

// play ahh
socket?.send({
cmd: RPCCommand.PLAY_SOUNDBOARD_SOUND,
args: {
name: "cricket",
sound_id: "3",
guild_id: "DEFAULT",
},
});
}}
>
Test soundbard
</Button>
<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",
}
4 changes: 4 additions & 0 deletions apps/desktop/src/rpc/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ 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",
}

// TODO: move this somewhere
Expand Down
25 changes: 22 additions & 3 deletions apps/desktop/src/rpc/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const SUBSCRIBABLE_EVENTS = [
RPCEvent.VOICE_STATE_CREATE,
RPCEvent.VOICE_STATE_DELETE,
RPCEvent.VOICE_STATE_UPDATE,
// NOTE: these dont work or im using them wrong?
// RPCEvent.SCREENSHARE_STATE_UPDATE,
// RPCEvent.VIDEO_STATE_UPDATE,
];

export const APP_ID = "905987126099836938";
Expand Down Expand Up @@ -91,6 +94,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 +156,7 @@ class SocketManager {
this.send({
args: {
client_id: APP_ID,
scopes: ["rpc", "identify"],
scopes: ["identify", "rpc", "rpc.voice.read", "rpc.video.read", "rpc.screenshare.read"],
},
cmd: RPCCommand.AUTHORIZE,
});
Expand Down Expand Up @@ -240,6 +245,11 @@ class SocketManager {
if (payload.evt === RPCEvent.VOICE_STATE_UPDATE) {
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) {
Expand Down Expand Up @@ -375,12 +385,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 Down

0 comments on commit 180b5f7

Please sign in to comment.