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/client | |
| 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/client')
| -rw-r--r-- | src/client/gateway/shard.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/client/gateway/shard.rs b/src/client/gateway/shard.rs index 4706397..d26cc01 100644 --- a/src/client/gateway/shard.rs +++ b/src/client/gateway/shard.rs @@ -18,6 +18,7 @@ use ::internal::prelude::*; use ::internal::ws_impl::{ReceiverExt, SenderExt}; use ::model::event::{Event, GatewayEvent, ReadyEvent}; use ::model::{Game, GuildId, OnlineStatus}; +use ::utils; #[cfg(feature="cache")] use ::client::CACHE; @@ -482,7 +483,7 @@ impl Shard { Ok(()) } - /// Requests that one or multiple [`Guild`]s be synced. + /// Requests that one or multiple [`Guild`]s be chunked. /// /// This will ask Discord to start sending member chunks for large guilds /// (250 members+). If a guild is over 250 members, then a full member list @@ -507,6 +508,28 @@ impl Shard { let _ = self.keepalive_channel.send(GatewayStatus::SendMessage(msg)); } + /// Calculates the number of guilds that the shard is responsible for. + /// + /// If sharding is not being used (i.e. 1 shard), then the total number of + /// guilds in the [`Cache`] will be used. + /// + /// **Note**: Requires the `cache` feature be enabled. + /// + /// [`Cache`]: ../../ext/cache/struct.Cache.html + #[cfg(feature="cache")] + pub fn guilds_handled(&self) -> u16 { + let cache = CACHE.read().unwrap(); + + if let Some((shard_id, shard_count)) = self.shard_info.map(|s| (s[0], s[1])) { + cache.guilds + .keys() + .filter(|guild_id| utils::shard_id(guild_id.0, shard_count) == shard_id) + .count() as u16 + } else { + cache.guilds.len() as u16 + } + } + #[allow(unused_variables)] fn handle_dispatch(&mut self, event: &Event) { #[cfg(feature="voice")] |