Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions DiscordRPC/DiscordRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,77 @@ public void SetPresence(RichPresence presence)
}

#region Updates

/// <summary>
/// Updates only the <see cref="BaseRichPresence.Buttons"/> of the <see cref="CurrentPresence"/> and updates/removes the buttons. Returns the newly edited Rich Presence.
/// </summary>
/// <param name="Button">The buttons of the Rich Presence</param>
/// <returns>Updated Rich Presence</returns>
public RichPresence UpdateButtons(Button[] button = null)
{
if (!IsInitialized)
{
throw new UninitializedException();
}

// Clone the presence
RichPresence presence;
lock (_sync)
{
if (CurrentPresence == null)
{
presence = new RichPresence();
}
else
{
presence = CurrentPresence.Clone();
}
}

// Update the buttons.
presence.Buttons = button;
SetPresence(presence);

return presence;
}

/// <summary>
/// Updates only the <see cref="BaseRichPresence.Buttons"/> of the <see cref="CurrentPresence"/> and updates the button with the given index. Returns the newly edited Rich Presence.
/// </summary>
/// <param name="Button">The buttons of the Rich Presence</param>
/// <param name="buttonId">The number of the button</param>
/// <returns>Updated Rich Presence</returns>
public RichPresence UpdateButtons(Button[] button, int buttonId)
{
if (!IsInitialized)
{
throw new UninitializedException();
}

// Get the original index of the button
var buttonIndex = buttonId - 1;

// Clone the presence
RichPresence presence;
lock (_sync)
{
if (CurrentPresence == null)
{
presence = new RichPresence();
}
else
{
presence = CurrentPresence.Clone();
}
}

// Update the buttons
presence.Buttons[buttonIndex] = button[buttonIndex];
SetPresence(presence);

return presence;
}
Copy link
Owner

@Lachee Lachee Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better way to implement this function is to:

  • Make it only take a single Button instead of an array
  • Turn buttonId into index instead which starts from 0.
  • Rename it to UpdateButton or SetButton

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into the code later today for the minor changes you have requested

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented your request. I added that the index in the SetButton method is 0 by default. Users can now use this method by the following way:

// Using button as an variable
var newButton = new Button()
{
	Label = "Updated btn",
	Url = "https://github.com/Lachee/discord-rpc-csharp"
};

client.SetButton(newButton);

// Updating it without an variable
client.SetButton(new Button()
{
	Label = "Updated btn",
	Url = "https://github.com/Lachee/discord-rpc-csharp"
});


/// <summary>
/// Updates only the <see cref="BaseRichPresence.Details"/> of the <see cref="CurrentPresence"/> and sends the updated presence to Discord. Returns the newly edited Rich Presence.
/// </summary>
Expand Down