diff options
| author | Mei Boudreau <[email protected]> | 2017-10-07 17:54:08 -0400 |
|---|---|---|
| committer | alex <[email protected]> | 2017-10-07 23:54:08 +0200 |
| commit | 14b92221184fcaca0f4a60a3b31d5a9470b14b1f (patch) | |
| tree | 17c28573651c7a638092017c8ce5d39b8d61ba5d /src/model/guild | |
| parent | Do not to " " if none of the provided delimiters are found (#183) (diff) | |
| download | serenity-14b92221184fcaca0f4a60a3b31d5a9470b14b1f.tar.xz serenity-14b92221184fcaca0f4a60a3b31d5a9470b14b1f.zip | |
Change behaviour of default_channel (#185)
Diffstat (limited to 'src/model/guild')
| -rw-r--r-- | src/model/guild/member.rs | 22 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 11 |
2 files changed, 25 insertions, 8 deletions
diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 70289b9..364b9f4 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -174,6 +174,26 @@ impl Member { .map(|r| r.colour) } + /// Returns the "default channel" of the guild for the member. + /// (This returns the first channel that can be read by the member, if there isn't + /// one returns `None`) + pub fn default_channel(&self) -> Option<Arc<RwLock<GuildChannel>>> { + let guild = match self.guild_id.find() { + Some(guild) => guild, + None => return None, + }; + + let reader = guild.read().unwrap(); + + for (cid, channel) in &reader.channels { + if reader.permissions_for(*cid, self.user.read().unwrap().id).read_messages() { + return Some(channel.clone()); + } + } + + None + } + /// Calculates the member's display name. /// /// The nickname takes priority over the member's username if it exists. @@ -292,7 +312,7 @@ impl Member { let guild = guild.read().unwrap(); - let default_channel = match guild.default_channel() { + let default_channel = match guild.default_channel(self.user.read().unwrap().id) { Some(dc) => dc, None => return Err(From::from(ModelError::ItemMissing)), }; diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index df8bd2c..cec9e63 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -123,13 +123,10 @@ 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 the "default" channel of the guild for the passed user id. + /// (This returns the first channel that can be read by the user, if there isn't one, /// returns `None`) - pub fn default_channel(&self) -> Option<Arc<RwLock<GuildChannel>>> { - let uid = CACHE.read().unwrap().user.id; - + pub fn default_channel(&self, uid: UserId) -> Option<Arc<RwLock<GuildChannel>>> { for (cid, channel) in &self.channels { if self.permissions_for(*cid, uid).read_messages() { return Some(channel.clone()); @@ -163,7 +160,7 @@ impl Guild { None => return Err(Error::Model(ModelError::ItemMissing)), }; - let default_channel = match self.default_channel() { + let default_channel = match self.default_channel(member.user.read().unwrap().id) { Some(dc) => dc, None => return Err(Error::Model(ModelError::ItemMissing)), }; |