-
-
Notifications
You must be signed in to change notification settings - Fork 46
feat(DiscordRPC): add support for targeting specific Discord instances (PTB/Canary) #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,59 @@ | ||
| import { Client, StatusDisplayType, type SetActivity } from "@xhayper/discord-rpc"; | ||
| import { Client, StatusDisplayType, type ClientOptions, type SetActivity } from "@xhayper/discord-rpc"; | ||
| import { asyncDebounce } from "@inrixia/helpers"; | ||
| import { getAvailablePipes } from "./getAvailablePipes.native"; | ||
|
|
||
| let rpcClient: Client | null = null; | ||
| export const getClient = async () => { | ||
| let currentPipeId: number = -1; | ||
|
|
||
| export const getClient = asyncDebounce(async (preferredPipeId: number = -1) => { | ||
| const availablePipes = await getAvailablePipes(); | ||
|
|
||
| let targetPipe = preferredPipeId; | ||
| if (availablePipes.length > 0) { | ||
| targetPipe = availablePipes.includes(preferredPipeId) ? preferredPipeId : availablePipes[0]; | ||
| } | ||
|
|
||
| if (rpcClient && currentPipeId !== targetPipe) { | ||
| await rpcClient.destroy().catch(() => {}); | ||
|
Inrixia marked this conversation as resolved.
Outdated
|
||
| rpcClient = null; | ||
|
Inrixia marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const isAvailable = rpcClient && rpcClient.transport.isConnected && rpcClient.user; | ||
| if (isAvailable) return rpcClient!; | ||
|
|
||
| if (rpcClient) await rpcClient.destroy(); | ||
| rpcClient = new Client({ clientId: "1130698654987067493" }); | ||
| await rpcClient.connect(); | ||
| if (rpcClient) await rpcClient.destroy().catch(() => {}); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the catch here as well. |
||
|
|
||
| return rpcClient; | ||
| }; | ||
| const clientOptions: ClientOptions = { | ||
| clientId: "1130698654987067493" | ||
| }; | ||
|
|
||
| if (targetPipe >= 0) { | ||
| clientOptions.pipeId = targetPipe; | ||
| } | ||
|
|
||
| export const setActivity = async (activity?: SetActivity) => { | ||
|
Inrixia marked this conversation as resolved.
|
||
| const client = await getClient(); | ||
| if (!client.user) return; | ||
| rpcClient = new Client(clientOptions); | ||
| currentPipeId = targetPipe; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should only be set after
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only this left and then happy to merge, thanks for fixing these things 💜 |
||
|
|
||
| try { | ||
| await rpcClient.connect(); | ||
| return rpcClient; | ||
| } catch (error) { | ||
|
Inrixia marked this conversation as resolved.
Outdated
|
||
| rpcClient = null; | ||
| throw error; | ||
| } | ||
| }); | ||
|
|
||
| export const setActivity = async (activity?: SetActivity, preferredPipeId: number = -1) => { | ||
| const client = await getClient(preferredPipeId); | ||
| if (!client || !client.user) return; | ||
|
Inrixia marked this conversation as resolved.
Outdated
|
||
| if (!activity) return client.user.clearActivity(); | ||
| return client.user.setActivity(activity); | ||
| return await client.user.setActivity(activity); | ||
| }; | ||
|
|
||
| export const cleanupRPC = () => { | ||
| if (rpcClient) rpcClient.destroy().catch(() => {}); | ||
| }; | ||
|
|
||
| export const cleanupRPC = () => rpcClient?.destroy()!; | ||
|
Inrixia marked this conversation as resolved.
|
||
| export const StatusDisplayTypeEnum = () => ({ | ||
| Name: StatusDisplayType.NAME, | ||
| State: StatusDisplayType.STATE, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { readdir } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
|
|
||
| export const getAvailablePipes = async (): Promise<number[]> => { | ||
| const pipes = new Set<number>(); | ||
| try { | ||
| if (process.platform === "win32") { | ||
| const files = await readdir("\\\\.\\pipe\\"); | ||
| for (const file of files) { | ||
| if (file.startsWith("discord-ipc-")) { | ||
| const id = parseInt(file.replace("discord-ipc-", ""), 10); | ||
| if (!isNaN(id)) pipes.add(id); | ||
| } | ||
| } | ||
| } else { | ||
| const tempDir = process.env.XDG_RUNTIME_DIR ?? tmpdir(); | ||
| const files = await readdir(tempDir); | ||
| for (const file of files) { | ||
| if (file.startsWith("discord-ipc-")) { | ||
| const id = parseInt(file.replace("discord-ipc-", ""), 10); | ||
| if (!isNaN(id)) pipes.add(id); | ||
| } | ||
| } | ||
| } | ||
| } catch { | ||
| // Ignore read errors | ||
| } | ||
| return Array.from(pipes).sort((a, b) => a - b); | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.