diff options
| author | acdenisSK <[email protected]> | 2017-10-02 22:29:10 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-10-02 22:29:10 +0200 |
| commit | 6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249 (patch) | |
| tree | ae9ad7d7cb73d4ece6a199796af5975545071493 /src | |
| parent | `to_owned` -> `to_string` (diff) | |
| download | serenity-6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249.tar.xz serenity-6a4e52b3fac7d2e96e3a1a67901fbdd4721fb249.zip | |
Use the de-generification trick.
Fixes #168
Diffstat (limited to 'src')
| -rw-r--r-- | src/builder/create_embed.rs | 16 | ||||
| -rw-r--r-- | src/builder/create_message.rs | 6 | ||||
| -rw-r--r-- | src/builder/edit_guild.rs | 27 | ||||
| -rw-r--r-- | src/builder/edit_member.rs | 8 | ||||
| -rw-r--r-- | src/builder/get_messages.rs | 24 | ||||
| -rw-r--r-- | src/cache/mod.rs | 52 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 47 | ||||
| -rw-r--r-- | src/http/mod.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/channel_id.rs | 61 | ||||
| -rw-r--r-- | src/model/channel/group.rs | 8 | ||||
| -rw-r--r-- | src/model/channel/message.rs | 6 | ||||
| -rw-r--r-- | src/model/channel/reaction.rs | 12 | ||||
| -rw-r--r-- | src/model/guild/guild_id.rs | 91 | ||||
| -rw-r--r-- | src/model/guild/member.rs | 22 | ||||
| -rw-r--r-- | src/model/guild/mod.rs | 30 | ||||
| -rw-r--r-- | src/model/invite.rs | 5 | ||||
| -rw-r--r-- | src/model/user.rs | 10 | ||||
| -rw-r--r-- | src/utils/message_builder.rs | 24 | ||||
| -rw-r--r-- | src/voice/manager.rs | 17 |
19 files changed, 349 insertions, 123 deletions
diff --git a/src/builder/create_embed.rs b/src/builder/create_embed.rs index a128c91..7303693 100644 --- a/src/builder/create_embed.rs +++ b/src/builder/create_embed.rs @@ -62,14 +62,20 @@ impl CreateEmbed { /// [`colour`]: #method.colour #[cfg(feature = "utils")] #[inline] - pub fn color<C: Into<Colour>>(self, colour: C) -> Self { self.colour(colour.into()) } + pub fn color<C: Into<Colour>>(self, colour: C) -> Self { self.colour(colour) } + /// Set the colour of the left-hand side of the embed. #[cfg(feature = "utils")] pub fn colour<C: Into<Colour>>(mut self, colour: C) -> Self { + self._colour(colour.into()) + } + + #[cfg(feature = "utils")] + fn _colour(mut self, colour: Colour) -> Self { self.0.insert( "color".to_string(), - Value::Number(Number::from(colour.into().0 as u64)), + Value::Number(Number::from(colour.0 as u64)), ); CreateEmbed(self.0) @@ -282,8 +288,12 @@ impl CreateEmbed { /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self { + self._timestamp(timestamp.into()) + } + + fn _timestamp(mut self, timestamp: Timestamp) -> Self { self.0 - .insert("timestamp".to_string(), Value::String(timestamp.into().ts)); + .insert("timestamp".to_string(), Value::String(timestamp.ts)); CreateEmbed(self.0) } diff --git a/src/builder/create_message.rs b/src/builder/create_message.rs index 96bad82..123c0b5 100644 --- a/src/builder/create_message.rs +++ b/src/builder/create_message.rs @@ -75,7 +75,11 @@ impl CreateMessage { /// Adds a list of reactions to create after the message's sent. pub fn reactions<R: Into<ReactionType>>(mut self, reactions: Vec<R>) -> Self { - self.1 = Some(reactions.into_iter().map(|r| r.into()).collect()); + self._reactions(reactions.into_iter().map(|r| r.into()).collect()) + } + + fn _reactions(mut self, reactions: Vec<ReactionType>) -> Self { + self.1 = Some(reactions); CreateMessage(self.0, self.1) } diff --git a/src/builder/edit_guild.rs b/src/builder/edit_guild.rs index 0719305..a4f2c54 100644 --- a/src/builder/edit_guild.rs +++ b/src/builder/edit_guild.rs @@ -22,13 +22,14 @@ impl EditGuild { /// valid. /// /// [`afk_timeout`]: #method.afk_timeout - pub fn afk_channel<C: Into<ChannelId>>(mut self, channel: Option<C>) -> Self { + pub fn afk_channel<C: Into<ChannelId>>(self, channel: Option<C>) -> Self { + self._afk_channel(channel.map(|c| c.into())) + } + + fn _afk_channel(mut self, channel: Option<ChannelId>) -> Self { self.0.insert( "afk_channel_id".to_string(), - match channel { - Some(channel) => Value::Number(Number::from(channel.into().0)), - None => Value::Null, - }, + channel.map_or(Value::Null, |ChannelId(id)| Value::Number(Number::from(id))) ); self @@ -98,10 +99,14 @@ impl EditGuild { /// Transfers the ownership of the guild to another user by Id. /// /// **Note**: The current user must be the owner of the guild. - pub fn owner<U: Into<UserId>>(mut self, user_id: U) -> Self { + pub fn owner<U: Into<UserId>>(self, user_id: U) -> Self { + self._owner(user_id.into()) + } + + fn _owner(mut self, user_id: UserId) -> Self { self.0.insert( "owner_id".to_string(), - Value::Number(Number::from(user_id.into().0)), + Value::Number(Number::from(user_id.0)), ); self @@ -185,9 +190,13 @@ impl EditGuild { /// /// [`VerificationLevel`]: ../model/enum.VerificationLevel.html /// [`VerificationLevel::High`]: ../model/enum.VerificationLevel.html#variant.High - pub fn verification_level<V>(mut self, verification_level: V) -> Self + pub fn verification_level<V>(self, verification_level: V) -> Self where V: Into<VerificationLevel> { - let num = Value::Number(Number::from(verification_level.into().num())); + self._verification_level(verification_level.into()) + } + + fn _verification_level(mut self, verification_level: VerificationLevel) -> Self { + let num = Value::Number(Number::from(verification_level.num())); self.0.insert("verification_level".to_string(), num); diff --git a/src/builder/edit_member.rs b/src/builder/edit_member.rs index e450669..53cf0d0 100644 --- a/src/builder/edit_member.rs +++ b/src/builder/edit_member.rs @@ -66,10 +66,14 @@ impl EditMember { /// Requires the [Move Members] permission. /// /// [Move Members]: ../model/permissions/constant.MOVE_MEMBERS.html - pub fn voice_channel<C: Into<ChannelId>>(mut self, channel_id: C) -> Self { + pub fn voice_channel<C: Into<ChannelId>>(self, channel_id: C) -> Self { + self._voice_channel(channel_id.into()) + } + + fn _voice_channel(mut self, channel_id: ChannelId) -> Self { self.0.insert( "channel_id".to_string(), - Value::Number(Number::from(channel_id.into().0)), + Value::Number(Number::from(channel_id.0)), ); self diff --git a/src/builder/get_messages.rs b/src/builder/get_messages.rs index 71af9e5..eff90f4 100644 --- a/src/builder/get_messages.rs +++ b/src/builder/get_messages.rs @@ -55,24 +55,36 @@ pub struct GetMessages(pub BTreeMap<String, u64>); impl GetMessages { /// Indicates to retrieve the messages after a specific message, given by /// its Id. - pub fn after<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("after".to_string(), message_id.into().0); + pub fn after<M: Into<MessageId>>(self, message_id: M) -> Self { + self._after(message_id.into()) + } + + fn _after(mut self, MessageId(id): MessageId) -> Self { + self.0.insert("after".to_string(), id); self } /// Indicates to retrieve the messages _around_ a specific message in either /// direction (before+after) the given message. - pub fn around<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("around".to_string(), message_id.into().0); + pub fn around<M: Into<MessageId>>(self, message_id: M) -> Self { + self._around(message_id.into()) + } + + fn _around(mut self, MessageId(id): MessageId) -> Self { + self.0.insert("around".to_string(), id); self } /// Indicates to retrieve the messages before a specific message, given by /// its Id. - pub fn before<M: Into<MessageId>>(mut self, message_id: M) -> Self { - self.0.insert("before".to_string(), message_id.into().0); + pub fn before<M: Into<MessageId>>(self, message_id: M) -> Self { + self._before(message_id.into()) + } + + fn _before(mut self, MessageId(id): MessageId) -> Self { + self.0.insert("before".to_string(), id); self } diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 2bbc9bc..def6ac4 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -298,8 +298,10 @@ impl Cache { /// [`groups`]: #structfield.groups /// [`private_channels`]: #structfield.private_channels pub fn channel<C: Into<ChannelId>>(&self, id: C) -> Option<Channel> { - let id = id.into(); + self._channel(id.into()) + } + fn _channel(&self, id: ChannelId) -> Option<Channel> { if let Some(channel) = self.channels.get(&id) { return Some(Channel::Guild(channel.clone())); } @@ -346,7 +348,11 @@ impl Cache { /// ``` #[inline] pub fn guild<G: Into<GuildId>>(&self, id: G) -> Option<Arc<RwLock<Guild>>> { - self.guilds.get(&id.into()).cloned() + self._guild(id.into()) + } + + fn _guild(&self, id: GuildId) -> Option<Arc<RwLock<Guild>>> { + self.guilds.get(&id).cloned() } /// Retrieves a reference to a [`Guild`]'s channel. Unlike [`channel`], @@ -395,7 +401,11 @@ impl Cache { /// [`channel`]: #method.channel #[inline] pub fn guild_channel<C: Into<ChannelId>>(&self, id: C) -> Option<Arc<RwLock<GuildChannel>>> { - self.channels.get(&id.into()).cloned() + self._guild_channel(id.into()) + } + + fn _guild_channel(&self, id: ChannelId) -> Option<Arc<RwLock<GuildChannel>>> { + self.channels.get(&id).cloned() } /// Retrieves a reference to a [`Group`] from the cache based on the given @@ -431,7 +441,11 @@ impl Cache { /// ``` #[inline] pub fn group<C: Into<ChannelId>>(&self, id: C) -> Option<Arc<RwLock<Group>>> { - self.groups.get(&id.into()).cloned() + self._group(id.into()) + } + + fn _group(&self, id: ChannelId) -> Option<Arc<RwLock<Group>>> { + self.groups.get(&id).cloned() } /// Retrieves a [`Guild`]'s member from the cache based on the guild's and @@ -481,6 +495,10 @@ impl Cache { /// [`members`]: ../model/struct.Guild.html#structfield.members pub fn member<G, U>(&self, guild_id: G, user_id: U) -> Option<Member> where G: Into<GuildId>, U: Into<UserId> { + self._member(guild_id.into(), user_id.into()) + } + + fn _member(&self, guild_id: GuildId, user_id: UserId) -> Option<Member> { self.guilds.get(&guild_id.into()).and_then(|guild| { guild.read().unwrap().members.get(&user_id.into()).cloned() }) @@ -518,7 +536,11 @@ impl Cache { pub fn private_channel<C: Into<ChannelId>>(&self, channel_id: C) -> Option<Arc<RwLock<PrivateChannel>>> { - self.private_channels.get(&channel_id.into()).cloned() + self._private_channel(channel_id.into()) + } + + fn _private_channel(&self, channel_id: ChannelId) -> Option<Arc<RwLock<PrivateChannel>>> { + self.private_channels.get(&channel_id).cloned() } /// Retrieves a [`Guild`]'s role by their Ids. @@ -553,9 +575,13 @@ impl Cache { /// ``` pub fn role<G, R>(&self, guild_id: G, role_id: R) -> Option<Role> where G: Into<GuildId>, R: Into<RoleId> { + self._role(guild_id.into(), role_id.into()) + } + + fn _role(&self, guild_id: GuildId, role_id: RoleId) -> Option<Role> { self.guilds - .get(&guild_id.into()) - .and_then(|g| g.read().unwrap().roles.get(&role_id.into()).cloned()) + .get(&guild_id) + .and_then(|g| g.read().unwrap().roles.get(&role_id).cloned()) } /// Retrieves a `User` from the cache's [`users`] map, if it exists. @@ -590,14 +616,22 @@ impl Cache { /// ``` #[inline] pub fn user<U: Into<UserId>>(&self, user_id: U) -> Option<Arc<RwLock<User>>> { - self.users.get(&user_id.into()).cloned() + self._user(user_id.into()) + } + + fn _user(&self, user_id: UserId) -> Option<Arc<RwLock<User>>> { + self.users.get(&user_id).cloned() } #[inline] pub fn categories<C: Into<ChannelId>>(&self, channel_id: C) -> Option<Arc<RwLock<ChannelCategory>>> { - self.categories.get(&channel_id.into()).cloned() + self._categories(channel_id.into()) + } + + fn _categories(&self, channel_id: ChannelId) -> Option<Arc<RwLock<ChannelCategory>>> { + self.categories.get(&channel_id).cloned() } #[cfg(feature = "client")] diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 92572dd..682a466 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -236,10 +236,9 @@ impl StandardFramework { /// .bucket("basic") /// .exec_str("pong!"))); /// ``` - pub fn bucket<S>(mut self, s: S, delay: i64, time_span: i64, limit: i32) -> Self - where S: Into<String> { + pub fn bucket(mut self, s: &str, delay: i64, time_span: i64, limit: i32) -> Self { self.buckets.insert( - s.into(), + s.to_string(), Bucket { ratelimit: Ratelimit { delay: delay, @@ -282,8 +281,8 @@ impl StandardFramework { /// /// [`bucket`]: #method.bucket #[cfg(feature = "cache")] - pub fn complex_bucket<S, Check>(mut self, - s: S, + pub fn complex_bucket<Check>(mut self, + s: &str, delay: i64, time_span: i64, limit: i32, @@ -292,10 +291,9 @@ impl StandardFramework { where Check: Fn(&mut Context, Option<GuildId>, ChannelId, UserId) -> bool + Send + Sync - + 'static, - S: Into<String> { + + 'static { self.buckets.insert( - s.into(), + s.to_string(), Bucket { ratelimit: Ratelimit { delay, @@ -336,17 +334,16 @@ impl StandardFramework { /// /// [`bucket`]: #method.bucket #[cfg(not(feature = "cache"))] - pub fn complex_bucket<S, Check>(mut self, - s: S, + pub fn complex_bucket<Check>(mut self, + s: &str, delay: i64, time_span: i64, limit: i32, check: Check) -> Self - where Check: Fn(&mut Context, ChannelId, UserId) -> bool + Send + Sync + 'static, - S: Into<String> { + where Check: Fn(&mut Context, ChannelId, UserId) -> bool + Send + Sync + 'static { self.buckets.insert( - s.into(), + s.to_string(), Bucket { ratelimit: Ratelimit { delay, @@ -381,10 +378,9 @@ impl StandardFramework { /// .bucket("simple") /// .exec_str("pong!"))); /// ``` - pub fn simple_bucket<S>(mut self, s: S, delay: i64) -> Self - where S: Into<String> { + pub fn simple_bucket(mut self, s: &str, delay: i64) -> Self { self.buckets.insert( - s.into(), + s.to_string(), Bucket { ratelimit: Ratelimit { delay: delay, @@ -596,20 +592,17 @@ impl StandardFramework { /// }); /// # } /// ``` - pub fn on<F, S>(mut self, command_name: S, f: F) -> Self - where F: Fn(&mut Context, &Message, Args) -> Result<(), CommandError> + Send + Sync + 'static, - S: Into<String> { + pub fn on<F>(mut self, command_name: &str, f: F) -> Self + where F: Fn(&mut Context, &Message, Args) -> Result<(), CommandError> + Send + Sync + 'static { { let ungrouped = self.groups .entry("Ungrouped".to_string()) .or_insert_with(|| Arc::new(CommandGroup::default())); if let Some(ref mut group) = Arc::get_mut(ungrouped) { - let name = command_name.into(); - group .commands - .insert(name, CommandOrAlias::Command(Arc::new(Command::new(f)))); + .insert(command_name.to_string(), CommandOrAlias::Command(Arc::new(Command::new(f)))); } } @@ -629,8 +622,8 @@ impl StandardFramework { /// let _ = ctx.say("pong"); /// })); /// ``` - pub fn command<F, S>(mut self, command_name: S, f: F) -> Self - where F: FnOnce(CreateCommand) -> CreateCommand, S: Into<String> { + pub fn command<F>(mut self, command_name: &str, f: F) -> Self + where F: FnOnce(CreateCommand) -> CreateCommand { { let ungrouped = self.groups .entry("Ungrouped".to_string()) @@ -638,7 +631,7 @@ impl StandardFramework { if let Some(ref mut group) = Arc::get_mut(ungrouped) { let cmd = f(CreateCommand(Command::default())).0; - let name = command_name.into(); + let name = command_name.to_string(); if let Some(ref prefix) = group.prefix { for v in &cmd.aliases { @@ -688,8 +681,8 @@ impl StandardFramework { /// .command("ping", |c| c.exec_str("pong!")) /// .command("pong", |c| c.exec_str("ping!")))); /// ``` - pub fn group<F, S>(mut self, group_name: S, f: F) -> Self - where F: FnOnce(CreateGroup) -> CreateGroup, S: Into<String> { + pub fn group<F>(mut self, group_name: &str, f: F) -> Self + where F: FnOnce(CreateGroup) -> CreateGroup { let group = f(CreateGroup(CommandGroup::default())).0; self.groups.insert(group_name.into(), Arc::new(group)); diff --git a/src/http/mod.rs b/src/http/mod.rs index 4f70dcf..f7a71ee 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1613,6 +1613,10 @@ pub fn remove_group_recipient(group_id: u64, user_id: u64) -> Result<()> { /// [`HttpError::InvalidRequest`]: enum.HttpError.html#variant.InvalidRequest pub fn send_files<'a, T>(channel_id: u64, files: Vec<T>, map: JsonMap) -> Result<Message> where T: Into<AttachmentType<'a>> { + _send_files(channel_id, files.into_iter().map(|a| a.into()).collect(), map) +} + +fn _send_files<'a>(channel_id: u64, files: Vec<AttachmentType<'a>>, map: JsonMap) -> Result<Message> { let uri = format!(api!("/channels/{}/messages"), channel_id); let url = match Url::parse(&uri) { Ok(url) => url, @@ -1633,7 +1637,7 @@ pub fn send_files<'a, T>(channel_id: u64, files: Vec<T>, map: JsonMap) -> Result let mut file_num = "0".to_string(); for file in files { - match file.into() { + match file { AttachmentType::Bytes((mut bytes, filename)) => { request .write_stream(&file_num, &mut bytes, Some(filename), None)?; diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index 4cc997d..04ed266 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -81,7 +81,11 @@ impl ChannelId { #[inline] pub fn create_reaction<M, R>(&self, message_id: M, reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { - http::create_reaction(self.0, message_id.into().0, &reaction_type.into()) + self._create_reaction(message_id.into(), reaction_type.into()) + } + + fn _create_reaction(&self, MessageId(id): MessageId, reaction_type: ReactionType) -> Result<()> { + http::create_reaction(self.0, id, &reaction_type) } /// Deletes this channel, returning the channel on a successful deletion. @@ -100,7 +104,11 @@ impl ChannelId { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn delete_message<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::delete_message(self.0, message_id.into().0) + self._delete_message(message_id.into()) + } + + fn _delete_message(&self, MessageId(id): MessageId) -> Result<()> { + http::delete_message(self.0, id) } /// Deletes all messages by Ids from the given vector in the given channel. @@ -156,11 +164,18 @@ impl ChannelId { reaction_type: R) -> Result<()> where M: Into<MessageId>, R: Into<ReactionType> { + self._delete_reaction(message_id.into(), user_id, reaction_type.into()) + } + + fn _delete_reaction(&self, + MessageId(id): MessageId, + user_id: Option<UserId>, + reaction_type: ReactionType) -> Result<()> { http::delete_reaction( self.0, - message_id.into().0, + id, user_id.map(|uid| uid.0), - &reaction_type.into(), + &reaction_type, ) } @@ -209,6 +224,11 @@ impl ChannelId { /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content pub fn edit_message<F, M>(&self, message_id: M, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage, M: Into<MessageId> { + self._edit_message(message_id.into(), f) + } + + fn _edit_message<F>(&self, MessageId(id): MessageId, f: F) -> Result<Message> + where F: FnOnce(CreateMessage) -> CreateMessage { let map = f(CreateMessage::default()).0; if let Some(content) = map.get("content") { @@ -219,7 +239,7 @@ impl ChannelId { } } - http::edit_message(self.0, message_id.into().0, &Value::Object(map)) + http::edit_message(self.0, id, &Value::Object(map)) } /// Search the cache for the channel with the Id. @@ -253,7 +273,11 @@ impl ChannelId { /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[inline] pub fn message<M: Into<MessageId>>(&self, message_id: M) -> Result<Message> { - http::get_message(self.0, message_id.into().0) + self._message(message_id.into()) + } + + fn _message(&self, MessageId(id): MessageId) -> Result<Message> { + http::get_message(self.0, id) .map(|mut msg| { msg.transform_content(); @@ -326,7 +350,11 @@ impl ChannelId { /// [`Message`]: struct.Message.html #[inline] pub fn pin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - http::pin_message(self.0, message_id.into().0) + self._pin(message_id.into()) + } + + fn _pin(&self, MessageId(id): MessageId) -> Result<()> { + http::pin_message(self.0, id) } /// Gets the list of [`Message`]s which are pinned to the channel. @@ -354,14 +382,23 @@ impl ChannelId { after: Option<U>) -> Result<Vec<User>> where M: Into<MessageId>, R: Into<ReactionType>, U: Into<UserId> { + self._reaction_users(message_id.into(), reaction_type.into(), limit, after.map(|u| u.into())) + } + + fn _reaction_users(&self, + MessageId(id): MessageId, + reaction_type: ReactionType, + limit: Option<u8>, + after: Option<UserId>) + -> Result<Vec<User>> { let limit = limit.map_or(50, |x| if x > 100 { 100 } else { x }); http::get_reaction_users( self.0, - message_id.into().0, - &reaction_type.into(), + id, + &reaction_type, limit, - after.map(|u| u.into().0), + after.map(|u| u.0), ) } @@ -504,6 +541,10 @@ impl ChannelId { http::unpin_message(self.0, message_id.into().0) } + fn _unpin(&self, MessageId(id): MessageId) -> Result<()> { + http::unpin_message(self.0, id) + } + /// Retrieves the channel's webhooks. /// /// **Note**: Requires the [Manage Webhooks] permission. diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs index 418ce3a..dfccca3 100644 --- a/src/model/channel/group.rs +++ b/src/model/channel/group.rs @@ -49,8 +49,10 @@ impl Group { /// /// [`http::add_group_recipient`]: ../http/fn.add_group_recipient.html pub fn add_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._add_recipient(user.into()) + } + fn _add_recipient(&self, user: UserId) -> Result<()> { // If the group already contains the recipient, do nothing. if self.recipients.contains_key(&user) { return Ok(()); @@ -253,8 +255,10 @@ impl Group { /// /// **Note**: This is only available to the group owner. pub fn remove_recipient<U: Into<UserId>>(&self, user: U) -> Result<()> { - let user = user.into(); + self._remove_recipient(user.into()) + } + fn _remove_recipient(&self, user: UserId) -> Result<()> { // If the group does not contain the recipient already, do nothing. if !self.recipients.contains_key(&user) { return Ok(()); diff --git a/src/model/channel/message.rs b/src/model/channel/message.rs index 55f61ba..52f5be5 100644 --- a/src/model/channel/message.rs +++ b/src/model/channel/message.rs @@ -419,6 +419,10 @@ impl Message { /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html /// [permissions]: permissions pub fn react<R: Into<ReactionType>>(&self, reaction_type: R) -> Result<()> { + self._react(reaction_type.into()) + } + + fn _react(&self, reaction_type: ReactionType) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::ADD_REACTIONS; @@ -428,7 +432,7 @@ impl Message { } } - http::create_reaction(self.channel_id.0, self.id.0, &reaction_type.into()) + http::create_reaction(self.channel_id.0, self.id.0, &reaction_type) } /// Replies to the user, mentioning them prior to the content in the form diff --git a/src/model/channel/reaction.rs b/src/model/channel/reaction.rs index 8edc2e9..976ea3d 100644 --- a/src/model/channel/reaction.rs +++ b/src/model/channel/reaction.rs @@ -106,12 +106,20 @@ impl Reaction { after: Option<U>) -> Result<Vec<User>> where R: Into<ReactionType>, U: Into<UserId> { + self._users(reaction_type.into(), limit, after.map(|u| u.into())) + } + + fn _users(&self, + reaction_type: ReactionType, + limit: Option<u8>, + after: Option<UserId>) + -> Result<Vec<User>> { http::get_reaction_users( self.channel_id.0, self.message_id.0, - &reaction_type.into(), + &reaction_type, limit.unwrap_or(50), - after.map(|u| u.into().0), + after.map(|u| u.0), ) } } diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 4b9a713..bc50e16 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -49,6 +49,10 @@ impl GuildId { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, ban_options: BO) -> Result<()> { + self._ban(user.into(), ban_options) + } + + fn _ban<BO: BanOptions>(&self, UserId(id): UserId, ban_options: BO) -> Result<()> { let dmd = ban_options.dmd(); if dmd > 7 { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); @@ -60,7 +64,7 @@ impl GuildId { return Err(Error::ExceededLimit(reason.to_string(), 512)); } - http::ban_user(self.0, user.into().0, dmd, &*reason) + http::ban_user(self.0, id, dmd, &reason) } /// Gets a list of the guild's bans. @@ -149,13 +153,16 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html pub fn create_integration<I>(&self, integration_id: I, kind: &str) -> Result<()> where I: Into<IntegrationId> { - let integration_id = integration_id.into(); + self._create_integration(integration_id.into(), kind) + } + + fn _create_integration(&self, IntegrationId(id): IntegrationId, kind: &str) -> Result<()> { let map = json!({ - "id": integration_id.0, + "id": id, "type": kind, }); - http::create_guild_integration(self.0, integration_id.0, &map) + http::create_guild_integration(self.0, id, &map) } /// Creates a new role in the guild with the data set, if any. @@ -190,7 +197,11 @@ impl GuildId { /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html #[inline] pub fn delete_emoji<E: Into<EmojiId>>(&self, emoji_id: E) -> Result<()> { - http::delete_emoji(self.0, emoji_id.into().0) + self._delete_emoji(emoji_id.into()) + } + + fn _delete_emoji(&self, EmojiId(id): EmojiId) -> Result<()> { + http::delete_emoji(self.0, id) } /// Deletes an integration by Id from the guild. @@ -200,7 +211,11 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn delete_integration<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::delete_guild_integration(self.0, integration_id.into().0) + self._delete_integration(integration_id.into()) + } + + fn _delete_integration(&self, IntegrationId(id): IntegrationId) -> Result<()> { + http::delete_guild_integration(self.0, id) } /// Deletes a [`Role`] by Id from the guild. @@ -215,7 +230,11 @@ impl GuildId { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[inline] pub fn delete_role<R: Into<RoleId>>(&self, role_id: R) -> Result<()> { - http::delete_role(self.0, role_id.into().0) + self._delete_role(role_id.into()) + } + + fn _delete_role(&self, RoleId(id): RoleId) -> Result<()> { + http::delete_role(self.0, id) } /// Edits the current guild with new data where specified. @@ -243,11 +262,15 @@ impl GuildId { /// [`Emoji::edit`]: struct.Emoji.html#method.edit /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html pub fn edit_emoji<E: Into<EmojiId>>(&self, emoji_id: E, name: &str) -> Result<Emoji> { + self._edit_emoji(emoji_id.into(), name) + } + + fn _edit_emoji(&self, EmojiId(id): EmojiId, name: &str) -> Result<Emoji> { let map = json!({ "name": name, }); - http::edit_emoji(self.0, emoji_id.into().0, &map) + http::edit_emoji(self.0, id, &map) } /// Edits the properties of member of the guild, such as muting or @@ -266,7 +289,12 @@ impl GuildId { #[inline] pub fn edit_member<F, U>(&self, user_id: U, f: F) -> Result<()> where F: FnOnce(EditMember) -> EditMember, U: Into<UserId> { - http::edit_member(self.0, user_id.into().0, &f(EditMember::default()).0) + self._edit_member(user_id.into(), f) + } + + fn _edit_member<F>(&self, UserId(id): UserId, f: F) -> Result<()> + where F: FnOnce(EditMember) -> EditMember { + http::edit_member(self.0, id, &f(EditMember::default()).0) } /// Edits the current user's nickname for the guild. @@ -300,7 +328,12 @@ impl GuildId { #[inline] pub fn edit_role<F, R>(&self, role_id: R, f: F) -> Result<Role> where F: FnOnce(EditRole) -> EditRole, R: Into<RoleId> { - http::edit_role(self.0, role_id.into().0, &f(EditRole::default()).0) + self._edit_role(role_id.into(), f) + } + + fn _edit_role<F>(&self, RoleId(id): RoleId, f: F) -> Result<Role> + where F: FnOnce(EditRole) -> EditRole { + http::edit_role(self.0, id, &f(EditRole::default()).0) } /// Search the cache for the guild. @@ -336,7 +369,11 @@ impl GuildId { /// [Kick Members]: permissions/constant.KICK_MEMBERS.html #[inline] pub fn kick<U: Into<UserId>>(&self, user_id: U) -> Result<()> { - http::kick_member(self.0, user_id.into().0) + self._kick(user_id.into()) + } + + fn _kick(&self, UserId(id): UserId) -> Result<()> { + http::kick_member(self.0, id) } /// Leaves the guild. @@ -349,7 +386,11 @@ impl GuildId { /// [`Member`]: struct.Member.html #[inline] pub fn member<U: Into<UserId>>(&self, user_id: U) -> Result<Member> { - http::get_member(self.0, user_id.into().0) + self._member(user_id.into()) + } + + fn _member(&self, UserId(id): UserId) -> Result<Member> { + http::get_member(self.0, id) } /// Gets a list of the guild's members. @@ -362,7 +403,11 @@ impl GuildId { #[inline] pub fn members<U>(&self, limit: Option<u64>, after: Option<U>) -> Result<Vec<Member>> where U: Into<UserId> { - http::get_guild_members(self.0, limit, after.map(|x| x.into().0)) + self._members(limit, after.map(|x| x.into())) + } + + fn _members(&self, limit: Option<u64>, after: Option<UserId>) -> Result<Vec<Member>> { + http::get_guild_members(self.0, limit, after.map(|x| x.0)) } /// Moves a member to a specific voice channel. @@ -372,13 +417,17 @@ impl GuildId { /// [Move Members]: permissions/constant.MOVE_MEMBERS.html pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> where C: Into<ChannelId>, U: Into<UserId> { + self._move_member(user_id.into(), channel_id.into()) + } + + fn _move_member(&self, UserId(uid): UserId, ChannelId(cid): ChannelId) -> Result<()> { let mut map = Map::new(); map.insert( "channel_id".to_string(), - Value::Number(Number::from(channel_id.into().0)), + Value::Number(Number::from(cid)), ); - http::edit_member(self.0, user_id.into().0, &map) + http::edit_member(self.0, uid, &map) } /// Gets the number of [`Member`]s that would be pruned with the given @@ -442,7 +491,11 @@ impl GuildId { /// [Manage Guild]: permissions/constant.MANAGE_GUILD.html #[inline] pub fn start_integration_sync<I: Into<IntegrationId>>(&self, integration_id: I) -> Result<()> { - http::start_integration_sync(self.0, integration_id.into().0) + self._start_integration_sync(integration_id.into()) + } + + fn _start_integration_sync(&self, IntegrationId(id): IntegrationId) -> Result<()> { + http::start_integration_sync(self.0, id) } /// Starts a prune of [`Member`]s. @@ -471,7 +524,11 @@ impl GuildId { /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[inline] pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { - http::remove_ban(self.0, user_id.into().0) + self._unban(user_id.into()) + } + + fn _unban(&self, UserId(id): UserId) -> Result<()> { + http::remove_ban(self.0, id) } /// Retrieves the guild's webhooks. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index 09474f9..7c0c784 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -78,15 +78,18 @@ impl Member { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] pub fn add_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._add_role(role_id.into()) + } - if self.roles.contains(&role_id) { + #[cfg(feature = "cache")] + fn _add_role(&mut self, id: RoleId) -> Result<()> { + if self.roles.contains(&id) { return Ok(()); } - match http::add_member_role(self.guild_id.0, self.user.read().unwrap().id.0, role_id.0) { + match http::add_member_role(self.guild_id.0, self.user.read().unwrap().id.0, id.0) { Ok(()) => { - self.roles.push(role_id); + self.roles.push(id); Ok(()) }, @@ -312,15 +315,18 @@ impl Member { /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature = "cache")] pub fn remove_role<R: Into<RoleId>>(&mut self, role_id: R) -> Result<()> { - let role_id = role_id.into(); + self._remove_role(role_id.into()) + } - if !self.roles.contains(&role_id) { + #[cfg(feature = "cache")] + fn _remove_role(&mut self, id: RoleId) -> Result<()> { + if !self.roles.contains(&id) { return Ok(()); } - match http::remove_member_role(self.guild_id.0, self.user.read().unwrap().id.0, role_id.0) { + match http::remove_member_role(self.guild_id.0, self.user.read().unwrap().id.0, id.0) { Ok(()) => { - self.roles.retain(|r| r.0 != role_id.0); + self.roles.retain(|r| r.0 != id.0); Ok(()) }, diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 8173a04..a3fa055 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -205,6 +205,10 @@ impl Guild { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn ban<U: Into<UserId>, BO: BanOptions>(&self, user: U, options: BO) -> Result<()> { + self._ban(user.into(), options) + } + + fn _ban<BO: BanOptions>(&self, user: UserId, options: BO) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::BAN_MEMBERS; @@ -600,7 +604,9 @@ impl Guild { /// /// Requires that the current user be in the guild. #[inline] - pub fn get<G: Into<GuildId>>(guild_id: G) -> Result<PartialGuild> { guild_id.into().get() } + pub fn get<G: Into<GuildId>>(guild_id: G) -> Result<PartialGuild> { Self::_get(guild_id.into()) } + + fn _get(guild_id: GuildId) -> Result<PartialGuild> { guild_id.get() } /// Returns the formatted URL of the guild's icon, if one exists. pub fn icon_url(&self) -> Option<String> { @@ -757,15 +763,15 @@ impl Guild { /// [`User`]: struct.User.html pub fn permissions_for<C, U>(&self, channel_id: C, user_id: U) -> Permissions where C: Into<ChannelId>, U: Into<UserId> { - let user_id = user_id.into(); + self._permissions_for(channel_id.into(), user_id.into()) + } + fn _permissions_for(&self, ChannelId(cid): ChannelId, UserId(uid): UserId) -> Permissions { // The owner has all permissions in all cases. - if user_id == self.owner_id { + if self.owner_id == uid { return Permissions::all(); } - let channel_id = channel_id.into(); - // Start by retrieving the @everyone role's permissions. let everyone = match self.roles.get(&RoleId(self.id.0)) { Some(everyone) => everyone, @@ -783,7 +789,7 @@ impl Guild { // Create a base set of permissions, starting with `@everyone`s. let mut permissions = everyone.permissions; - let member = match self.members.get(&user_id) { + let member = match self.members.get(&UserId(uid)) { Some(member) => member, None => return everyone.permissions, }; @@ -806,7 +812,7 @@ impl Guild { return Permissions::all(); } - if let Some(channel) = self.channels.get(&channel_id) { + if let Some(channel) = self.channels.get(&ChannelId(cid)) { let channel = channel.read().unwrap(); // If this is a text channel, then throw out voice permissions. @@ -839,7 +845,7 @@ impl Guild { // Member for overwrite in &channel.permission_overwrites { - if PermissionOverwriteType::Member(user_id) != overwrite.kind { + if PermissionOverwriteType::Member(UserId(uid)) != overwrite.kind { continue; } @@ -849,12 +855,12 @@ impl Guild { warn!( "(╯°□°)╯︵ ┻━┻ Guild {} does not contain channel {}", self.id, - channel_id + cid ); } // The default channel is always readable. - if channel_id.0 == self.id.0 { + if cid == self.id.0 { permissions |= Permissions::READ_MESSAGES; } @@ -1007,6 +1013,10 @@ impl Guild { /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html pub fn unban<U: Into<UserId>>(&self, user_id: U) -> Result<()> { + self._unban(user_id.into()) + } + + fn _unban(&self, user_id: UserId) -> Result<()> { #[cfg(feature = "cache")] { let req = Permissions::BAN_MEMBERS; diff --git a/src/model/invite.rs b/src/model/invite.rs index b4f326c..9a836be 100644 --- a/src/model/invite.rs +++ b/src/model/invite.rs @@ -62,8 +62,11 @@ impl Invite { /// [permission]: permissions/index.html pub fn create<C, F>(channel_id: C, f: F) -> Result<RichInvite> where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite { - let channel_id = channel_id.into(); + Self::_create(channel_id.into(), f) + } + fn _create<F>(channel_id: ChannelId, f: F) -> Result<RichInvite> + where F: FnOnce(CreateInvite) -> CreateInvite { #[cfg(feature = "cache")] { let req = Permissions::CREATE_INVITE; diff --git a/src/model/user.rs b/src/model/user.rs index 3ae33aa..c3c3c0d 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -573,17 +573,19 @@ impl User { // no-cache would warn on guild_id. pub fn has_role<G, R>(&self, guild: G, role: R) -> bool where G: Into<GuildContainer>, R: Into<RoleId> { - let role_id = role.into(); + self._has_role(guild.into(), role.into()) + } - match guild.into() { - GuildContainer::Guild(guild) => guild.roles.contains_key(&role_id), + fn _has_role(&self, guild: GuildContainer, role: RoleId) -> bool { + match guild { + GuildContainer::Guild(guild) => guild.roles.contains_key(&role), GuildContainer::Id(_guild_id) => { feature_cache! {{ CACHE.read() .unwrap() .guilds .get(&_guild_id) - .map(|g| g.read().unwrap().roles.contains_key(&role_id)) + .map(|g| g.read().unwrap().roles.contains_key(&role)) .unwrap_or(false) } else { true diff --git a/src/utils/message_builder.rs b/src/utils/message_builder.rs index f58c0b3..e1ba328 100644 --- a/src/utils/message_builder.rs +++ b/src/utils/message_builder.rs @@ -122,8 +122,12 @@ impl MessageBuilder { /// [`ChannelId`]: ../model/struct.ChannelId.html /// [`GuildChannel`]: ../model/struct.GuildChannel.html /// [Display implementation]: ../model/struct.ChannelId.html#method.fmt-1 - pub fn channel<C: Into<ChannelId>>(mut self, channel: C) -> Self { - let _ = write!(self.0, "{}", channel.into().mention()); + pub fn channel<C: Into<ChannelId>>(self, channel: C) -> Self { + self._channel(channel.into()) + } + + fn _channel(mut self, channel: ChannelId) -> Self { + let _ = write!(self.0, "{}", channel.mention()); self } @@ -701,8 +705,12 @@ impl MessageBuilder { /// [`Role`]: ../model/struct.Role.html /// [`RoleId`]: ../model/struct.RoleId.html /// [Display implementation]: ../model/struct.RoleId.html#method.fmt-1 - pub fn role<R: Into<RoleId>>(mut self, role: R) -> Self { - let _ = write!(self.0, "{}", role.into().mention()); + pub fn role<R: Into<RoleId>>(self, role: R) -> Self { + self._role(role.into()) + } + + fn _role(mut self, role: RoleId) -> Self { + let _ = write!(self.0, "{}", role.mention()); self } @@ -718,8 +726,12 @@ impl MessageBuilder { /// [`User`]: ../model/struct.User.html /// [`UserId`]: ../model/struct.UserId.html /// [Display implementation]: ../model/struct.UserId.html#method.fmt-1 - pub fn user<U: Into<UserId>>(mut self, user: U) -> Self { - let _ = write!(self.0, "{}", user.into().mention()); + pub fn user<U: Into<UserId>>(self, user: U) -> Self { + self._user(user.into()) + } + + fn _user(mut self, user: UserId) -> Self { + let _ = write!(self.0, "{}", user.mention()); self } diff --git a/src/voice/manager.rs b/src/voice/manager.rs index 34d2a40..5aa392f 100644 --- a/src/voice/manager.rs +++ b/src/voice/manager.rs @@ -38,7 +38,11 @@ impl Manager { /// Retrieves a mutable handler for the given target, if one exists. pub fn get<G: Into<GuildId>>(&mut self, guild_id: G) -> Option<&mut Handler> { - self.handlers.get_mut(&guild_id.into()) + self._get(guild_id.into()) + } + + fn _get(&mut self, guild_id: GuildId) -> Option<&mut Handler> { + self.handlers.get_mut(&guild_id) } /// Connects to a target by retrieving its relevant [`Handler`] and @@ -65,9 +69,10 @@ impl Manager { #[allow(map_entry)] pub fn join<C, G>(&mut self, guild_id: G, channel_id: C) -> &mut Handler where C: Into<ChannelId>, G: Into<GuildId> { - let channel_id = channel_id.into(); - let guild_id = guild_id.into(); + self._join(guild_id.into(), channel_id.into()) + } + fn _join(&mut self, guild_id: GuildId, channel_id: ChannelId) -> &mut Handler { { let mut found = false; @@ -104,7 +109,11 @@ impl Manager { /// [`get`]: #method.get /// [`leave`]: struct.Handler.html#method.leave pub fn leave<G: Into<GuildId>>(&mut self, guild_id: G) { - if let Some(handler) = self.handlers.get_mut(&guild_id.into()) { + self._leave(guild_id.into()) + } + + fn _leave(&mut self, guild_id: GuildId) { + if let Some(handler) = self.handlers.get_mut(&guild_id) { handler.leave(); } } |