Skip to content

Commit 1f2f730

Browse files
Social SDK: Update UFL Guide with new helper functions (#7689)
* Social SDK: Update UFL Guide with new helper functions Update documentation to show how to use `Client::GetRelationshipsByGroup` and `Client::SetRelationshipGroupsUpdatedCallback`. * Apply suggestion from @anthonydiscord Review upadate Co-authored-by: Anthony <[email protected]> --------- Co-authored-by: Anthony <[email protected]>
1 parent d29b668 commit 1f2f730

File tree

1 file changed

+154
-14
lines changed

1 file changed

+154
-14
lines changed

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

Lines changed: 154 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ Before you begin, make sure you have:
2525

2626
### Implementing a Unified Friends List
2727

28-
The Discord friend list is ultimately constructed from two entities: Relationships, and Users. You can query Relationships API to find everyone a user is a friend with, and the Users API to find the necessary extra information for rendering the list, such as whether they are online or not.
28+
The Discord friend list is ultimately constructed from two entities: Relationships, and Users. You can query
29+
Relationships API to find everyone a user is a friend with, and the Users API to find the necessary extra information
30+
for rendering the list, such as whether they are online or not.
2931

3032
### Relationships
3133

32-
[Relationships](/docs/discord-social-sdk/development-guides/managing-relationships) are how Discord models friends, friend requests, and more. All relationships for the current user are loaded when the Client connects. Each relationship has a target user id, and a type, such as `Friend`, `PendingOutgoing`, or `Blocked`. The set of all relationships can be queried with [`Client::GetRelationships`].
34+
[Relationships](/docs/discord-social-sdk/development-guides/managing-relationships) are how Discord models friends,
35+
friend requests, and more. All relationships for the current user are loaded when the Client connects. Each
36+
relationship has a target user id, and a type, such as `Friend`, `PendingOutgoing`, or `Blocked`.
3337

34-
To allow users to manage their relationships in your game, you should provide a way to accept or reject friend requests, block users, and manage pending requests. See [Development Guide: Managing Relationships in Your Game](/docs/discord-social-sdk/development-guides/managing-relationships) for implementation details.
38+
To allow users to manage their relationships in your game, you should provide a way to accept or reject friend
39+
requests, block users, and manage pending requests. See [Development Guide: Managing Relationships in Your Game](/docs/discord-social-sdk/development-guides/managing-relationships) for implementation details.
3540

3641
### Users
3742

@@ -50,13 +55,144 @@ The SDK will only display activities associated with the current game, meaning y
5055

5156
See our [Design Guidelines: Status & Rich Presence](/docs/discord-social-sdk/design-guidelines/status-rich-presence) for best practices on displaying presence information.
5257

53-
Let's combine to create a unified friends list.
58+
There are two ways in which you can create a unified friends list in your game:
59+
60+
1. Using the SDK Unified Friends List helper functions, which automatically group and sort
61+
relationships and users for you.
62+
2. Directly retrieving relationships and users from the SDK, and sorting manually.
63+
64+
## Approach 1: Using SDK Unified Friends List Helper Functions
65+
66+
:::info
67+
This approach is recommended as it significantly reduces the amount of code you need to write and maintain compared to
68+
manually fetching and organizing relationships, while ensuring your friends list follows Discord's best practices.
69+
:::
70+
71+
The Discord Social SDK provides built-in helper functions that automatically group and sort your friends list according
72+
to Discord's [recommended design guidelines](/docs/discord-social-sdk/design-guidelines/unified-friends-list).
73+
This approach is generally simpler and more maintainable than manually fetching and organizing relationships.
74+
75+
The SDK automatically organizes friends into the three groups we find via [`RelationshipGroupType`]:
76+
- `OnlinePlayingGame`: Friends who are online and currently playing your game
77+
- `OnlineElsewhere`: Friends who are online but not playing your game
78+
- `Offline`: Friends who are offline
79+
80+
### Step 1: Display the Unified Friends List
81+
82+
The [`Client::GetRelationshipsByGroup`] method returns a pre-sorted list of relationships for a specific group type.
83+
This eliminates the need to manually filter, categorize, and sort friends yourself. The SDK handles all the logic
84+
for determining which group each friend belongs to based on their online status and game activity, and
85+
automatically sorts users within each group (for example, users who have played your game are moved to the top of
86+
the OnlineElsewhere group).
87+
88+
Let's create a function that uses the SDK helper functions to display a properly organized friends list:
89+
90+
```cpp
91+
void DisplayUnifiedFriendsList(const std::shared_ptr<discordpp::Client> &client) {
92+
// Get friends playing the game
93+
const auto onlineInGame = client->GetRelationshipsByGroup(
94+
discordpp::RelationshipGroupType::OnlinePlayingGame
95+
);
96+
97+
// Get friends online elsewhere
98+
const auto onlineElsewhere = client->GetRelationshipsByGroup(
99+
discordpp::RelationshipGroupType::OnlineElsewhere
100+
);
101+
102+
// Get offline friends
103+
const auto offline = client->GetRelationshipsByGroup(
104+
discordpp::RelationshipGroupType::Offline
105+
);
106+
107+
// Display "Online - GameTitle" Friends
108+
std::cout << "\n=== Online - GameTitle (" << onlineInGame.size() << ") ===\n";
109+
for (const auto& relationship : onlineInGame) {
110+
auto user = relationship.User();
111+
if (user) {
112+
std::string displayStr = "🟣 " + user->DisplayName();
113+
114+
// Add Discord friend indicator
115+
if (relationship.DiscordRelationshipType() == discordpp::RelationshipType::Friend) {
116+
displayStr += " 👾";
117+
}
118+
119+
// Add game friend indicator
120+
if (relationship.GameRelationshipType() == discordpp::RelationshipType::Friend) {
121+
displayStr += " 🎮";
122+
}
123+
124+
std::cout << displayStr << "\n";
125+
}
126+
}
127+
128+
// Display "Online - Elsewhere" Friends
129+
std::cout << "\n=== Online - Elsewhere (" << onlineElsewhere.size() << ") ===\n";
130+
for (const auto& relationship : onlineElsewhere) {
131+
auto user = relationship.User();
132+
if (user) {
133+
std::string displayStr = "🟢 " + user->DisplayName();
134+
135+
// Add Discord friend indicator
136+
if (relationship.DiscordRelationshipType() == discordpp::RelationshipType::Friend) {
137+
displayStr += " 👾";
138+
}
139+
140+
// Add game friend indicator
141+
if (relationship.GameRelationshipType() == discordpp::RelationshipType::Friend) {
142+
displayStr += " 🎮";
143+
}
144+
145+
std::cout << displayStr << "\n";
146+
}
147+
}
148+
149+
// Display "Offline" Friends
150+
std::cout << "\n=== Offline (" << offline.size() << ") ===\n";
151+
for (const auto& relationship : offline) {
152+
auto user = relationship.User();
153+
if (user) {
154+
std::string displayStr = "" + user->DisplayName();
155+
156+
// Add Discord friend indicator
157+
if (relationship.DiscordRelationshipType() == discordpp::RelationshipType::Friend) {
158+
displayStr += " 👾";
159+
}
160+
161+
// Add game friend indicator
162+
if (relationship.GameRelationshipType() == discordpp::RelationshipType::Friend) {
163+
displayStr += " 🎮";
164+
}
165+
166+
std::cout << displayStr << "\n";
167+
}
168+
}
169+
}
170+
```
171+
172+
### Step 2: Set Up Automatic Updates
173+
174+
To keep your friends list up-to-date automatically, use the [`Client::SetRelationshipGroupsUpdatedCallback`]. This
175+
callback is triggered whenever any change occurs that might affect the friends list grouping, such as a friend going
176+
online or offline, or when a relationship changes, such as when you accept a friend request, or block a user.
177+
178+
```cpp
179+
// Set up the unified friends list update callback
180+
client->SetRelationshipGroupsUpdatedCallback([&client](const uint64_t userId) {
181+
std::cout << "👥 Friends list updated for user: " << userId << std::endl;
182+
DisplayUnifiedFriendsList(client);
183+
});
184+
```
54185
55186
---
56187
57-
## Step 1: Fetch Relationships
188+
## Approach 2: Manually Fetching Relationships and Users
189+
190+
In this section we'll show a more manual method which gives you more control over how the friends list is displayed in your game.
191+
192+
### Step 1: Fetch Relationships
58193
59-
First, let's create a function to see what kind of relationships and information we have to work with:
194+
First, let's create a function that utilises [`Client::GetRelationships`] to query all the relationships and
195+
user information we for our account:
60196
61197
```cpp
62198
void DisplayFriendsList(discordpp::Client& client) {
@@ -122,7 +258,7 @@ This will output the raw relationship data to the console. You can use this info
122258
123259
---
124260
125-
## Step 2: Organize Relationships
261+
### Step 2: Organize Relationships
126262
127263
Based on our design guidelines for a [Unified Friends List](/docs/discord-social-sdk/design-guidelines/unified-friends-list), you should separate the player's friends list into three sections: `Online - GameTitle`, `Online - Elsewhere`, and `Offline`.
128264
@@ -219,7 +355,7 @@ void DisplayFriendsList(std::shared_ptr<discordpp::Client> client) {
219355

220356
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`.
221357

222-
## Step 3: Monitor Changes to Users
358+
### Step 3: Monitor Changes to Users
223359

224360
To monitor for user changes, we're going using the [`Client::SetUserUpdatedCallback`] function.
225361

@@ -242,7 +378,7 @@ user list, or similar operations.
242378
:::
243379
---
244380
245-
## Step 4: Monitor Changes in Relationships
381+
### Step 4: Monitor Changes in Relationships
246382
247383
Let us setup two callbacks to handle relationship updates.
248384
@@ -251,7 +387,7 @@ These examples rebuild the friends list from scratch every time a relationship c
251387
recommend maintaining a collection of [`UserHandle`] objects and adding and removing them appropriately.
252388
:::
253389
254-
### Relationship Created Callback
390+
#### Relationship Created Callback
255391
256392
This can happen when a user sends or accepts a friend invite, or blocks a user.
257393
@@ -266,7 +402,7 @@ client->SetRelationshipCreatedCallback([&client](uint64_t userId, bool isDiscord
266402
});
267403
```
268404

269-
### Relationship Deleted Callback
405+
#### Relationship Deleted Callback
270406

271407
This can happen when a user rejects a friend request or removes a friend.
272408

@@ -303,11 +439,15 @@ Now that you have a unified friends list, you can build on your social features
303439
304440
## Change Log
305441
306-
| Date | Changes |
307-
|----------------|-----------------|
308-
| March 17, 2025 | Initial release |
442+
| Date | Changes |
443+
|----------------|------------------------|
444+
| March 17, 2025 | Initial release |
445+
| July 17, 2025 | Add UFL helper methods |
309446
310447
{/* Autogenerated Reference Links */}
311448
[`Client::GetRelationships`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#ad481849835cd570f0e03adafcf90125d
449+
[`Client::GetRelationshipsByGroup`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#a9f7898d3f3d1ec92b06c662df70746d5
450+
[`Client::SetRelationshipGroupsUpdatedCallback`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#af12441ef091298f968075b7190851098
312451
[`Client::SetUserUpdatedCallback`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1Client.html#a3559f375165acedc6d6677ef599b3a4a
452+
[`RelationshipGroupType`]: https://discord.com/developers/docs/social-sdk/namespacediscordpp.html#a503ed2f7b0bfbd435321a0e8b1dfba35
313453
[`UserHandle`]: https://discord.com/developers/docs/social-sdk/classdiscordpp_1_1UserHandle.html#a587bcc838e42dc5c56f840a350070707

0 commit comments

Comments
 (0)