Lachee / discord-rpc-csharp

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

[BUG] UpdateState not working #104

Closed minisbett closed 3 years ago

minisbett commented 3 years ago

Describe the bug

UpdateState does not update the state on discord.

DiscordRpcClient m_client = new DiscordRpcClient({appid}, -1);
m_client.Initialize();
RichPresence rp = new RichPresence();
rp.Timestamps = new Timestamps(DateTime.UtcNow);
rp.State = "...";
rp.Details = "...";
rp.Assets = new Assets() { LargeImageKey = "...", LargeImageText = "..." };
m_client.SetPresence(rp);

when I then call m_client.UpdateState("test"); the CurrentPresence property updates but it doesn't update on discord.

Desktop (please complete the following information):

Lachee commented 3 years ago

have you ensured you have "Game Activity" enabled in your discord settings?

minisbett commented 3 years ago

Of course, it works when I initially set it. But when I try to update it it stays the same

Lachee commented 3 years ago

Is that the entire code? the client needs to be scoped so it stays within the lifetime of your application. You shouldn't create the client everytime you want to set the presence. Instead, create the client once, and then just call the SetPresence everytime.

(Just making sure since it does implement IDisposable and will dispose of itself once hte scope is left).

Are you able to give the console output please?

minisbett commented 3 years ago

Of course not, it's just a summary of what I have. The client is in a static class with methods using it. The program is running. I can show console output later, I'm currently on the way to school

Lachee commented 3 years ago

No worries, just trying to knock off easy issues first. Be sure to put it in trace as well so I can see what exactly its doing :)

minisbett commented 3 years ago

Trace?

minisbett commented 3 years ago

There is no console output.

minisbett commented 3 years ago

nvm I didn't know you need to add a logger

minisbett commented 3 years ago
=================== SET 1 ===================
INFO: Attempting a new connection
TRACE: Enqueue Command: DiscordRPC.RPC.Commands.PresenceCommand
INFO: RPC Connection Started
TRACE: ============================
TRACE: Assembly:             DiscordRPC, Version=1.0.150.0, Culture=neutral, PublicKeyToken=null
TRACE: Pipe:                 DiscordRPC.IO.ManagedNamedPipeClient
TRACE: Platform:             Microsoft Windows NT 6.2.9200.0
TRACE: applicationID:        770355757622755379
TRACE: targetPipe:           -1
TRACE: POLL_RATE:            1000
TRACE: _maxRtQueueSize:      512
TRACE: _maxRxQueueSize:      0
TRACE: ============================
TRACE: Connecting to the pipe through the DiscordRPC.IO.ManagedNamedPipeClient
TRACE: ManagedNamedPipeClient.Connection(-1)
TRACE: Connection Attempt 0 ()
INFO: Attempting to connect to discord-ipc-0
TRACE: Waiting for connection...
INFO: Connected to discord-ipc-0
TRACE: Done. Result: False
TRACE: Begining Read of 16384 bytes
TRACE: Connected to the pipe. Attempting to establish handshake...
TRACE: Enqueued Message, but queue size is 0.
TRACE: Attempting to establish a handshake...
TRACE: Sending Handshake...
TRACE: Setting the connection state to CONNECTING
TRACE: Connection Established. Starting reading loop...
TRACE: Ending Read
TRACE: Read 342 bytes
TRACE: Read a frame: Frame
TRACE: Starting another read
TRACE: Begining Read of 16384 bytes
TRACE: Read Payload: Frame
INFO: Handling Response. Cmd: Dispatch, Event: Ready
INFO: Connection established with the RPC
TRACE: Setting the connection state to CONNECTED
TRACE: Enqueued Message, but queue size is 0.
TRACE: Attempting to send payload: SetActivity
TRACE: Sending payload: SetActivity
TRACE: Ending Read
TRACE: Read 397 bytes
TRACE: Read a frame: Frame
TRACE: Starting another read
TRACE: Begining Read of 16384 bytes
TRACE: Sent Successfully.
TRACE: Read Payload: Frame
INFO: Handling Response. Cmd: SetActivity, Event:
TRACE: Enqueued Message, but queue size is 0.
=================== SET 2 ===================
    static void Main(string[] args)
    {
      Console.WriteLine("=================== SET 1 ===================");
      Discord.SetPresenceServer("test");
      Thread.Sleep(3000);
      Console.WriteLine("=================== SET 2 ===================");
      Discord.SetPresenceServer("123");
      while (true)
        ;
    }
using DiscordRPC;
using DiscordRPC.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
  public static class Discord
  {

    // The discord RPC client used for the rich presence
    // given the app id (see developer portal) and -1 as the pipe for automatic pipe scan
    private static DiscordRpcClient m_client = new DiscordRpcClient("", -1, new ConsoleLogger(LogLevel.Trace, true));

    // Determines if the presence has been initialized yet
    private static bool m_initialized = false;

    public static void SetPresenceServer(string name)
    {
      if (!m_initialized)
      {
        m_client.Initialize();

        RichPresence rp = new RichPresence();
        rp.Timestamps = new Timestamps(DateTime.UtcNow);
        rp.State = "Currently playing on: " + name;
        rp.Details = "Playing osu! on a 3rd-party server using the Ultimate Osu Server Switcher";
        rp.Assets = new Assets() { LargeImageKey = "uoss", LargeImageText = "Download on GitHub!\r\nminisbett/ultimate-osu-server-switcher" };
        m_client.SetPresence(rp);

        m_initialized = true;
      }
      else
      {
        m_client.UpdateState("Currently playing on: " + name);
      }
    }

    public static void RemovePresence()
    {
      if (m_initialized)
        m_client.ClearPresence();
    }
  }
}
minisbett commented 3 years ago

Exactly nothing happens, no console output. But in Visual Studio I saw that a thread stopped.

minisbett commented 3 years ago

A quick fix would be nice, otherwise this library has no use.

Lachee commented 3 years ago

A quick fix would be nice, otherwise this library has no use.

........ excuse me?

I do this for free.. as a hobby. I am very busy IRL and do this in my free time. As you can see, there are a collection of minor issues that need to be resolved and I am currently waiting for Mason to document some more features so I could enhance the library. This bug is on my list and I have been meaning to get around to it. I just dont have the time this week.

This bug doesn't prevent the usage of this library. All the UpdateXXX functions are purely shortcuts and "Nice to Have" features. I could delete the function and the library would work just the same. Just call SetPresence again.

If you want it fixed faster, you can submit a PR yourself. Its open source and contribution is welcomed.

minisbett commented 3 years ago

Sorry, I didn't want to sound mean, it was more of a wish that a fix would be available soon. I voiced very badly, sorry. I can speak english very well but when it comes to emotions and feelings a sentence tells I often come across wrong.

I will submit a PR, I hope it will be accepted soon

Lachee commented 3 years ago

Thank you for creating the issue. This has been fixed hopefully now.