Skip to content

Conversation

Almighty-Shogun
Copy link
Contributor

This pull request will allow users to update and remove their buttons without having to restart the program.

You can use this code in the following way:

Updating all the buttons

When a user wants to update all their buttons from the current Rich Presence they can do the following:

client.UpdateButtons(new Button[]
{
    new() { Label = "ButtonLabel 1", Url = "https://domain.com" }, // Button 1
    new() { Label = "ButtonLabel 2", Url = "https://domain.com" } // Button 2
});

Updating a single button

When a user wants to update a single button from the current Rich Presence they can do the following:

Example: You have 2 buttons. The first one is a link to your website and the second one is a link to your Discord server. You want to update the second one with an up-to-date invite link you can do the following code:

client.UpdateButtons(new Button[]
{
    new() { Label = "Join our Discord", Url = "https://discord.com/invite/xxx" }
}, 1);

Removing buttons

When a user want to remove their buttons from the current Rich Presence they can do the following:

client.UpdateButtons();

If there are any follow up questions or things I might have missed let me know and I will look into it asap!

@Almighty-Shogun
Copy link
Contributor Author

Almighty-Shogun commented Feb 24, 2022

I forgot to mention that there was also an issue for this #147

Copy link
Owner

@Lachee Lachee left a comment

Choose a reason for hiding this comment

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

Overall good contribution. Only request minor change to the UpdateButton(Button[], int) call

Comment on lines 530 to 559
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"
});

Implemented the request. See: #175 (comment)
@Lachee Lachee merged commit 415be20 into Lachee:master Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants