Skip to content

Commit

Permalink
Retrieve the member from HTTP if it is missing in the cache (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
arqunis authored Sep 19, 2020
1 parent 88af7cf commit a3f9186
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,31 @@ impl Message {
/// Retrieves a clone of the author's Member instance, if this message was
/// sent in a guild.
///
/// Note that since this clones, it is preferable performance-wise to
/// manually retrieve the guild from the cache and access
/// [`Guild::members`].
/// If the instance cannot be found in the cache, or the `cache` feature is
/// disabled, a HTTP request is performed to retrieve it from Discord's API.
///
/// [`Guild::members`]: ../guild/struct.Guild.html#structfield.members
#[cfg(feature = "cache")]
pub async fn member(&self, cache: impl AsRef<Cache>) -> Option<Member> {
cache.as_ref().member(self.guild_id?, self.author.id).await
/// # Errors
///
/// [`ModelError::ItemMissing`] is returned if [`guild_id`] is `None`.
///
/// [`ModelError::ItemMissing`]: ../error/enum.Error.html#variant.ItemMissing
/// [`guild_id`]: #structfield.guild_id
pub async fn member(&self, cache_http: impl CacheHttp) -> Result<Member> {
let guild_id = match self.guild_id {
Some(guild_id) => guild_id,
None => return Err(Error::Model(ModelError::ItemMissing)),
};

#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
if let Some(member) = cache.member(guild_id, self.author.id).await {
return Ok(member);
}
}
}

cache_http.http().get_member(guild_id.0, self.author.id.0).await
}

/// Checks the length of a string to ensure that it is within Discord's
Expand Down

0 comments on commit a3f9186

Please sign in to comment.