Skip to content

Commit a3369d8

Browse files
authored
Improvements to Unified Friends List Guide (#7495)
* Fix some small C++ syntax inconsistencies. * Introduce and explain `UserHandle`, and user data retrieval * Reorder into what I think is a better learning order.
1 parent 2bae562 commit a3369d8

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

docs/discord-social-sdk/development-guides/creating-a-unified-friends-list.mdx

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ First, let's create a function to see what kind of relationships and information
6161
```cpp
6262
void DisplayFriendsList(discordpp::Client& client) {
6363
std::vector<std::string> relationships{};
64-
for (auto& relationship: client.GetRelationships()) {
64+
for (auto& relationship: client->GetRelationships()) {
6565
auto user = relationship.User();
6666
if (!user) {
6767
continue;
@@ -91,6 +91,13 @@ void DisplayFriendsList(discordpp::Client& client) {
9191
}
9292
```
9393
94+
:::info
95+
The `relationship.User()` function returns a [`UserHandle`] object that represents a user that the Discord Social SDK
96+
knows about.
97+
Handle objects maintain references to both the underlying data and the SDK instance, which means that when their
98+
data changes, existing handle objects automatically reflect these changes without needing to be recreated.
99+
:::
100+
94101
We will want to call this function when the client is ready, so let's add it to our ready callback:
95102
96103
```cpp
@@ -141,7 +148,7 @@ void DisplayFriendsList(discordpp::Client& client) {
141148
std::vector<std::string> online;
142149
std::vector<std::string> offline;
143150

144-
for (auto& relationship : client.GetRelationships()) {
151+
for (auto& relationship : client->GetRelationships()) {
145152
auto user = relationship.User();
146153
if (!user) {
147154
continue;
@@ -209,21 +216,51 @@ void DisplayFriendsList(discordpp::Client& client) {
209216
210217
If we build and run our application, we should now see a list of friends separated into three categories: `Online - GameTitle`, `Online - Elsewhere`, and `Offline`.
211218
219+
## Step 3: Monitor Changes to Users
220+
221+
To monitor for user changes, we're going using the [`Client::SetUserUpdatedCallback`] function.
222+
223+
This callback will be triggered whenever a user's info is updated, such as name or presence changes (when they go online, offline, or start playing your game).
224+
225+
```cpp
226+
client->SetUserUpdatedCallback([&client](uint64_t userId) {
227+
std::cout << "👤 User updated: " << userId << std::endl;
228+
DisplayFriendsList(*client);
229+
});
230+
```
231+
232+
Now your friends list will automatically update when the presence of a friend changes, such as when they go online, offline or start playing your game.
233+
234+
:::info
235+
The automatic updates of the [`UserHandle`] object to the latest the user information should be sufficient for
236+
retrieving the most up-to-date user information.
237+
[`Client::SetUserUpdatedCallback`] may be more useful to identify times when you wish to re-sort your
238+
user list, or similar operations.
239+
:::
212240
---
213241
214-
## Step 3: Monitor Changes in Relationships
242+
## Step 4: Monitor Changes in Relationships
215243
216-
Let's setup two callbacks to handle relationship updates.
244+
Let us setup two callbacks to handle relationship updates.
245+
246+
:::warn
247+
These examples rebuild the friends list from scratch every time a relationship changes. For performance reasons, we
248+
recommend maintaining a collection of [`UserHandle`] objects and adding and removing them appropriately.
249+
:::
217250
218251
### Relationship Created Callback
219252
220253
This can happen when a user sends or accepts a friend invite, or blocks a user.
221254
222255
```cpp
223256
client->SetRelationshipCreatedCallback([&client](uint64_t userId, bool isDiscordRelationshipUpdate) {
224-
std::cout << "🤝 Relationship created: " << userId << std::endl;
225-
DisplayFriendsList(*client);
226-
});
257+
std::optional<discordpp::UserHandle> user = client->GetUser(userId);
258+
// if the userid is valid (which it should be), we can display the user's display name
259+
if(user) {
260+
std::cout << "🤝 Relationship created: " << user->DisplayName() << std::endl;
261+
DisplayFriendsList(*client);
262+
}
263+
});
227264
```
228265
229266
### Relationship Deleted Callback
@@ -239,29 +276,6 @@ client->SetRelationshipDeletedCallback([&client](uint64_t userId, bool isDiscord
239276
240277
Now your friends list will automatically update when relationships change, such as when you add a new friend, accept a friend request, or block a user.
241278
242-
:::warn
243-
Note: Our example will rebuild the friends list from scratch every time a relationship changes. For performance reasons, you will want to update only the changed relationships.
244-
:::
245-
246-
## Step 4: Monitor Changes to Users
247-
248-
To monitor for user changes, we're going using the [`Client::SetUserUpdatedCallback`] function.
249-
250-
This callback will be triggered whenever a user's info is updated, such as name or presence changes (when they go online, offline, or start playing your game).
251-
252-
```cpp
253-
client->SetUserUpdatedCallback([&client](uint64_t userId) {
254-
std::cout << "👤 User updated: " << userId << std::endl;
255-
DisplayFriendsList(*client);
256-
});
257-
```
258-
259-
Now your friends list will automatically update when the presence of a friend changes, such as when they go online, offline or start playing your game.
260-
261-
:::warn
262-
Note: Our example will rebuild the friends list from scratch every time a relationship changes. For performance reasons, you will want to update only the user or relationship that changed.
263-
:::
264-
265279
---
266280
267281
## Next Steps
@@ -292,4 +306,5 @@ Now that you have a unified friends list, you can build on your social features
292306
293307
{/* Autogenerated Reference Links */}
294308
[`Client::GetRelationships`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#ad481849835cd570f0e03adafcf90125d
295-
[`Client::SetUserUpdatedCallback`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#a3559f375165acedc6d6677ef599b3a4a
309+
[`Client::SetUserUpdatedCallback`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#a3559f375165acedc6d6677ef599b3a4a
310+
[`UserHandle`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1UserHandle.html#a3c4e8166cb923dbb7b778ef87b73fd34

0 commit comments

Comments
 (0)