Skip to content

Accept an external instance (v1.3.0) - #13

Merged
Furnyr merged 4 commits into
mainfrom
feat/external-instance
Oct 21, 2024
Merged

Accept an external instance (v1.3.0)#13
Furnyr merged 4 commits into
mainfrom
feat/external-instance

Conversation

@Furnyr

@Furnyr Furnyr commented Oct 20, 2024

Copy link
Copy Markdown
Owner

This pull request adds support for using an external SDK instance to improve compatibility with the Robo.js framework.

New exports

  • DiscordSDK: class
  • useSdk: function
  • DataPromise: type

Explanation

Dissonity v1 needs to have an instance and a user object to provide functionality. By the time the Unity build loads, the message handler has to be already awaiting the data. If useSdk was called once the authentication was done (and you had access to the user object), messages sent by Unity until then would be lost.

This is avoided by passing a promise to useSdk that will resolve once authentication is done. That way, messages sent by Unity before authentication are left pending until the promise is resolved.

The DataPromise type represents the shape this promise needs to have.

Use

index.ts

import { useSdk, DiscordSDK, DataPromise } from "dissonity";

window.addEventListener("DOMContentLoaded", async () => {

    const promise: DataPromise = new Promise(async resolve => {
    
        const discordSdk = new DiscordSDK(/* Client id */);
        await discordSdk.ready();
  
        // (Authorization process...)
  
        const { user } = await discordSdk.commands.authenticate({
            access_token: /* Retrieved access token */,
        });

        resolve({ discordSdk, user });
    });

  useSdk(promise);
});

TODO

  • Update example server to use v1.3.0
  • Add documentation

@Furnyr

Furnyr commented Oct 20, 2024

Copy link
Copy Markdown
Owner Author

I can make the user parameter nullable to make authentication optional. However, it's important to note that if the user isn't authenticated, while the Unity game would load anyway, any attempt to communicate with the Discord client will result in an error.

@Furnyr

Furnyr commented Oct 20, 2024

Copy link
Copy Markdown
Owner Author

Using an external instance without authentication would be something like this:


index.ts

import { useSdk, DiscordSDK, DataPromise } from "dissonity";

let authenticate = /* Authenticate? */;

window.addEventListener("DOMContentLoaded", async () => {

    const promise: DataPromise = new Promise(async resolve => {
    
        const discordSdk = new DiscordSDK(/* Client id */);
        await discordSdk.ready();
  
        if (authenticate) {
        
            // (Authorization process...)
  
            const { user } = await discordSdk.commands.authenticate({
                access_token: /* Retrieved access token */,
            });
            
            resolve({ discordSdk, user });
        }
        
        else {
            resolve({ discordSdk, user: null });
        }
    });

  useSdk(promise);
});

@Furnyr
Furnyr merged commit 3fe79b2 into main Oct 21, 2024
@Furnyr
Furnyr deleted the feat/external-instance branch October 21, 2024 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant