diff options
| author | Zeyla Hellyer <[email protected]> | 2017-04-12 21:31:37 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-04-12 21:31:37 -0700 |
| commit | 1561f9e36384a215d2b866a752996f80d36a3ede (patch) | |
| tree | 93d477139af1e96cdb4d167ed0ee67921c27a482 /src/model | |
| parent | Fix `client::rest::delete_webhook_with_token` test (diff) | |
| download | serenity-1561f9e36384a215d2b866a752996f80d36a3ede.tar.xz serenity-1561f9e36384a215d2b866a752996f80d36a3ede.zip | |
Add Shard Id helpers
Add helpers to retrieve the shard Id for guilds, and count how many
guilds are handled by a Shard.
Helpers to retrieve the shard Id of a guild have been added as:
- `Guild::shard_id`
- `GuildId::shard_id`
These are in two forms: one working with the cache feature, and one
without. The function that works with the cache will automatically
retrieve the total number of shards from the Cache, while the uncached
version requires passing in the total number of shards used.
With the cache enabled, this might look like:
```rust
guild.shard_id();
// which calls:
guild_id.shard_id();
```
Without the cache enabled, this looks like:
```rust
let shard_count = 7;
guild.shard_id(shard_count);
// which calls:
guild_id.shard_id(shard_count);
```
These two variants on `Guild` and `GuildId` are helper sugar methods
over the new function `utils::shard_id`, which accepts a `guild_id` and
a `shard_count`:
```rust
use serenity::utils;
assert_eq!(utils::shard_id(81384788765712384, 17), 7);
```
You would use `utils::shard_id` when you have the total number of shards
due to `{Guild,GuildId}::shard_id` unlocking the cache to retrieve the
total number of shards. This avoids some amount of work
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/guild/guild_id.rs | 43 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 42 | ||||
| -rw-r--r-- | src/model/guild/partial_guild.rs | 42 | ||||
| -rw-r--r-- | src/model/invite.rs | 44 |
4 files changed, 171 insertions, 0 deletions
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 1ef9a32..e5a0686 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -420,6 +420,49 @@ impl GuildId { rest::edit_member(self.0, user_id.into().0, &map) } + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// **Note**: When the cache is enabled, this function unlocks the cache to + /// retrieve the total number of shards in use. If you already have the + /// total, consider using [`utils::shard_id`]. + /// + /// [`utils::shard_id`]: ../utils/fn.shard_id.html + #[cfg(feature="cache")] + #[inline] + pub fn shard_id(&self) -> u64 { + ::utils::shard_id(self.0, CACHE.read().unwrap().shard_count) + } + + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// When the cache is not enabled, the total number of shards being used + /// will need to be passed. + /// + /// # Examples + /// + /// Retrieve the Id of the shard for a guild with Id `81384788765712384`, + /// using 17 shards: + /// + /// ```rust + /// use serenity::model::GuildId; + /// use serenity::utils; + /// + /// let guild_id = GuildId(81384788765712384); + /// + /// assert_eq!(guild_id.shard_id(17), 7); + /// ``` + #[cfg(not(feature="cache"))] + #[inline] + pub fn shard_id(&self, shard_count: u64) -> u64 { + ::utils::shard_id(self.0, shard_count) + } + /// Starts an integration sync for the given integration Id. /// /// Requires the [Manage Guild] permission. diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 7299f84..bbf8ecc 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -887,6 +887,48 @@ impl Guild { permissions } + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// **Note**: When the cache is enabled, this function unlocks the cache to + /// retrieve the total number of shards in use. If you already have the + /// total, consider using [`utils::shard_id`]. + /// + /// [`utils::shard_id`]: ../utils/fn.shard_id.html + #[cfg(feature="cache")] + #[inline] + pub fn shard_id(&self) -> u64 { + self.id.shard_id() + } + + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// When the cache is not enabled, the total number of shards being used + /// will need to be passed. + /// + /// # Examples + /// + /// Retrieve the Id of the shard for a guild with Id `81384788765712384`, + /// using 17 shards: + /// + /// ```rust,ignore + /// use serenity::utils; + /// + /// // assumes a `guild` has already been bound + /// + /// assert_eq!(guild.shard_id(17), 7); + /// ``` + #[cfg(not(feature="cache"))] + #[inline] + pub fn shard_id(&self, shard_count: u64) -> u64 { + self.id.shard_id(shard_count) + } + /// Returns the formatted URL of the guild's splash image, if one exists. pub fn splash_url(&self) -> Option<String> { self.icon.as_ref().map(|icon| diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs index 947de50..b6d027a 100644 --- a/src/model/guild/partial_guild.rs +++ b/src/model/guild/partial_guild.rs @@ -404,6 +404,48 @@ impl PartialGuild { self.id.move_member(user_id, channel_id) } + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// **Note**: When the cache is enabled, this function unlocks the cache to + /// retrieve the total number of shards in use. If you already have the + /// total, consider using [`utils::shard_id`]. + /// + /// [`utils::shard_id`]: ../utils/fn.shard_id.html + #[cfg(feature="cache")] + #[inline] + pub fn shard_id(&self) -> u64 { + self.id.shard_id() + } + + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// When the cache is not enabled, the total number of shards being used + /// will need to be passed. + /// + /// # Examples + /// + /// Retrieve the Id of the shard for a guild with Id `81384788765712384`, + /// using 17 shards: + /// + /// ```rust,ignore + /// use serenity::utils; + /// + /// // assumes a `guild` has already been bound + /// + /// assert_eq!(guild.shard_id(17), 7); + /// ``` + #[cfg(not(feature="cache"))] + #[inline] + pub fn shard_id(&self, shard_count: u64) -> u64 { + self.id.shard_id(shard_count) + } + /// Returns the formatted URL of the guild's splash image, if one exists. pub fn splash_url(&self) -> Option<String> { self.icon.as_ref().map(|icon| diff --git a/src/model/invite.rs b/src/model/invite.rs index d041d55..338d90c 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -112,6 +112,50 @@ pub struct InviteGuild { pub splash_hash: Option<String>, } +impl InviteGuild { + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// **Note**: When the cache is enabled, this function unlocks the cache to + /// retrieve the total number of shards in use. If you already have the + /// total, consider using [`utils::shard_id`]. + /// + /// [`utils::shard_id`]: ../utils/fn.shard_id.html + #[cfg(feature="cache")] + #[inline] + pub fn shard_id(&self) -> u64 { + self.id.shard_id() + } + + /// Returns the Id of the shard associated with the guild. + /// + /// When the cache is enabled this will automatically retrieve the total + /// number of shards. + /// + /// When the cache is not enabled, the total number of shards being used + /// will need to be passed. + /// + /// # Examples + /// + /// Retrieve the Id of the shard for a guild with Id `81384788765712384`, + /// using 17 shards: + /// + /// ```rust,ignore + /// use serenity::utils; + /// + /// // assumes a `guild` has already been bound + /// + /// assert_eq!(guild.shard_id(17), 7); + /// ``` + #[cfg(not(feature="cache"))] + #[inline] + pub fn shard_id(&self, shard_count: u64) -> u64 { + self.id.shard_id(shard_count) + } +} + /// Detailed information about an invite. /// This information can only be retrieved by anyone with the [Manage Guild] /// permission. Otherwise, a minimal amount of information can be retrieved via |