Rigner / discord-rpc-java

Java implementation of Discord RPC for rich presence https://github.com/discordapp/discord-rpc
MIT License
25 stars 9 forks source link

Rich Presence not updating when state and details not set #14

Open echebbi opened 6 years ago

echebbi commented 6 years ago

Hi !

Calling DiscordRpc.updatePresence(DiscordRichPresence) on a presence that has neither details nor state has no effect. Indeed, if these properties are not set (or empty) then Discord is not updated.

Since Discord's developer documentation seems to indicate that all fields are optional when updating a presence, I assume that it is a bug related to this implementation.

I can reproduce the bug by taking the example SimpleGame.java and commenting one of the lines that set the details or the state (on lines 91, 92).

Does someone have an idea about how to resolve this issue ? I'm on Windows, by the way.

echebbi commented 6 years ago

I think that I've found why !

Currently, the JSON sent to Discord contains the state and details fields, even when they are empty. For instance, the following presence:

discordRichPresence.setState("Doing something");
discordRichPresence.setStartTimestamp(System.currentTimeMillis() / 1000);
discordRpc.updatePresence(discordRichPresence);

leads to the following JSON:

{
   "nonce":"1",
   "cmd":"SET_ACTIVITY",
   "args":{
      "pid":15228,
      "activity":{
         "state":"Doing something",
         "details":"",
         "timestamps":{
            "start":1524398662
         },
         "instance":false
      }
   }
}

and Discord displays neither the state nor the elapsed time.

However, if we avoid empty unspecified fields and generate the JSON as follows:

{
   "nonce":"1",
   "cmd":"SET_ACTIVITY",
   "args":{
      "pid":968,
      "activity":{
         "state":"Doing something",
         "timestamps":{
            "start":1524398370
         },
         "instance":false
      }
   }
}

then Discord shows the state and the elapsed time as expected.

That's kind of strange: I would expect Discord to handle empty fields. I'll do further tests and will submit a PR if this fix works well.