aboutsummaryrefslogtreecommitdiff
path: root/src/model
diff options
context:
space:
mode:
authorLakelezz <[email protected]>2017-08-29 18:53:50 +0200
committerZeyla Hellyer <[email protected]>2017-08-29 09:53:50 -0700
commitf6fcf32e7f62dfc207ac2f9f293f804446ea3423 (patch)
treec0fd79b80cf0178db9c95653e057feb000377456 /src/model
parentAdd find and find_n (#153) (diff)
downloadserenity-f6fcf32e7f62dfc207ac2f9f293f804446ea3423.tar.xz
serenity-f6fcf32e7f62dfc207ac2f9f293f804446ea3423.zip
Make role references attainable via name
Diffstat (limited to 'src/model')
-rw-r--r--src/model/guild/mod.rs35
-rw-r--r--src/model/guild/partial_guild.rs34
2 files changed, 69 insertions, 0 deletions
diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs
index 5ffbf4e..a690003 100644
--- a/src/model/guild/mod.rs
+++ b/src/model/guild/mod.rs
@@ -1038,6 +1038,41 @@ impl Guild {
/// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html
#[inline]
pub fn webhooks(&self) -> Result<Vec<Webhook>> { self.id.webhooks() }
+
+ /// Obtain a reference to a role by its name.
+ ///
+ /// **Note**: If two or more roles have the same name, obtained reference will be one of
+ /// them.
+ ///
+ /// # Examples
+ ///
+ /// Obtain a reference to a [`Role`] by its name.
+ ///
+ /// ```rust,no_run
+ /// use serenity::model::*;
+ /// use serenity::prelude::*;
+ ///
+ /// struct Handler;
+ ///
+ /// use serenity::CACHE;
+ ///
+ /// impl EventHandler for Handler {
+ /// fn on_message(&self, _: Context, msg: Message) {
+ /// if let Some(arc) = msg.guild_id().unwrap().find() {
+ /// if let Some(role) = arc.read().unwrap().role_by_name("role_name") {
+ /// println!("{:?}", role);
+ /// }
+ /// }
+ /// }
+ /// }
+ ///
+ /// let mut client = Client::new("token", Handler);
+ ///
+ /// client.start().unwrap();
+ /// ```
+ pub fn role_by_name(&self, role_name: &str) -> Option<&Role> {
+ self.roles.values().find(|role| role_name == role.name)
+ }
}
impl<'de> Deserialize<'de> for Guild {
diff --git a/src/model/guild/partial_guild.rs b/src/model/guild/partial_guild.rs
index 799f6b6..84d48d3 100644
--- a/src/model/guild/partial_guild.rs
+++ b/src/model/guild/partial_guild.rs
@@ -450,4 +450,38 @@ impl PartialGuild {
/// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html
#[inline]
pub fn webhooks(&self) -> Result<Vec<Webhook>> { self.id.webhooks() }
+
+ /// Obtain a reference to a role by its name.
+ ///
+ /// **Note**: If two or more roles have the same name, obtained reference will be one of
+ /// them.
+ ///
+ /// # Examples
+ ///
+ /// Obtain a reference to a [`Role`] by its name.
+ ///
+ /// ```rust,no_run
+ /// use serenity::model::*;
+ /// use serenity::prelude::*;
+ ///
+ /// struct Handler;
+ ///
+ /// use serenity::CACHE;
+ ///
+ /// impl EventHandler for Handler {
+ /// fn on_message(&self, _: Context, msg: Message) {
+ /// if let Some(role) =
+ /// msg.guild_id().unwrap().get().unwrap().role_by_name("role_name") {
+ /// println!("Obtained role's reference: {:?}", role);
+ /// }
+ /// }
+ /// }
+ ///
+ /// let mut client = Client::new("token", Handler);
+ ///
+ /// client.start().unwrap();
+ /// ```
+ pub fn role_by_name(&self, role_name: &str) -> Option<&Role> {
+ self.roles.values().find(|role| role_name == role.name)
+ }
}