diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/context.rs | 28 | ||||
| -rw-r--r-- | src/client/rest/mod.rs | 48 | ||||
| -rw-r--r-- | src/model/user.rs | 7 |
3 files changed, 74 insertions, 9 deletions
diff --git a/src/client/context.rs b/src/client/context.rs index a5c9ffa..bc983bf 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -4,7 +4,7 @@ use std::fmt::Write as FmtWrite; use std::io::Read; use std::sync::{Arc, Mutex}; use super::gateway::Shard; -use super::rest; +use super::rest::{self, GuildPagination}; use super::login_type::LoginType; use ::utils::builder::{ CreateEmbed, @@ -1050,9 +1050,29 @@ impl Context { rest::get_guild_prune_count(guild_id.into().0, map) } - /// Gets all guilds that the current user is in. - pub fn get_guilds(&self) -> Result<Vec<GuildInfo>> { - rest::get_guilds() + /// Gets a paginated list of guilds that the current user is in. + /// + /// The `limit` has a maximum value of 100. + /// + /// See also: [`CurrentUser::guilds`]. + /// + /// # Examples + /// + /// Get the first 10 guilds after the current [`Message`]'s guild's Id: + /// + /// ```rust,ignore + /// use serenity::client::rest::GuildPagination; + /// + /// // assuming you are in a context + /// + /// let guild_id = message.guild_id.unwrap(); + /// context.get_guilds(GuildPagination::After(guild_id, 10)).unwrap(); + /// ``` + /// + /// [`CurrentUser::guilds`]: ../model/struct.CurrentUser.html#method.guilds + /// [`Message`]: ../model/struct.Message.html + pub fn get_guilds(&self, target: GuildPagination, limit: u8) -> Result<Vec<GuildInfo>> { + rest::get_guilds(target, limit as u64) } /// Gets all integrations of a guild via the given Id. diff --git a/src/client/rest/mod.rs b/src/client/rest/mod.rs index da16671..a325f15 100644 --- a/src/client/rest/mod.rs +++ b/src/client/rest/mod.rs @@ -1125,11 +1125,38 @@ pub fn get_guild_webhooks(guild_id: u64) -> Result<Vec<Webhook>> { decode_array(try!(serde_json::from_reader(response)), Webhook::decode) } -/// Gets all guilds we're connected to. -pub fn get_guilds() -> Result<Vec<GuildInfo>> { - let response = request!(Route::UsersMeGuilds, - get, - "/users/@me/guilds"); +/// Gets a paginated list of the current user's guilds. +/// +/// The `limit` has a maximum value of 100. +/// +/// [Discord's documentation][docs] +/// +/// # Examples +/// +/// Get the first 10 guilds after a certain guild's Id: +/// +/// ```rust,no_run +/// use serenity::rest::{GuildPagination, get_guilds}; +/// +/// let guild_id = 81384788765712384; +/// +/// let guilds = get_guilds(GuildPagination::after(guild_id), 10).unwrap(); +/// ``` +/// +/// [docs]: https://discordapp.com/developers/docs/resources/user#get-current-user-guilds +pub fn get_guilds(target: GuildPagination, limit: u64) -> Result<Vec<GuildInfo>> { + let mut uri = format!("/users/@me/guilds?limit={}", limit); + + match target { + GuildPagination::After(id) => { + try!(write!(uri, "&after={}", id)); + }, + GuildPagination::Before(id) => { + try!(write!(uri, "&before={}", id)); + }, + } + + let response = request!(Route::UsersMeGuilds, get, "{}", uri); decode_array(try!(serde_json::from_reader(response)), GuildInfo::decode) } @@ -1534,3 +1561,14 @@ fn verify(expected_status_code: u16, Err(Error::Client(ClientError::UnexpectedStatusCode(response.status))) } + +/// Representation of the method of a query to send for the [`get_guilds`] +/// function. +/// +/// [`get_guilds`]: fn.get_guilds.html +pub enum GuildPagination { + /// The Id to get the guilds after. + After(GuildId), + /// The Id to get the guilds before. + Before(GuildId), +} diff --git a/src/model/user.rs b/src/model/user.rs index d7ea416..88a6f9d 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -5,11 +5,13 @@ use super::{ FriendSourceFlags, GuildContainer, GuildId, + GuildInfo, Mention, RoleId, UserSettings, User, }; +use ::client::rest::GuildPagination; use ::internal::prelude::*; use ::utils::decode_array; @@ -31,6 +33,11 @@ impl CurrentUser { self.avatar.as_ref().map(|av| format!(cdn!("/avatars/{}/{}.jpg"), self.id, av)) } + + /// Gets a list of guilds that the current user is in. + pub fn guilds(&self) -> Result<Vec<GuildInfo>> { + rest::get_guilds(GuildPagination::After(GuildId(0)), 100) + } } impl User { |