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++