diff options
| author | acdenisSK <[email protected]> | 2017-08-04 17:32:57 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-08-04 17:32:57 +0200 |
| commit | 878684f61fb48a25e117ed32548f78869cb027fc (patch) | |
| tree | 32d331395f50e45feb3d03a2dda74206ece83c1c /src/model | |
| parent | Remove uneccessary map (diff) | |
| download | serenity-878684f61fb48a25e117ed32548f78869cb027fc.tar.xz serenity-878684f61fb48a25e117ed32548f78869cb027fc.zip | |
Deprecate `GuildId::as_channel_id` and add simulation methods for the "default channel" concept
Also fix a little mistake
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/guild/guild_id.rs | 1 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 43 |
2 files changed, 43 insertions, 1 deletions
diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index a199331..5bad3ab 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -15,6 +15,7 @@ use internal::prelude::*; impl GuildId { /// Converts the guild Id into the default channel's Id. #[inline] + #[deprecated(note = "The concept of default channels is no more, use `Guild::default_channel{_guaranteed}` to simulate the concept.")] pub fn as_channel_id(&self) -> ChannelId { ChannelId(self.0) } /// Ban a [`User`] from the guild. All messages by the diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index c46dba6..9e6d6ee 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -124,13 +124,54 @@ pub struct Guild { #[cfg(feature = "model")] impl Guild { #[cfg(feature = "cache")] + /// Returns the "default" channel of the guild. + /// (This returns the first channel that can be read by the bot, if there isn't one, returns `None`) + pub fn default_channel(&self) -> Option<GuildChannel> { + let uid = CACHE.read().unwrap().user.id; + + for (cid, channel) in &self.channels { + if { + let perms = self.permissions_for(*cid, uid); + perms.read_messages() + } { + return Some(channel.read().unwrap().clone()); + } + } + + None + } + + /// Returns the guaranteed "default" channel of the guild. + /// (This returns the first channel that can be read by everyone, if there isn't one, returns `None`) + /// Note however that this is very costy if used in a server with lots of channels, members, or both. + pub fn default_channel_guaranteed(&self) -> Option<GuildChannel> { + for (cid, channel) in &self.channels { + for memid in self.members.keys() { + if { + let perms = self.permissions_for(*cid, *memid); + perms.read_messages() + } { + return Some(channel.read().unwrap().clone()); + } + } + } + + None + } + + #[cfg(feature = "cache")] fn has_perms(&self, mut permissions: Permissions) -> Result<bool> { let member = match self.members.get(&CACHE.read().unwrap().user.id) { Some(member) => member, None => return Err(Error::Model(ModelError::ItemMissing)), }; - let perms = self.permissions_for(ChannelId(self.id.0), member.user.read().unwrap().id); + let default_channel = match self.default_channel() { + Some(dc) => dc, + None => return Err(Error::Model(ModelError::ItemMissing)), + }; + + let perms = self.permissions_for(default_channel.id, member.user.read().unwrap().id); permissions.remove(perms); Ok(permissions.is_empty()) |