From b7cdf1542cb9199c61c0b17bdd381d4f117f635e Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Sun, 15 Oct 2017 17:09:33 +0200 Subject: Hash and do equality on just the id for `User` --- src/model/user.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/model') diff --git a/src/model/user.rs b/src/model/user.rs index 39efa10..c1c3135 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -343,7 +343,7 @@ impl Default for OnlineStatus { } /// Information about a user. -#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Hash)] +#[derive(Clone, Debug, Deserialize)] pub struct User { /// The unique Id of the user. Can be used to calculate the account's /// cration date. @@ -365,6 +365,22 @@ pub struct User { pub name: String, } +use std::hash::{Hash, Hasher}; + +impl PartialEq for User { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +impl Eq for User {} + +impl Hash for User { + fn hash(&self, hasher: &mut H) { + self.id.hash(hasher); + } +} + #[cfg(feature = "model")] impl User { /// Returns the formatted URL of the user's icon, if one exists. -- cgit v1.2.3 From c7aa27dbb64e64d70c7f13725c79017c4bba1c95 Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Mon, 16 Oct 2017 14:39:57 +0200 Subject: defer to `delete_message` if there's just one message to delete --- src/model/channel/channel_id.rs | 10 +++++++--- src/model/channel/guild_channel.rs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'src/model') diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs index f728191..2e415dc 100644 --- a/src/model/channel/channel_id.rs +++ b/src/model/channel/channel_id.rs @@ -122,10 +122,14 @@ impl ChannelId { .into_iter() .map(|message_id| message_id.as_ref().0) .collect::>(); + + if ids.len() == 1 { + self.delete_message(ids[0]) + } else { + let map = json!({ "messages": ids }); - let map = json!({ "messages": ids }); - - http::delete_messages(self.0, &map) + http::delete_messages(self.0, &map) + } } /// Deletes all permission overrides in the channel from a member or role. diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs index e2e5f09..d6649b4 100644 --- a/src/model/channel/guild_channel.rs +++ b/src/model/channel/guild_channel.rs @@ -258,7 +258,7 @@ impl GuildChannel { /// [`Channel::delete_messages`]: enum.Channel.html#method.delete_messages /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[inline] - pub fn delete_messages<'a, It: IntoIterator>(&self, message_ids: It) -> Result<()> { + pub fn delete_messages, It: IntoIterator>(&self, message_ids: It) -> Result<()> { self.id.delete_messages(message_ids) } -- cgit v1.2.3 From e57b510edd640abb243664337a1c163924313612 Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Tue, 17 Oct 2017 22:24:12 +0200 Subject: Use the underlaying integer value of a `ChannelType` variant --- src/model/guild/guild_id.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/model') diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 4b9a713..64e1181 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -110,7 +110,7 @@ impl GuildId { pub fn create_channel(&self, name: &str, kind: ChannelType) -> Result { let map = json!({ "name": name, - "type": kind.name(), + "type": kind as u8, }); http::create_channel(self.0, &map) -- cgit v1.2.3 From f47a0c831efe5842ca38cb1067de361ae42f6edc Mon Sep 17 00:00:00 2001 From: Ken Swenson Date: Thu, 19 Oct 2017 08:27:40 -0400 Subject: Implement changing a role's position (#201) --- src/model/guild/guild_id.rs | 21 +++++++++++++++++++++ src/model/guild/mod.rs | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) (limited to 'src/model') diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index 64e1181..622d059 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -303,6 +303,27 @@ impl GuildId { http::edit_role(self.0, role_id.into().0, &f(EditRole::default()).0) } + /// Edits the order of [`Role`]s + /// Requires the [Manage Roles] permission. + /// + /// # Examples + /// + /// Change the order of a role: + /// + /// ```rust,ignore + /// use serenity::model::{GuildId, RoleId}; + /// GuildId(7).edit_role_position(RoleId(8), 2); + /// ``` + /// + /// [`Role`]: struct.Role.html + /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html + #[inline] + pub fn edit_role_position(&self, role_id: R, position: u64) -> Result> + where R: Into { + http::edit_role_position(self.0, role_id.into().0, position) + } + + /// Search the cache for the guild. #[cfg(feature = "cache")] pub fn find(&self) -> Option>> { CACHE.read().unwrap().guild(*self) } diff --git a/src/model/guild/mod.rs b/src/model/guild/mod.rs index 5a5ab63..aa9fd1d 100644 --- a/src/model/guild/mod.rs +++ b/src/model/guild/mod.rs @@ -597,6 +597,26 @@ impl Guild { self.id.edit_role(role_id, f) } + /// Edits the order of [`Role`]s + /// Requires the [Manage Roles] permission. + /// + /// # Examples + /// + /// Change the order of a role: + /// + /// ```rust,ignore + /// use serenity::model::RoleId; + /// guild.edit_role_position(RoleId(8), 2); + /// ``` + /// + /// [`Role`]: struct.Role.html + /// [Manage Roles]: permissions/constant.MANAGE_ROLES.html + #[inline] + pub fn edit_role_position(&self, role_id: R, position: u64) -> Result> + where R: Into { + self.id.edit_role_position(role_id, position) + } + /// Gets a partial amount of guild data by its Id. /// /// Requires that the current user be in the guild. -- cgit v1.2.3 From 23ff6f21019bc94f8dc32355fa34691b881bfb69 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Mon, 16 Oct 2017 20:19:27 -0700 Subject: Deprecate some text-only Channel methods Some methods on `model::Channel` are only for text channels. Methods on Channel should be applicable to all channel types. Deprecates the following methods on `model::Channel`: - `create_reaction` - `delete_message` - `delete_reaction` - `edit_message` - `message` - `messages` - `reaction_users` - `say` - `send_files` - `send_message` - `unpin` --- src/model/channel/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/model') diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs index 4ba0322..07a73d7 100644 --- a/src/model/channel/mod.rs +++ b/src/model/channel/mod.rs @@ -67,6 +67,7 @@ impl Channel { /// [`Message::react`]: struct.Message.html#method.react /// [Add Reactions]: permissions/constant.ADD_REACTIONS.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn create_reaction(&self, message_id: M, reaction_type: R) -> Result<()> where M: Into, R: Into { @@ -110,6 +111,7 @@ impl Channel { /// [`Message::delete`]: struct.Message.html#method.delete /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn delete_message>(&self, message_id: M) -> Result<()> { self.id().delete_message(message_id) @@ -123,6 +125,7 @@ impl Channel { /// [`Reaction`]: struct.Reaction.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn delete_reaction(&self, message_id: M, @@ -154,6 +157,7 @@ impl Channel { /// [`Message`]: struct.Message.html /// [`the limit`]: ../builder/struct.CreateMessage.html#method.content #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn edit_message(&self, message_id: M, f: F) -> Result where F: FnOnce(CreateMessage) -> CreateMessage, M: Into { @@ -181,6 +185,7 @@ impl Channel { /// /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn message>(&self, message_id: M) -> Result { self.id().message(message_id) @@ -203,6 +208,7 @@ impl Channel { /// /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn messages(&self, f: F) -> Result> where F: FnOnce(GetMessages) -> GetMessages { @@ -226,6 +232,7 @@ impl Channel { /// [`User`]: struct.User.html /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn reaction_users(&self, message_id: M, @@ -264,6 +271,7 @@ impl Channel { /// [`ChannelId`]: struct.ChannelId.html /// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn say(&self, content: &str) -> Result { self.id().say(content) } @@ -286,6 +294,7 @@ impl Channel { /// [Attach Files]: permissions/constant.ATTACH_FILES.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn send_files<'a, F, T, It: IntoIterator>(&self, files: It, f: F) -> Result where F: FnOnce(CreateMessage) -> CreateMessage, T: Into> { @@ -312,6 +321,7 @@ impl Channel { /// [`CreateMessage`]: ../builder/struct.CreateMessage.html /// [Send Messages]: permissions/constant.SEND_MESSAGES.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn send_message(&self, f: F) -> Result where F: FnOnce(CreateMessage) -> CreateMessage { @@ -325,6 +335,7 @@ impl Channel { /// [`Message`]: struct.Message.html /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html #[cfg(feature = "model")] + #[deprecated(since = "0.4.2", note = "Use the inner channel's method")] #[inline] pub fn unpin>(&self, message_id: M) -> Result<()> { self.id().unpin(message_id) -- cgit v1.2.3 From e02d5fb8171b11214e1502c6754fef1972bbf1b9 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 23 Oct 2017 21:10:47 +0200 Subject: Properly update emojis, fix shard retries, fix cs * If a guild's emojis are being altered, Serenity will straight up use the new `HashMap` instead of just extending. If `connect()` returns an `Err`, it will retry connecting. Cleaned up `help_command.rs`. --- src/model/event.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/model') diff --git a/src/model/event.rs b/src/model/event.rs index 086f422..d4148b9 100644 --- a/src/model/event.rs +++ b/src/model/event.rs @@ -404,7 +404,9 @@ impl CacheUpdate for GuildEmojisUpdateEvent { fn update(&mut self, cache: &mut Cache) -> Option<()> { cache.guilds.get_mut(&self.guild_id).map(|guild| { - guild.with_mut(|g| g.emojis.extend(self.emojis.clone())) + guild.with_mut(|g| { + g.emojis.clone_from(&self.emojis) + }); }); None -- cgit v1.2.3 From d3015a0ff0c0c87888437f991945453b92296875 Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Tue, 24 Oct 2017 07:52:24 -0700 Subject: Fix User::has_role Fix the return value of the function by properly checking whether the user has the role in the given guild. The function made the erraneous mistake of simply checking if the given guild had the role by Id, not whether the _member in the given guild_ had the role by Id. --- src/model/user.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/model') diff --git a/src/model/user.rs b/src/model/user.rs index c1c3135..3d39759 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -599,7 +599,11 @@ impl User { .unwrap() .guilds .get(&_guild_id) - .map(|g| g.read().unwrap().roles.contains_key(&role_id)) + .map(|g| { + g.read().unwrap().members.get(&self.id) + .map(|m| m.roles.contains(&role_id)) + .unwrap_or(false) + }) .unwrap_or(false) } else { true -- cgit v1.2.3 From 8c85664a94f7439ab4bc3a132f313a9e26d94fe7 Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Tue, 24 Oct 2017 18:05:25 +0200 Subject: Fall back to `str::parse` if `parse_username` fails --- src/model/misc.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/model') diff --git a/src/model/misc.rs b/src/model/misc.rs index af49a76..3398a14 100644 --- a/src/model/misc.rs +++ b/src/model/misc.rs @@ -125,9 +125,10 @@ impl FromStr for UserId { type Err = UserIdParseError; fn from_str(s: &str) -> StdResult { - utils::parse_username(s) - .ok_or_else(|| UserIdParseError::InvalidFormat) - .map(UserId) + Ok(match utils::parse_username(s) + Some(id) => UserId(id), + None => s.parse::().map(UserId).map_err(|_| UserIdParseError::InvalidFormat) + }) } } -- cgit v1.2.3