From 41b67c60bf3a6d87d6d80595bdaa00a8d9d0590a Mon Sep 17 00:00:00 2001 From: Austin Hellyer Date: Sun, 15 Jan 2017 17:01:46 -0800 Subject: First round of deleting useless methods --- src/ext/framework/mod.rs | 36 +++++++++++--- src/model/channel.rs | 127 +---------------------------------------------- src/model/guild.rs | 21 +------- src/model/id.rs | 8 --- src/model/user.rs | 11 ---- 5 files changed, 33 insertions(+), 170 deletions(-) (limited to 'src') diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs index 1e3927e..ccb81f1 100644 --- a/src/ext/framework/mod.rs +++ b/src/ext/framework/mod.rs @@ -78,6 +78,8 @@ use ::utils; #[cfg(feature="cache")] use ::client::CACHE; +#[cfg(feature="cache")] +use ::ext::cache::ChannelRef; /// A macro to generate "named parameters". This is useful to avoid manually /// using the "arguments" parameter and manually parsing types. @@ -354,7 +356,7 @@ impl Framework { return; } - if self.configuration.ignore_webhooks && message.is_webhook() { + if self.configuration.ignore_webhooks && message.webhook_id.is_some() { return; } @@ -404,7 +406,15 @@ impl Framework { #[cfg(feature="cache")] { - if let Some(guild_id) = message.guild_id() { + + let guild_id = { + match CACHE.read().unwrap().get_channel(message.channel_id) { + Some(ChannelRef::Guild(channel)) => Some(channel.guild_id), + _ => None, + } + }; + + if let Some(guild_id) = guild_id { if self.configuration.blocked_guilds.contains(&guild_id) { if let Some(ref message) = self.configuration.blocked_guild_message { let _ = context.say(message); @@ -412,9 +422,7 @@ impl Framework { return; } - } - if let Some(guild_id) = message.guild_id() { if let Some(guild) = guild_id.find() { if self.configuration.blocked_users.contains(&guild.owner_id) { if let Some(ref message) = self.configuration.blocked_guild_message { @@ -499,9 +507,25 @@ impl Framework { if !is_owner && !command.required_permissions.is_empty() { let mut permissions_fulfilled = false; - if let Some(member) = message.get_member() { - let cache = CACHE.read().unwrap(); + let cache = CACHE.read().unwrap(); + + // Really **really** dirty code in the meantime + // before the framework rewrite. + let member = { + let mut member_found = None; + + if let Some(ChannelRef::Guild(channel)) = cache.get_channel(message.channel_id) { + if let Some(guild) = channel.guild_id.find() { + if let Some(member) = guild.members.get(&message.author.id) { + member_found = Some(member.clone()); + } + } + } + + member_found + }; + if let Some(member) = member { if let Ok(guild_id) = member.find_guild() { if let Some(guild) = cache.get_guild(guild_id) { let perms = guild.permissions_for(message.channel_id, message.author.id); diff --git a/src/model/channel.rs b/src/model/channel.rs index b441e1f..7cd4077 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -17,13 +17,9 @@ use hyper::Client as HyperClient; #[cfg(feature="methods")] use serde_json::builder::ObjectBuilder; #[cfg(feature="methods")] -use std::fs::File; -#[cfg(feature="methods")] -use std::io::{Read, Write as IoWrite}; +use std::io::Read; #[cfg(feature="methods")] use std::mem; -#[cfg(feature="methods")] -use std::path::{Path, PathBuf}; #[cfg(all(feature="cache", feature="methods"))] use super::utils; @@ -124,71 +120,6 @@ impl Attachment { Ok(bytes) } - - /// Downloads the attachment, saving it to the provided directory path. - /// Returns a path to the saved file. - /// - /// # Examples - /// - /// Download all of the attachments associated with a [`Message`] to a - /// given folder: - /// - /// ```rust,no_run - /// use serenity::Client; - /// use std::env; - /// use std::fs; - /// - /// // Make sure that the directory to store images in exists. - /// fs::create_dir_all("./attachment_downloads") - /// .expect("Error making directory"); - /// - /// let token = env::var("DISCORD_TOKEN").expect("token in environment"); - /// let mut client = Client::login_bot(&token); - /// - /// client.on_message(|context, message| { - /// for attachment in message.attachments { - /// let dir = "./attachment_downloads"; - /// - /// let _ = match attachment.download_to_directory(dir) { - /// Ok(_saved_filepath) => { - /// context.say(&format!("Saved {:?}", attachment.filename)) - /// }, - /// Err(why) => { - /// println!("Error saving attachment: {:?}", why); - /// context.say("Error saving attachment") - /// }, - /// }; - /// } - /// }); - /// - /// client.on_ready(|_context, ready| { - /// println!("{} is connected!", ready.user.name); - /// }); - /// - /// let _ = client.start(); - /// ``` - /// - /// # Errors - /// - /// Returns an [`Error::Io`] when there is a problem reading the contents of - /// the HTTP response, creating the file, or writing to the file. - /// - /// Returns an [`Error::Hyper`] when there is a problem retrieving the - /// attachment. - /// - /// [`Error::Hyper`]: ../enum.Error.html#variant.Hyper - /// [`Error::Io`]: ../enum.Error.html#variant.Io - /// [`Message`]: struct.Message.html - #[cfg(feature="methods")] - pub fn download_to_directory>(&self, path: P) -> Result { - let bytes = self.download()?; - - let filepath: PathBuf = path.as_ref().join(&self.filename); - let mut file = File::create(&filepath)?; - file.write(&bytes)?; - - Ok(filepath) - } } impl Channel { @@ -592,41 +523,6 @@ impl Message { } } - /// Retrieves the message author's member instance. - /// - /// **Note**: This will always return `None` for messages not sent in a - /// [`Guild`]. - /// - /// [`Guild`]: struct.Guild.html - #[cfg(all(feature="cache", feature="methods"))] - pub fn get_member(&self) -> Option { - let cache = CACHE.read().unwrap(); - - if let Some(ChannelRef::Guild(channel)) = cache.get_channel(self.channel_id) { - if let Some(guild) = channel.guild_id.find() { - if let Some(member) = guild.members.get(&self.author.id) { - return Some(member.clone()) - } - } - } - - None - } - - /// Retrieves the guild associated with this message. - /// - /// Returns `None` if the guild data does not exist in the cache, or if the - /// message was not sent in a [`GuildChannel`]. - /// - /// [`GuildChannel`]: struct.GuildChannel.html - #[cfg(all(feature="cache", feature="methods"))] - pub fn guild(&self) -> Option { - match self.guild_id().map(|x| CACHE.read().unwrap().get_guild(x).cloned()) { - Some(Some(guild)) => Some(guild), - _ => None, - } - } - /// Returns message content, but with user and role mentions replaced with /// names and everyone/here mentions cancelled. #[cfg(all(feature="cache", feature="methods"))] @@ -654,19 +550,6 @@ impl Message { .replace("@here", "@\u{200B}here") } - /// Retrieves the Id of the guild that the message was sent in, if sent in - /// one. - /// - /// Returns `None` if the channel data or guild data does not exist in the - /// cache. - #[cfg(all(feature="cache", feature="methods"))] - pub fn guild_id(&self) -> Option { - match CACHE.read().unwrap().get_channel(self.channel_id) { - Some(ChannelRef::Guild(channel)) => Some(channel.guild_id), - _ => None, - } - } - /// True if message was sent using direct messages. #[cfg(all(feature="cache", feature="methods"))] pub fn is_private(&self) -> bool { @@ -676,12 +559,6 @@ impl Message { } } - /// True only if message was sent using a webhook. - #[cfg(feature="methods")] - pub fn is_webhook(&self) -> bool { - self.webhook_id.is_some() - } - /// Checks the length of a string to ensure that it is within Discord's /// maximum message length limit. /// @@ -1088,7 +965,7 @@ impl GuildChannel { /// ```rust,ignore /// channel.edit(|c| c /// .name("test") - /// .bitrate(71)); + /// .bitrate(86400)); /// ``` #[cfg(feature="methods")] pub fn edit(&mut self, f: F) -> Result<()> diff --git a/src/model/guild.rs b/src/model/guild.rs index 7b8ed60..9b0c321 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -160,12 +160,6 @@ impl PartialGuild { rest::edit_nickname(self.id.0, new_nickname) } - /// Finds a role by Id within the guild. - #[cfg(feature="methods")] - pub fn find_role>(&self, role_id: R) -> Option<&Role> { - self.roles.get(&role_id.into()) - } - /// Returns a formatted URL of the guild's icon, if the guild has an icon. pub fn icon_url(&self) -> Option { self.icon.as_ref().map(|icon| @@ -271,7 +265,7 @@ impl PartialGuild { impl Guild { #[cfg(all(feature="cache", feature="methods"))] fn has_perms(&self, mut permissions: Permissions) -> Result { - let member = match self.get_member(CACHE.read().unwrap().user.id) { + let member = match self.members.get(&CACHE.read().unwrap().user.id) { Some(member) => member, None => return Err(Error::Client(ClientError::ItemMissing)), }; @@ -577,14 +571,6 @@ impl Guild { rest::edit_nickname(self.id.0, new_nickname) } - /// Attempts to retrieve a [`GuildChannel`] with the given Id. - /// - /// [`GuildChannel`]: struct.GuildChannel.html - pub fn get_channel>(&self, channel_id: C) - -> Option<&GuildChannel> { - self.channels.get(&channel_id.into()) - } - /// Retrieves the active invites for the guild. /// /// **Note**: Requires the [Manage Guild] permission. @@ -610,11 +596,6 @@ impl Guild { rest::get_guild_invites(self.id.0) } - /// Attempts to retrieve the given user's member instance in the guild. - pub fn get_member>(&self, user_id: U) -> Option<&Member> { - self.members.get(&user_id.into()) - } - /// Retrieves the first [`Member`] found that matches the name - with an /// optional discriminator - provided. /// diff --git a/src/model/id.rs b/src/model/id.rs index 9c2bb7a..17fd29c 100644 --- a/src/model/id.rs +++ b/src/model/id.rs @@ -88,14 +88,6 @@ impl GuildId { rest::get_guild(self.0) } - /// Returns this Id as a `ChannelId`, which is useful when needing to use - /// the guild Id to send a message to the default channel. - #[cfg(feature="methods")] - #[inline(always)] - pub fn to_channel(&self) -> ChannelId { - ChannelId(self.0) - } - /// Retrieves the guild's webhooks. /// /// **Note**: Requires the [Manage Webhooks] permission. diff --git a/src/model/user.rs b/src/model/user.rs index 253219a..8a2dd45 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -19,8 +19,6 @@ use serde_json::builder::ObjectBuilder; use std::mem; #[cfg(feature="methods")] use super::Message; -#[cfg(all(feature="cache", feature="methods"))] -use super::Member; #[cfg(feature="methods")] use time::Timespec; #[cfg(feature="methods")] @@ -260,15 +258,6 @@ impl User { } } - /// Gets the user's member instance for a guild. - #[cfg(all(feature="cache", feature="methods"))] - pub fn member(&self, guild_id: G) -> Option - where G: Into { - let cache = CACHE.read().unwrap(); - - cache.get_member(guild_id.into(), self.id).cloned() - } - /// Returns a static formatted URL of the user's icon, if one exists. /// /// This will always produce a WEBP image URL. -- cgit v1.2.3