Lachee / discord-rpc-csharp

C# custom implementation for Discord Rich Presence. Not deprecated and still available!
MIT License
560 stars 93 forks source link

[BUG] RPC doesn't update a second time or beyond #145

Open Founntain opened 3 years ago

Founntain commented 3 years ago

Describe the bug After initializing the client and setting the RPC for the first time it works totally fine. Updating the presence doesn't work anymore. Worked perfectly fine before. Looking inside the OnPresenceUpdate the presence got updated, but the discord client doesn't show the updated RPC

To Reproduce Steps to reproduce the behavior:

  1. Create Client
  2. Initialize the client
  3. Set RPC once
  4. Set RPC again

Expected behavior The discord client should change the presence to the according data

Desktop (please complete the following information):

Additional context First RPC (correct)

Second presence (Not updated) Should be LA FESTA LA VITA by GUHROOVY

Data from OnPresenceUpdate Screenshot after successfull update

I also want to mention that it worked totally fine I just noticed it now. No code changes made. If I have to change something because of changes of the RPC, please let me know.

Lachee commented 3 years ago

I'll look into this and see if I can reproduce it. Are you able to provide a code snippet that reproduces this issue?

Founntain commented 3 years ago

Code Snipptes

Client.Invoke() isn't called manually by me because of AutomaticeInvoke is set to true (by default)

I hope these will help you. If you need anything please tell I try to give you as much info as I possible can.


private DiscordRpcClient Client;
private RichPresence Presence;

//Initializing the client
private void Initialize()
{
    Debug.WriteLine("Creating... Discord Client");

    Client = new DiscordRpcClient(ClientId)
    {
        Logger = new ConsoleLogger {Level = DiscordLogLevel}
    };

    Debug.WriteLine("Subscribing Discord events...");

    Client.OnReady += OnReady;
    Client.OnClose += OnClose;
    Client.OnError += OnError;

    Client.OnConnectionEstablished += OnConnectionEstablished;
    Client.OnConnectionFailed += OnConnectionFailed;

    Client.OnPresenceUpdate += OnPresenceUpdate;

    Client.OnSubscribe += OnSubscribe;
    Client.OnUnsubscribe += OnUnsubscribe;

    Client.OnJoin += OnJoin;
    Client.OnJoinRequested += OnJoinRequested;

    Client.SetSubscription(EventType.Join | EventType.JoinRequest);
    Client.Initialize();

    Debug.WriteLine("Discord Initialize finished...\n");
}

//Method to update the current Discord RPC
public void UpdatePresence(string details, string state, int partySize, string joinSecret)
{
    var presence = new RichPresence
    {
        Details = details,
        State = state,
        Assets = new Assets { },
        Party = new Party {ID = OsuPlayer.PartyManager.PartyId ?? string.Empty, Size = partySize, Max = 256}
    };

    presence.Secrets = new Secrets
    {
        JoinSecret = joinSecret
    };

    Client.SetPresence(presence);
}
IMB11 commented 2 years ago

I am currently hitting this bug aswell, my code:

public class MainMod : MelonMod
    {
        public static DiscordRpcClient client;
        public static MelonLogger.Instance Logger;
        private float timer = 0.0f;
        public float period = 15f;

        public override void OnApplicationLateStart()
        {
            Logger = LoggerInstance;
        }

        public override void OnSceneWasLoaded(int buildIndex, string sceneName)
        {
            if (client == null)
            {
                client = new DiscordRpcClient("925431370577231974", client: new UnityNamedPipe())
                {
                    Logger = new LoggerPassthrough()
                };

                client.OnReady += (sender, e) =>
                {
                    LoggerInstance.Msg("Received Ready from user " + e.User.Username);
                    client.SetPresence(new RichPresence()
                    {
                        State = "Idle",
                        Assets = new DiscordRPC.Assets()
                        {
                            LargeImageKey = "big-image"
                        }
                    });
                };

                client.OnPresenceUpdate += (sender, e) =>
                {
                    LoggerInstance.Msg("Received Update! " + e.Presence);
                };

                client.Initialize();
            }
        }

        public override void OnUpdate()
        {
            timer += Time.deltaTime;

            if (timer > period)
            {
                timer = timer - period;
                LoggerInstance.Msg("Tick RPC");
                Logger.Msg(String.Format("{0}/15 | {1} | {2}", Hooks._big.CurrentRoom.RemoteUsers.Count, Hooks._big.CurrentRoom.Name, Hooks._big.CurrentRoom.Visibility));
                Logger.Msg(Hooks._big.CurrentRoom.Environment);
                client.ClearPresence();
                client.SetPresence(new RichPresence()
                {
                    State = String.Format("{0}/15 | {1} | {2}", Hooks._big.CurrentRoom.RemoteUsers.Count, Hooks._big.CurrentRoom.Name, Hooks._big.CurrentRoom.Visibility),
                    Details = Hooks._big.CurrentRoom.Environment,
                    Assets = new DiscordRPC.Assets()
                    {
                        LargeImageKey = "big-image"
                    }
                });
            }
        }

        public override void OnApplicationQuit()
        {
            client.Dispose();
        }
    }
IMB11 commented 2 years ago

Manually client.Invoke()ing doesn't do any difference.

IMB11 commented 2 years ago

@Founntain Did you get it fixed or?

IMB11 commented 2 years ago

Seems to be linked to #84

Founntain commented 2 years ago

@Founntain Did you get it fixed or?

Well I can't tell. I really waited for an response to that. And I have to say sometimes it just keeps working fine and sometimes it doesn't have to check tho.

IMB11 commented 2 years ago

Is there any update on this? @Lachee

Lachee commented 2 years ago

This library isn't broken or deprecated. The core library is working fine, if there is an issue its likely just the Unity package. If you are able to provide logs with the latest package, then please share them as they will be better able to diagnose the issue.

IMB11 commented 2 years ago

Im using this for Unity via a modloader, specifically MelonLoader - I've imported UnityNamedPipe - there are no logs because it works as intended, just the presence doesn't change after 1 update