JnCrMx / discord-game-sdk4j

Java bindings for Discord's Game SDK
MIT License
114 stars 23 forks source link

Activity not showing up in Discord #55

Closed JJJannik closed 1 year ago

JJJannik commented 1 year ago

I took the ActivityExample and changed some small things, but there is no activity on Discord and theres also no error thrown.

The current code:

   public static void main(String[] args) {
        Core.init(new File("./DiscordGameSDK/lib/x86_64/discord_game_sdk.dll"));

        try(CreateParams params = new CreateParams()) {
            params.setClientID(661151077600722947L); //my discord client id
            params.setFlags(CreateParams.getDefaultFlags());
            params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
            Core core = new Core(params);

            Activity activity = new Activity();

            activity.setDetails("Running an example");
            activity.setState("and having fun");

            activity.timestamps().setStart(Instant.now());
            activity.assets().setLargeImage("test");

            core.activityManager().updateActivity(activity);

            activity.close();

            while(true) {
                core.runCallbacks();
                try {
                    Thread.sleep(16);
                } catch(InterruptedException e) {
                    core.close();
                    e.printStackTrace();
                }
            }
        }
    }

What could be the reason for this problem?

Robatortas commented 1 year ago

Depends on your project too. Do you want to add the activity display only to the main function seen in this code snippet, or do you want to execute the activity displayer when running another program?

I am not good at explanations but just check if you're actually running the main function you want to run, sounds incredibly dumb, but could be the case.

The second thing would be that the file path to the sdk is incorrect, either by a spelling mistake, or some other issue.

Third thing, try exporting your code into a jar and run it, see if the user activity works.

Hope this helps 😉

JJJannik commented 1 year ago

It is just this one main method in my project.

I've tried everything you suggested, the main is definitely running, the path is correct and I built my programm with the dependency to a jar und ran it.

Sadly, there is still no activity on Discord shown.

Robatortas commented 1 year ago

Try making the File Path to the .dll file a variable. Then of course make the new File have the parameter of the variable.

Also make the File instance a variable too, and of course, create the file.

This is just for a more organized code base.

`
File file = null; String path = "./DiscordGameSDK/lib/x86_64/discord_game_sdk.dll";

public static void main(String[] args) { this.file = new File(this.path); Core.init(file);

    try(CreateParams params = new CreateParams()) {
        params.setClientID(661151077600722947L); //my discord client id
        params.setFlags(CreateParams.getDefaultFlags());
        params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
        Core core = new Core(params);

        Activity activity = new Activity();

        activity.setDetails("Running an example");
        activity.setState("and having fun");

        activity.timestamps().setStart(Instant.now());
        activity.assets().setLargeImage("test");

        core.activityManager().updateActivity(activity);

        activity.close();

        while(true) {
            core.runCallbacks();
            try {
                Thread.sleep(16);
            } catch(InterruptedException e) {
                core.close();
                e.printStackTrace();
            }
        }
    } catch(Exception e) {
              e.printStackTrace();
    }
}`

Also, you don't end the first TRY declaration with a CATCH.

So java never knows you catched an error.

Try adding to add the catch just after the penultimate curly brace and see if it throws an error or not.

If you get no exceptions after this, then it could also be that you're simply using another operating system other than Windows, since well... DLL files are exclusive for Windows OS', so if you are using a mac or linux machine, that could be the cause of the error.

If you still need help I am heavily open to help.

JJJannik commented 1 year ago

