diff options
| author | Austin Hellyer <[email protected]> | 2017-01-24 08:02:06 -0800 |
|---|---|---|
| committer | Austin Hellyer <[email protected]> | 2017-01-24 08:02:06 -0800 |
| commit | 324ff609bb8d87f5812af6f9aa69df2579f7c302 (patch) | |
| tree | 76fcb92e39d7a856bb66bd996aebc18a20bfa037 /src/model | |
| parent | Add GuildId::as_channel_id() (diff) | |
| download | serenity-324ff609bb8d87f5812af6f9aa69df2579f7c302.tar.xz serenity-324ff609bb8d87f5812af6f9aa69df2579f7c302.zip | |
Use Id methods where possible
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/channel.rs | 35 | ||||
| -rw-r--r-- | src/model/guild.rs | 53 | ||||
| -rw-r--r-- | src/model/user.rs | 1 | ||||
| -rw-r--r-- | src/model/webhook.rs | 2 |
4 files changed, 32 insertions, 59 deletions
diff --git a/src/model/channel.rs b/src/model/channel.rs index bda26e5..931524e 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -1128,7 +1128,7 @@ impl Group { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn unpin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - self.channel_id.unpin(message_id.into().0) + self.channel_id.unpin(message_id) } } @@ -1571,12 +1571,12 @@ impl PrivateChannel { } } - rest::ack_message(self.id.0, message_id.into().0) + self.id.ack(message_id) } /// Broadcasts that the current user is typing to the recipient. pub fn broadcast_typing(&self) -> Result<()> { - rest::broadcast_typing(self.id.0) + self.id.broadcast_typing() } /// React to a [`Message`] with a custom [`Emoji`] or unicode character. @@ -1764,13 +1764,7 @@ impl PrivateChannel { return Err(Error::Client(ClientError::MessageTooLong(length_over))); } - let map = ObjectBuilder::new() - .insert("content", content) - .insert("nonce", "") - .insert("tts", false) - .build(); - - rest::send_message(self.id.0, map) + self.id.send_message(|m| m.content(content)) } /// Unpins a [`Message`] in the channel given by its Id. @@ -1781,7 +1775,7 @@ impl PrivateChannel { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn unpin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - self.id.unpin(message_id.into().0) + self.id.unpin(message_id) } } @@ -1833,7 +1827,7 @@ impl GuildChannel { /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Send Messages]: permissions/constants.SEND_MESSAGES.html pub fn broadcast_typing(&self) -> Result<()> { - rest::broadcast_typing(self.id.0) + self.id.broadcast_typing() } /// Creates an invite leading to the given channel. @@ -2151,8 +2145,7 @@ impl GuildChannel { /// [`ClientError::InvalidOperationAsBot`]: ../client/enum.ClientError.html#variant.InvalidOperationAsBot /// [`Message`]: struct.Message.html /// [`Search`]: ../utils/builder/struct.Search.html - pub fn search<F>(&self, f: F) -> Result<SearchResult> - where F: FnOnce(Search) -> Search { + pub fn search<F: FnOnce(Search) -> Search>(&self, f: F) -> Result<SearchResult> { #[cfg(feature="cache")] { if CACHE.read().unwrap().user.bot { @@ -2160,7 +2153,7 @@ impl GuildChannel { } } - rest::search_channel_messages(self.id.0, f(Search::default()).0) + self.id.search(f) } /// Sends a message to the channel with the given content. @@ -2196,13 +2189,7 @@ impl GuildChannel { } } - let map = ObjectBuilder::new() - .insert("content", content) - .insert("nonce", "") - .insert("tts", false) - .build(); - - rest::send_message(self.id.0, map) + self.id.send_message(|m| m.content(content)) } /// Unpins a [`Message`] in the channel given by its Id. @@ -2213,7 +2200,7 @@ impl GuildChannel { /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] pub fn unpin<M: Into<MessageId>>(&self, message_id: M) -> Result<()> { - self.id.unpin(message_id.into().0) + self.id.unpin(message_id) } /// Retrieves the channel's webhooks. @@ -2223,7 +2210,7 @@ impl GuildChannel { /// [Manage Webhooks]: permissions/constant.MANAGE_WEBHOOKS.html #[inline] pub fn webhooks(&self) -> Result<Vec<Webhook>> { - rest::get_channel_webhooks(self.id.0) + self.id.webhooks() } } diff --git a/src/model/guild.rs b/src/model/guild.rs index 7dd72d9..e356385 100644 --- a/src/model/guild.rs +++ b/src/model/guild.rs @@ -210,7 +210,7 @@ impl Guild { } } - self.id.ban(user.into(), delete_message_days) + self.id.ban(user, delete_message_days) } /// Retrieves a list of [`Ban`]s for the guild. @@ -263,8 +263,7 @@ impl Guild { /// [`Shard`]: ../gateway/struct.Shard.html /// [US West region]: enum.Region.html#variant.UsWest /// [whitelist]: https://discordapp.com/developers/docs/resources/guild#create-guild - pub fn create(name: &str, region: Region, icon: Option<&str>) - -> Result<PartialGuild> { + pub fn create(name: &str, region: Region, icon: Option<&str>) -> Result<PartialGuild> { let map = ObjectBuilder::new() .insert("icon", icon) .insert("name", name) @@ -296,8 +295,7 @@ impl Guild { /// [`Channel`]: struct.Channel.html /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions /// [Manage Channels]: permissions/constants.MANAGE_CHANNELS.html - pub fn create_channel(&mut self, name: &str, kind: ChannelType) - -> Result<Channel> { + pub fn create_channel(&mut self, name: &str, kind: ChannelType) -> Result<Channel> { #[cfg(feature="cache")] { let req = permissions::MANAGE_CHANNELS; @@ -838,8 +836,8 @@ impl Guild { /// /// [Move Members]: permissions/constant.MOVE_MEMBERS.html #[inline] - pub fn move_member<C, U>(&self, user_id: U, channel_id: C) - -> Result<()> where C: Into<ChannelId>, U: Into<UserId> { + pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> + where C: Into<ChannelId>, U: Into<UserId> { self.id.move_member(user_id, channel_id) } @@ -991,7 +989,7 @@ impl Guild { } } - rest::search_guild_messages(self.id.0, &[], f(Search::default()).0) + self.id.search(f) } /// Performs a search request to the API for the guild's [`Message`]s in @@ -1311,9 +1309,7 @@ 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> { - let map = ObjectBuilder::new() - .insert("name", name) - .build(); + let map = ObjectBuilder::new().insert("name", name).build(); rest::edit_emoji(self.0, emoji_id.into().0, map) } @@ -1477,9 +1473,7 @@ impl GuildId { /// [`Member`]: struct.Member.html /// [Kick Members]: permissions/constant.KICK_MEMBERS.html pub fn get_prune_count(&self, days: u16) -> Result<GuildPrune> { - let map = ObjectBuilder::new() - .insert("days", days) - .build(); + let map = ObjectBuilder::new().insert("days", days).build(); rest::get_guild_prune_count(self.0, map) } @@ -1508,9 +1502,7 @@ 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> { - let map = ObjectBuilder::new() - .insert("channel_id", channel_id.into().0) - .build(); + let map = ObjectBuilder::new().insert("channel_id", channel_id.into().0).build(); rest::edit_member(self.0, user_id.into().0, map) } @@ -1574,12 +1566,9 @@ impl GuildId { /// [`GuildPrune`]: struct.GuildPrune.html /// [`Member`]: struct.Member.html /// [Kick Members]: permissions/constant.KICK_MEMBERS.html + #[inline] pub fn start_prune(&self, days: u16) -> Result<GuildPrune> { - let map = ObjectBuilder::new() - .insert("days", days) - .build(); - - rest::start_guild_prune(self.0, map) + rest::start_guild_prune(self.0, ObjectBuilder::new().insert("days", days).build()) } /// Unbans a [`User`] from the guild. @@ -1763,8 +1752,7 @@ impl Member { /// [`Context::edit_member`]: ../client/struct.Context.html#method.edit_member /// [`EditMember`]: ../builder/struct.EditMember.html #[cfg(feature="cache")] - pub fn edit<F>(&self, f: F) -> Result<()> - where F: FnOnce(EditMember) -> EditMember { + pub fn edit<F: FnOnce(EditMember) -> EditMember>(&self, f: F) -> Result<()> { let guild_id = self.find_guild()?; let map = f(EditMember::default()).0.build(); @@ -1928,13 +1916,12 @@ impl PartialGuild { /// [`ClientError::DeleteMessageDaysAmount`]: ../client/enum.ClientError.html#variant.DeleteMessageDaysAmount /// [`User`]: struct.User.html /// [Ban Members]: permissions/constant.BAN_MEMBERS.html - pub fn ban<U: Into<UserId>>(&self, user: U, delete_message_days: u8) - -> Result<()> { + pub fn ban<U: Into<UserId>>(&self, user: U, delete_message_days: u8) -> Result<()> { if delete_message_days > 7 { return Err(Error::Client(ClientError::DeleteMessageDaysAmount(delete_message_days))); } - self.id.ban(user.into(), delete_message_days) + self.id.ban(user, delete_message_days) } /// Creates a [`GuildChannel`] in the guild. @@ -2271,8 +2258,8 @@ impl PartialGuild { /// /// [Move Members]: permissions/constant.MOVE_MEMBERS.html #[inline] - pub fn move_member<C, U>(&self, user_id: U, channel_id: C) - -> Result<()> where C: Into<ChannelId>, U: Into<UserId> { + pub fn move_member<C, U>(&self, user_id: U, channel_id: C) -> Result<()> + where C: Into<ChannelId>, U: Into<UserId> { self.id.move_member(user_id, channel_id) } @@ -2300,8 +2287,7 @@ impl PartialGuild { /// [`Search`]: ../utils/builder/struct.Search.html /// [`search_channels`]: #method.search_channels /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html - pub fn search<F>(&self, f: F) -> Result<SearchResult> - where F: FnOnce(Search) -> Search { + pub fn search<F: FnOnce(Search) -> Search>(&self, f: F) -> Result<SearchResult> { #[cfg(feature="cache")] { if CACHE.read().unwrap().user.bot { @@ -2438,10 +2424,9 @@ impl Role { /// /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html #[cfg(feature="cache")] + #[inline] pub fn delete(&self) -> Result<()> { - let guild_id = self.find_guild()?; - - rest::delete_role(guild_id.0, self.id.0) + rest::delete_role(self.find_guild()?.0, self.id.0) } /// Edits a [`Role`], optionally setting its new fields. diff --git a/src/model/user.rs b/src/model/user.rs index 4eaf65e..42e8f32 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -43,6 +43,7 @@ impl CurrentUser { } /// Returns the DiscordTag of a User. + #[inline] pub fn distinct(&self) -> String { format!("{}#{}", self.name, self.discriminator) } diff --git a/src/model/webhook.rs b/src/model/webhook.rs index f1a664d..7448fcf 100644 --- a/src/model/webhook.rs +++ b/src/model/webhook.rs @@ -155,7 +155,7 @@ impl Webhook { pub fn refresh(&mut self) -> Result<()> { match rest::get_webhook_with_token(self.id.0, &self.token) { Ok(replacement) => { - mem::replace(self, replacement); + let _ = mem::replace(self, replacement); Ok(()) }, |