discordjs / RPC

A simple RPC client for Discord

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamically replacing client IDs

bdashore3 opened this issue · comments

Hi, I'm trying to use this library with an electron app. This app will allow the user to input a client id, but I wanted to add a check to see if the client id is valid for the client to connect to discord and if it's invalid, the user can enter it again.

It turns out that I cannot test client IDs with discord's servers, so I am relying on the error from the rpc.login() method. I return that error and successfully broadcast it to the user within the app.

However, if the connection fails once, I cannot reconnect with the correct client ID since the connection is closed. Here's some code showing that

const rpc: DiscordRPC.Client = new DiscordRPC.Client({ transport: 'ipc' });

export async function registerId(clientId: string): Promise<RpcResult> {
    DiscordRPC.register(clientId);

    try {
        await rpc.login({ clientId })
    } catch (e) {
        return {success: false, error: e.message};
    }

    return {success: true};
}

I narrowed this solution as regenerating the client when the registerId function is called. This is what the revised code looks like:

let rpc: DiscordRPC.Client | undefined;

export async function registerId(clientId: string): Promise<RpcResult> {
    rpc = new DiscordRPC.Client({ transport: 'ipc' });
    DiscordRPC.register(clientId);

    try {
        await rpc.login({ clientId })
    } catch (e) {
        return {success: false, error: e.message};
    }

    return {success: true};
}

In this case, the RPC client is generated every time this function is called and if successful, that client will be used throughout the duration of the app launch.

However, there was an issue regarding this because when I tried running this code, the RPC status would not show up on discord, but when running the first code snippet, it did.

I have this code in my own repository, let me know if you want to see anything else.

commented

If you could provide a runnable javascript reproduction that would be great. As it is, i don't see any evidence of a bug in this library.

Sure, I'll push my changes

Took me some time, but I made a working JS example with an electron app.

https://github.com/bdashore3/snek-test

If you have questions on how to test each case, let me know. My contact info is on the README or just comment on this issue. I understand that this is a very complicated case, but I appreciate your time.

commented

You need to add the ready event handler every time you create a new client.

Looks like that works. Tysm!