I've got now this code and it's still nothing to see on Discord or an error thrown.

    static File file = null;
    static String path = "./DiscordGameSDK/lib/x86_64/discord_game_sdk.dll";

    public static void main(String[] args) {
        System.out.println("Activity started.");
        file = new File(path);
        Core.init(file);

        try (CreateParams params = new CreateParams(); Activity activity = new Activity()) {
            params.setClientID(661151077600722947L);
            params.setFlags(CreateParams.getDefaultFlags());
            params.setFlags(CreateParams.Flags.NO_REQUIRE_DISCORD);
            Core core = new Core(params);

            activity.setDetails("Running an example");
            activity.setState("and having fun");

            activity.timestamps().setStart(Instant.now());

            core.activityManager().updateActivity(activity);

            while(true) {
                core.runCallbacks();
                try {
                    Thread.sleep(16);
                } catch(InterruptedException e) {
                    core.close();
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And I am using Windows, so there shouldn't be a problem with the DLL file.

JJJannik commented 1 year ago

@Robatortas any other ideas? The thing is, I even tried to use a unchanged example (i only changed the discord id) and there was anyways nothing shown on Discord.

Robatortas commented 1 year ago

You did create a discord application on discord's dev portal right?

(Sorry for the late reply)

JJJannik commented 1 year ago

Uhm, yeah I've got one but what exactly should I do there with it?

Robatortas commented 1 year ago

Get the Client ID located on the OAuth2 tab on Discords Developer Portal.

Get the Client ID and put it in the "params.setClientID()" method. (I am guessing you already have this part).

Also, check that your discord application is not a bot.

Of course, name the application too.

Also, could be that your class path is set incorrectly on your project.

If none of that works, try taking out all of the code you have and replace it with the one I am about to provide.

Create three classes, one called user activity, another called DownloadNativeLibrary, and the last one being the MainApp for organization's sake.

Add the following code to the UserActivity class: and change the CLIENT ID!:

public UserActivity() {
        System.out.println("\nStarting Discord's SDK\n");

        File discordLibrary = null;
        try {
            discordLibrary = DownloadNativeLibrary.downloadDiscordLibrary();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        if(discordLibrary == null)
        {
            System.err.println("Error downloading Discord SDK.");
            System.exit(-1);
        }
        // Initialize the Core
        Core.init(discordLibrary);

        // Set parameters for the Core
        try(CreateParams params = new CreateParams())
        {
            params.setClientID(<ADD YOUR CLIENT ID HERE!>);
            params.setFlags(CreateParams.getDefaultFlags());

            // Create the Core
            try(Core core = new Core(params))
            {
                // Create the Activity
                try(Activity activity = new Activity())
                {
                    activity.setDetails(Globals.VERSION);
//                  activity.setState("Concentrated");

                    // Setting a start time causes an "elapsed" field to appear
                    activity.timestamps().setStart(Instant.now());

                    // Make a "cool" image show up
                    activity.assets().setLargeImage("big");

                    // Setting a join secret and a party ID causes an "Ask to Join" button to appear
//                  activity.party().setID("Party!");
//                  activity.secrets().setJoinSecret("Join!");

                    // Finally, update the current activity to our activity
                    core.activityManager().updateActivity(activity);
                }

                // Run callbacks forever
                // Just keeps running the sdk y'know?
                while(true)
                {
                    core.runCallbacks();
                    try
                    {
                        // Sleep a bit to save CPU
                        Thread.sleep(16);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

Then make the DownloadNativeLibrary class and add this code into it:

public class DownloadNativeLibrary
{
    public static File downloadDiscordLibrary() throws IOException
    {
        // Find out which name Discord's library has (.dll for Windows, .so for Linux)
        String name = "discord_game_sdk";
        String suffix;

        String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
        String arch = System.getProperty("os.arch").toLowerCase(Locale.ROOT);

        if(osName.contains("windows"))
        {
            suffix = ".dll";
        }
        else if(osName.contains("linux"))
        {
            suffix = ".so";
        }
        else if(osName.contains("mac os"))
        {
            suffix = ".dylib";
        }
        else
        {
            throw new RuntimeException("cannot determine OS type: "+osName);
        }

        /*
        Some systems report "amd64" (e.g. Windows and Linux), some "x86_64" (e.g. Mac OS).
        At this point we need the "x86_64" version, as this one is used in the ZIP.
         */
        if(arch.equals("amd64")) {
            arch = "x86_64";
        }

        // Path of Discord's library inside the ZIP
        String zipPath = "lib/"+arch+"/"+name+suffix;

        // Open the URL as a ZipInputStream
        URL downloadUrl = new URL("https://dl-game-sdk.discordapp.net/2.5.6/discord_game_sdk.zip");
        HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection();
        connection.setRequestProperty("User-Agent", "discord-game-sdk4j (https://github.com/JnCrMx/discord-game-sdk4j)");
        ZipInputStream zin = new ZipInputStream(connection.getInputStream());

        // Search for the right file inside the ZIP
        ZipEntry entry;
        while((entry = zin.getNextEntry())!=null)
        {
            if(entry.getName().equals(zipPath))
            {
                // Create a new temporary directory
                // We need to do this, because we may not change the filename on Windows
                File tempDir = new File(System.getProperty("java.io.tmpdir"), "java-"+name+System.nanoTime());
                if(!tempDir.mkdir())
                    throw new IOException("Cannot create temporary directory");
                tempDir.deleteOnExit();

                // Create a temporary file inside our directory (with a "normal" name)
                File temp = new File(tempDir, name+suffix);
                temp.deleteOnExit();

                // Copy the file in the ZIP to our temporary file
                Files.copy(zin, temp.toPath());

                // We are done, so close the input stream
                zin.close();

                // Return our temporary file
                return temp;
            }
            // next entry
            zin.closeEntry();
        }
        zin.close();
        // We couldn't find the library inside the ZIP
        return null;
    }
}

(REMEMBER TO IMPORT ALL THE CLASSES! (CTRL+SHIFT+O if you're on Eclipse IDE))

And at last create the MainApp class with the following code:

public class MainApp {
    public static void main(String[] argss) {
        new UserActivity();
    }
}

So those are the solutions I have in mind.

JJJannik commented 1 year ago

If im running exactly this code, I'm getting this error:

[ERROR] ResponseError { code: UnknownError, message: "Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc." }
[ERROR] Failed to configure networking: ResponseError { code: UnknownError, message: "Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc." }

(Network is ofc not offline)

Robatortas commented 1 year ago

why don't you add me on discord so we can talk easier there. We will be able to solve your issue easier.

Discord ID: Robat#7727

JJJannik commented 1 year ago

Issue is more or less solved, for more information see #56