aboutsummaryrefslogtreecommitdiff
path: root/src/client/context.rs
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-05 14:44:11 -0700
committerAustin Hellyer <[email protected]>2016-11-05 14:44:11 -0700
commite3416d3f511894553b7625c501043077977ccb4d (patch)
treec2f9f5d35a90c649dcc67c6d15e4a9ae8478cdeb /src/client/context.rs
parentUpdate URLs to GitLab (diff)
downloadserenity-e3416d3f511894553b7625c501043077977ccb4d.tar.xz
serenity-e3416d3f511894553b7625c501043077977ccb4d.zip
Add message reactions
Add message reaction structs and an enum to differentiate between the two types of reactions, as well as event decoding and event handlers with dispatches. The following is, more or less, what is added: - `reactions` field to the `Message` struct; - `MessageReaction` struct, which is a consolidated form of reaction, containing the type of reaction, the number of them, and whether the current user has performed that type of reaction; - `Reaction`, a struct containing the information about a reaction - `ReactionType`, an enum to differentiate between the two types of reactions: `Custom` (a guild's custom emoji) and `Unicode` (twemoji); - Decoding for `MESSAGE_REACTION_ADD` and `MESSAGE_REACTION_REMOVE`; - Permission flag `ADD_REACTIONS`; - `Message::react` method; - Three `http` payload senders: `create_reaction`, `delete_reaction`, and `get_reaction_users`; - Three `Context` methods of equal names to the above.
Diffstat (limited to 'src/client/context.rs')
-rw-r--r--src/client/context.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/client/context.rs b/src/client/context.rs
index 6402e9b..2d3d3e4 100644
--- a/src/client/context.rs
+++ b/src/client/context.rs
@@ -232,6 +232,26 @@ impl Context {
http::create_private_channel(map)
}
+ /// React to a [`Message`] with a custom [`Emoji`] or unicode character.
+ ///
+ /// **Note**: Requires the [Add Reactions] permission.
+ ///
+ /// [`Emoji`]: ../models/struct.Emoji.html
+ /// [`Message`]: ../models/struct.Message.html
+ /// [Add Reactions]: ../models/permissions/constant.ADD_REACTIONS.html
+ pub fn create_reaction<C, M, R>(&self,
+ channel_id: C,
+ message_id: M,
+ reaction_type: R)
+ -> Result<()>
+ where C: Into<ChannelId>,
+ M: Into<MessageId>,
+ R: Into<ReactionType> {
+ http::create_reaction(channel_id.into().0,
+ message_id.into().0,
+ reaction_type.into())
+ }
+
pub fn create_role<F, G>(&self, guild_id: G, f: F) -> Result<Role>
where F: FnOnce(EditRole) -> EditRole, G: Into<GuildId> {
let id = guild_id.into().0;
@@ -331,6 +351,30 @@ impl Context {
http::delete_permission(channel_id.into().0, id)
}
+
+ /// Deletes the given [`Reaction`], but only if the current user is the user
+ /// who made the reaction or has permission to.
+ ///
+ /// **Note**: Requires the [`Manage Messages`] permission, _if_ the current
+ /// user did not perform the reaction.
+ ///
+ /// [`Reaction`]: ../models/struct.Reaction.html
+ /// [Manage Messages]: ../models/permissions/constant.MANAGE_MESSAGES.html
+ pub fn delete_reaction<C, M, R>(&self,
+ channel_id: C,
+ message_id: M,
+ user_id: Option<UserId>,
+ reaction_type: R)
+ -> Result<()>
+ where C: Into<ChannelId>,
+ M: Into<MessageId>,
+ R: Into<ReactionType> {
+ http::delete_reaction(channel_id.into().0,
+ message_id.into().0,
+ user_id.map(|uid| uid.0),
+ reaction_type.into())
+ }
+
pub fn delete_role<G, R>(&self, guild_id: G, role_id: R) -> Result<()>
where G: Into<GuildId>, R: Into<RoleId> {
http::delete_role(guild_id.into().0, role_id.into().0)
@@ -650,6 +694,48 @@ impl Context {
http::get_messages(channel_id.into().0, &query)
}
+ /// Retrieves the list of [`User`]s who have reacted to a [`Message`] with a
+ /// certain [`Emoji`].
+ ///
+ /// The default `limit` is `50` - specify otherwise to receive a different
+ /// maximum number of users. The maximum that may be retrieve at a time is
+ /// `100`, if a greater number is provided then it is automatically reduced.
+ ///
+ /// The optional `after` attribute is to retrieve the users after a certain
+ /// user. This is useful for pagination.
+ ///
+ /// **Note**: Requires the [Read Message History] permission.
+ ///
+ /// # Errors
+ ///
+ /// Returns a [`ClientError::InvalidPermissions`] if the current user does
+ /// not have the required [permissions].
+ ///
+ /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions
+ /// [`Emoji`]: struct.Emoji.html
+ /// [`Message`]: struct.Message.html
+ /// [`User`]: struct.User.html
+ /// [Read Message History]: permissions/constant.READ_MESSAGE_HISTORY.html
+ pub fn get_reaction_users<C, M, R, U>(&self,
+ channel_id: C,
+ message_id: M,
+ reaction_type: R,
+ limit: Option<u8>,
+ after: Option<U>)
+ -> Result<Vec<User>>
+ where C: Into<ChannelId>,
+ M: Into<MessageId>,
+ R: Into<ReactionType>,
+ U: Into<UserId> {
+ let limit = limit.map(|x| if x > 100 { 100 } else { x }).unwrap_or(50);
+
+ http::get_reaction_users(channel_id.into().0,
+ message_id.into().0,
+ reaction_type.into(),
+ limit,
+ after.map(|u| u.into().0))
+ }
+
pub fn get_voice_regions(&self) -> Result<Vec<VoiceRegion>> {
http::get_voice_regions()
}