#Discord Social SDK Java 25+ port

6 messages ยท Page 1 of 1 (latest)

clever spire
#

https://github.com/Jab125/Discord-Social-SDK4J

I've been taking advantage of Project Panama's Foreign Function and Memory API and jextract to generate bindings for the C headers the Discord Social SDK provides. Unfortunately, jextract does not support C++, which means I need to make the wrapper manually.

Java port of Getting Started with C++ and the Discord Social SDK

public static final long APPLICATION_ID = 1349146942634065960L;
public static final AtomicBoolean running = new AtomicBoolean(true);
void main() throws InterruptedException {
    try {
        DiscordNatives.loadNatives();
    } catch (Throwable e) {
        throw new Error("Failed to initialize natives!", e);
    }

    IO.println("๐Ÿš€ Initializing Discord SDK...");

    // Create our Discord Client
    Client client = new Client();

    // Set up logging callback
    client.addLogCallback((message, severity) -> {
        IO.println("[" + severity + "] " + message);
    }, Client.LoggingSeverity.INFO);

    // Set up status callback to monitor client connection
    client.setStatusChangedCallback((status, error, errorDetail) -> {
        IO.println("๐Ÿ”„ Status changed: " + status);

        if (status == Client.Status.READY) {
            IO.println("โœ… Client is ready! You can now call SDK functions.");

            // Access initial relationships data
            IO.println("๐Ÿ‘ฅ Friends Count: " + client.getRelationships().size());

            // Configure rich presence details
            Activity activity = new Activity();
            activity.setType(Activity.ActivityTypes.PLAYING);
            activity.setState("In Competitive Match");
            activity.setDetails("Rank: Diamond II");

            // Update rich presence
            client.updateRichPresence(activity, result -> {
                if (result.successful()) {
                    IO.println("๐ŸŽฎ Rich Presence updated successfully!");
                } else {
                    System.err.println("โŒ Rich Presence update failed");
                }
            });
        } else if (error != Client.Error.NONE) {
            System.err.println("โŒ Connection Error: " + error + " - Details: " + errorDetail);
        }
    });

    // Generate OAuth2 code verifier for authentication
    AuthorizationCodeVerifier codeVerifier = client.createAuthorizationCodeVerifier();

    AuthorizationArgs args = new AuthorizationArgs();
    args.setClientId(APPLICATION_ID);
    args.setScopes(client.getDefaultPresenceScopes());
    args.setCodeChallenge(codeVerifier.challenge());

    // Begin authentication process
    client.authorize(args, (result, code, redirectUri) -> {
        if (!result.successful()) {
            System.err.println("โŒ Authentication Error: " + result.error());
        } else {
            IO.println("โœ… Authorization successful! Getting access token...");

            // Exchange auth code for access token
            client.getToken(APPLICATION_ID, code, codeVerifier.verifier(), redirectUri,
                    (_, accessToken, _, _, _, _) -> {
                        IO.println("๐Ÿ”“ Access token received! Establishing connection...");
                        // Next Step: Update the token and connect
                        client.updateToken(Client.AuthorizationTokenType.BEARER, accessToken, clientResult -> {
                            if(clientResult.successful()) {
                                IO.println("๐Ÿ”‘ Token updated, connecting to Discord...");
                                client.connect();
                            }
                        });
                    });
        }
    });

    // Keep application running to allow SDK to receive events and callbacks
    while (running.get()) {
        runCallbacks();
        Thread.sleep(10);
    }
}

I probably made some major memory management issues, which is a skill issue since I can't read C++

GitHub

Discord Social SDK bindings for Java 25+! Contribute to Jab125/Discord-Social-SDK4J development by creating an account on GitHub.

Documentation - Discord

Get started with the Discord Social SDK in C++ applications.

wraith shoalBOT
#

what's up with the emojis

clever spire
graceful wraith
#

This is very interesting

#

how a mod using that could compare with things like simple-rpc?

#

like, would be able to do more things than just showing the now playing?