djs_rest/endpoints/lobby.rs
1//! [Lobby endpoints](https://docs.discord.com/developers/resources/lobby): groups of players
2//! of an app's game (Discord Social SDK), their members, messages and linked channels.
3//!
4//! Most endpoints take the bot token. Those acting as a player — creating or joining a lobby by
5//! its secret, leaving it, linking a channel, sending and reading messages and inviting
6//! oneself — need a client built with [`bearer_token`](crate::RestBuilder::bearer_token) and
7//! the player's access token (with the `sdk.social_layer` scope where noted).
8
9use djs_model::{
10 id::{ChannelId, LobbyId, MessageId, UserId},
11 lobby::{Lobby, LobbyInvite, LobbyMember, LobbyMessage},
12 rest::{
13 AddLobbyMember, BulkLobbyMember, CreateLobby, CreateOrJoinLobby, GetLobbyMessages,
14 LobbyMetadata, ModifyLobby, SendLobbyMessage,
15 },
16};
17use serde::Serialize;
18
19use crate::client::boxed;
20use crate::{Error, Request, Rest, Route};
21
22/// The body of Link Channel to Lobby; without a channel it unlinks.
23#[derive(Serialize)]
24struct ChannelLinking {
25 #[serde(skip_serializing_if = "Option::is_none")]
26 channel_id: Option<ChannelId>,
27}
28
29/// Lobbies.
30impl Rest {
31 /// [Create Lobby](https://docs.discord.com/developers/resources/lobby#create-lobby):
32 /// creates a lobby with the given members. Social SDK clients cannot join or leave it
33 /// themselves.
34 ///
35 /// discord.js counterpart: none.
36 ///
37 /// # Examples
38 ///
39 /// ```no_run
40 /// # async fn example(rest: djs_rest::Rest, players: Vec<djs_model::id::UserId>) -> Result<(), djs_rest::Error> {
41 /// use djs_model::rest::{CreateLobby, LobbyMemberData};
42 ///
43 /// let mut body = CreateLobby::default();
44 /// body.members = players.into_iter().map(LobbyMemberData::new).collect();
45 /// let lobby = rest.create_lobby(&body).await?;
46 /// println!("Lobby {} has {} members", lobby.id, lobby.members.len());
47 /// # Ok(())
48 /// # }
49 /// ```
50 ///
51 /// # Errors
52 ///
53 /// See [`Rest::request`].
54 pub fn create_lobby<'a>(
55 &'a self,
56 body: &'a CreateLobby,
57 ) -> impl Future<Output = Result<Lobby, Error>> + Send + 'a {
58 boxed(async move {
59 self.call(Request::new(Route::CreateLobby).json(body)?)
60 .await
61 })
62 }
63
64 /// [Create or Join Lobby](https://docs.discord.com/developers/resources/lobby#create-or-join-lobby):
65 /// joins the application's lobby with the body's secret, creating it if there is none.
66 /// Needs the player's access token with the `sdk.social_layer` scope.
67 ///
68 /// discord.js counterpart: none.
69 ///
70 /// # Errors
71 ///
72 /// See [`Rest::request`].
73 pub fn create_or_join_lobby<'a>(
74 &'a self,
75 body: &'a CreateOrJoinLobby,
76 ) -> impl Future<Output = Result<Lobby, Error>> + Send + 'a {
77 boxed(async move {
78 self.call(Request::new(Route::CreateOrJoinLobby).json(body)?)
79 .await
80 })
81 }
82
83 /// [Get Lobby](https://docs.discord.com/developers/resources/lobby#get-lobby): returns a
84 /// lobby with its members.
85 ///
86 /// discord.js counterpart: none.
87 ///
88 /// # Errors
89 ///
90 /// See [`Rest::request`].
91 pub fn get_lobby(
92 &self,
93 lobby_id: LobbyId,
94 ) -> impl Future<Output = Result<Lobby, Error>> + Send + '_ {
95 boxed(async move { self.call(Request::new(Route::GetLobby { lobby_id })).await })
96 }
97
98 /// [Modify Lobby](https://docs.discord.com/developers/resources/lobby#modify-lobby):
99 /// changes a lobby's metadata, members or idle timeout and returns it.
100 ///
101 /// discord.js counterpart: none.
102 ///
103 /// # Errors
104 ///
105 /// See [`Rest::request`].
106 pub fn modify_lobby<'a>(
107 &'a self,
108 lobby_id: LobbyId,
109 body: &'a ModifyLobby,
110 ) -> impl Future<Output = Result<Lobby, Error>> + Send + 'a {
111 boxed(async move {
112 self.call(Request::new(Route::ModifyLobby { lobby_id }).json(body)?)
113 .await
114 })
115 }
116
117 /// [Delete Lobby](https://docs.discord.com/developers/resources/lobby#delete-lobby):
118 /// deletes a lobby; deleting one that is already gone succeeds too.
119 ///
120 /// discord.js counterpart: none.
121 ///
122 /// # Errors
123 ///
124 /// See [`Rest::request`].
125 pub fn delete_lobby(
126 &self,
127 lobby_id: LobbyId,
128 ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
129 boxed(async move {
130 self.request_ignoring_body(Request::new(Route::DeleteLobby { lobby_id }))
131 .await
132 })
133 }
134}
135
136/// Lobby members.
137impl Rest {
138 /// [Add a Member to a Lobby](https://docs.discord.com/developers/resources/lobby#add-a-member-to-a-lobby):
139 /// adds a user to a lobby, or updates the member if they are in it already.
140 ///
141 /// discord.js counterpart: none.
142 ///
143 /// # Errors
144 ///
145 /// See [`Rest::request`].
146 pub fn add_lobby_member<'a>(
147 &'a self,
148 lobby_id: LobbyId,
149 user_id: UserId,
150 body: &'a AddLobbyMember,
151 ) -> impl Future<Output = Result<LobbyMember, Error>> + Send + 'a {
152 boxed(async move {
153 self.call(Request::new(Route::AddLobbyMember { lobby_id, user_id }).json(body)?)
154 .await
155 })
156 }
157
158 /// [Bulk Update Lobby Members](https://docs.discord.com/developers/resources/lobby#bulk-update-lobby-members):
159 /// adds, updates or removes up to 25 members in one request (D-134) and returns the added
160 /// and updated ones. Users that fail permission checks or are in too many lobbies are
161 /// silently left out.
162 ///
163 /// discord.js counterpart: none.
164 ///
165 /// # Examples
166 ///
167 /// ```no_run
168 /// # async fn example(rest: djs_rest::Rest, lobby_id: djs_model::id::LobbyId, joined: djs_model::id::UserId, left: djs_model::id::UserId) -> Result<(), djs_rest::Error> {
169 /// use djs_model::rest::{BulkLobbyMember, LobbyMemberData};
170 ///
171 /// rest.bulk_update_lobby_members(
172 /// lobby_id,
173 /// &[
174 /// BulkLobbyMember::upsert(LobbyMemberData::new(joined)),
175 /// BulkLobbyMember::remove(left),
176 /// ],
177 /// )
178 /// .await?;
179 /// # Ok(())
180 /// # }
181 /// ```
182 ///
183 /// # Errors
184 ///
185 /// See [`Rest::request`]; `10013` Unknown User for a user Discord does not know.
186 pub fn bulk_update_lobby_members<'a>(
187 &'a self,
188 lobby_id: LobbyId,
189 members: &'a [BulkLobbyMember],
190 ) -> impl Future<Output = Result<Vec<LobbyMember>, Error>> + Send + 'a {
191 boxed(async move {
192 self.call(Request::new(Route::BulkUpdateLobbyMembers { lobby_id }).json(members)?)
193 .await
194 })
195 }
196
197 /// [Remove a Member from a Lobby](https://docs.discord.com/developers/resources/lobby#remove-a-member-from-a-lobby):
198 /// removes a user from a lobby; removing one who already left succeeds too.
199 ///
200 /// discord.js counterpart: none.
201 ///
202 /// # Errors
203 ///
204 /// See [`Rest::request`]; an error for a lobby that does not exist.
205 pub fn remove_lobby_member(
206 &self,
207 lobby_id: LobbyId,
208 user_id: UserId,
209 ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
210 boxed(async move {
211 self.request_ignoring_body(Request::new(Route::RemoveLobbyMember { lobby_id, user_id }))
212 .await
213 })
214 }
215
216 /// [Leave Lobby](https://docs.discord.com/developers/resources/lobby#leave-lobby): removes
217 /// the player from a lobby. Needs the player's access token.
218 ///
219 /// discord.js counterpart: none.
220 ///
221 /// # Errors
222 ///
223 /// See [`Rest::request`]; an error for a lobby that does not exist.
224 pub fn leave_lobby(
225 &self,
226 lobby_id: LobbyId,
227 ) -> impl Future<Output = Result<(), Error>> + Send + '_ {
228 boxed(async move {
229 self.request_ignoring_body(Request::new(Route::LeaveLobby { lobby_id }))
230 .await
231 })
232 }
233}
234
235/// Linked channels and lobby invites.
236impl Rest {
237 /// [Link Channel to Lobby](https://docs.discord.com/developers/resources/lobby#link-channel-to-lobby):
238 /// links a guild text channel to the lobby, so their messages are shared. Needs the access
239 /// token of a member with [`CAN_LINK_LOBBY`](djs_model::lobby::LobbyMemberFlags::CAN_LINK_LOBBY).
240 ///
241 /// discord.js counterpart: none.
242 ///
243 /// # Errors
244 ///
245 /// See [`Rest::request`].
246 pub fn link_channel_to_lobby(
247 &self,
248 lobby_id: LobbyId,
249 channel_id: ChannelId,
250 ) -> impl Future<Output = Result<Lobby, Error>> + Send + '_ {
251 boxed(async move {
252 let body = ChannelLinking {
253 channel_id: Some(channel_id),
254 };
255 self.call(Request::new(Route::LinkChannelToLobby { lobby_id }).json(&body)?)
256 .await
257 })
258 }
259
260 /// [Unlink Channel from Lobby](https://docs.discord.com/developers/resources/lobby#unlink-channel-from-lobby):
261 /// removes the lobby's linked channel. Needs the access token of a member with
262 /// [`CAN_LINK_LOBBY`](djs_model::lobby::LobbyMemberFlags::CAN_LINK_LOBBY).
263 ///
264 /// discord.js counterpart: none.
265 ///
266 /// # Errors
267 ///
268 /// See [`Rest::request`].
269 pub fn unlink_channel_from_lobby(
270 &self,
271 lobby_id: LobbyId,
272 ) -> impl Future<Output = Result<Lobby, Error>> + Send + '_ {
273 boxed(async move {
274 let body = ChannelLinking { channel_id: None };
275 self.call(Request::new(Route::UnlinkChannelFromLobby { lobby_id }).json(&body)?)
276 .await
277 })
278 }
279
280 /// [Create Lobby Channel Invite for Self](https://docs.discord.com/developers/resources/lobby#create-lobby-channel-invite-for-self):
281 /// a single-use invite to the guild of the lobby's linked channel for the player, valid
282 /// for one hour. Needs the player's access token with the `sdk.social_layer` scope.
283 ///
284 /// discord.js counterpart: none.
285 ///
286 /// # Errors
287 ///
288 /// See [`Rest::request`]; an error when the lobby has no linked channel.
289 pub fn create_lobby_channel_invite_for_self(
290 &self,
291 lobby_id: LobbyId,
292 ) -> impl Future<Output = Result<LobbyInvite, Error>> + Send + '_ {
293 boxed(async move {
294 self.call(Request::new(Route::CreateLobbyChannelInviteForSelf {
295 lobby_id,
296 }))
297 .await
298 })
299 }
300
301 /// [Create Lobby Channel Invite for User](https://docs.discord.com/developers/resources/lobby#create-lobby-channel-invite-for-user):
302 /// a single-use invite to the guild of the lobby's linked channel for a member, valid for
303 /// one hour.
304 ///
305 /// discord.js counterpart: none.
306 ///
307 /// # Errors
308 ///
309 /// See [`Rest::request`]; an error when the lobby has no linked channel.
310 pub fn create_lobby_channel_invite_for_user(
311 &self,
312 lobby_id: LobbyId,
313 user_id: UserId,
314 ) -> impl Future<Output = Result<LobbyInvite, Error>> + Send + '_ {
315 boxed(async move {
316 self.call(Request::new(Route::CreateLobbyChannelInviteForUser {
317 lobby_id,
318 user_id,
319 }))
320 .await
321 })
322 }
323}
324
325/// Lobby messages.
326impl Rest {
327 /// [Send Lobby Message](https://docs.discord.com/developers/resources/lobby#send-lobby-message):
328 /// sends a message to the lobby (and its linked channel). Needs the access token of a
329 /// member with the `sdk.social_layer` scope.
330 ///
331 /// discord.js counterpart: none.
332 ///
333 /// # Errors
334 ///
335 /// See [`Rest::request`].
336 pub fn send_lobby_message<'a>(
337 &'a self,
338 lobby_id: LobbyId,
339 body: &'a SendLobbyMessage,
340 ) -> impl Future<Output = Result<LobbyMessage, Error>> + Send + 'a {
341 boxed(async move {
342 self.call(Request::new(Route::SendLobbyMessage { lobby_id }).json(body)?)
343 .await
344 })
345 }
346
347 /// [Get Lobby Messages](https://docs.discord.com/developers/resources/lobby#get-lobby-messages):
348 /// returns the lobby's most recent messages (50 by default). Needs the access token of a
349 /// member with the `sdk.social_layer` scope.
350 ///
351 /// discord.js counterpart: none.
352 ///
353 /// # Errors
354 ///
355 /// See [`Rest::request`].
356 pub fn get_lobby_messages<'a>(
357 &'a self,
358 lobby_id: LobbyId,
359 query: &'a GetLobbyMessages,
360 ) -> impl Future<Output = Result<Vec<LobbyMessage>, Error>> + Send + 'a {
361 boxed(async move {
362 self.call(Request::new(Route::GetLobbyMessages { lobby_id }).query(query))
363 .await
364 })
365 }
366
367 /// [Update Lobby Message Moderation Metadata](https://docs.discord.com/developers/resources/lobby#update-lobby-message-moderation-metadata):
368 /// sets the app's moderation decision for a message (up to 5 keys of at most 1024
369 /// characters, values of at most 2000), delivered to Social SDK clients as an update.
370 ///
371 /// discord.js counterpart: none.
372 ///
373 /// # Errors
374 ///
375 /// See [`Rest::request`].
376 pub fn update_lobby_message_moderation_metadata<'a>(
377 &'a self,
378 lobby_id: LobbyId,
379 message_id: MessageId,
380 metadata: &'a LobbyMetadata,
381 ) -> impl Future<Output = Result<(), Error>> + Send + 'a {
382 boxed(async move {
383 self.request_ignoring_body(
384 Request::new(Route::UpdateLobbyMessageModerationMetadata {
385 lobby_id,
386 message_id,
387 })
388 .json(metadata)?,
389 )
390 .await
391 })
392 }
393}