diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/dispatch.rs | 12 | ||||
| -rw-r--r-- | src/client/event_store.rs | 1 | ||||
| -rw-r--r-- | src/client/http/mod.rs | 28 | ||||
| -rw-r--r-- | src/client/http/ratelimiting.rs | 1 | ||||
| -rw-r--r-- | src/client/mod.rs | 10 | ||||
| -rw-r--r-- | src/model/channel.rs | 26 | ||||
| -rw-r--r-- | src/model/gateway.rs | 19 |
7 files changed, 95 insertions, 2 deletions
diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index a629f33..875b9b3 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -513,6 +513,18 @@ pub fn dispatch(event: Result<Event>, }); } }, + Ok(Event::ReactionRemoveAll(event)) => { + if let Some(ref handler) = handler!(on_reaction_remove_all, event_store) { + let context = context(Some(event.channel_id), + conn, + login_type); + let handler = handler.clone(); + + thread::spawn(move || { + (handler)(context, event.channel_id, event.message_id); + }); + } + }, Ok(Event::Ready(event)) => { if let Some(ref handler) = handler!(on_ready, event_store) { update!(update_with_ready, event); diff --git a/src/client/event_store.rs b/src/client/event_store.rs index 57f8dee..576fe29 100644 --- a/src/client/event_store.rs +++ b/src/client/event_store.rs @@ -57,6 +57,7 @@ pub struct EventStore { pub on_message_delete_bulk: Option<Arc<Fn(Context, ChannelId, Vec<MessageId>) + Send + Sync + 'static>>, pub on_reaction_add: Option<Arc<Fn(Context, Reaction) + Send + Sync + 'static>>, pub on_reaction_remove: Option<Arc<Fn(Context, Reaction) + Send + Sync + 'static>>, + pub on_reaction_remove_all: Option<Arc<Fn(Context, ChannelId, MessageId) + Send + Sync + 'static>>, pub on_message_update: Option<Arc<Fn(Context, MessageUpdateEvent) + Send + Sync + 'static>>, pub on_note_update: Option<Arc<Fn(Context, UserId, String) + Send + Sync + 'static>>, pub on_presence_replace: Option<Arc<Fn(Context, Vec<Presence>) + Send + Sync + 'static>>, diff --git a/src/client/http/mod.rs b/src/client/http/mod.rs index fa7e874..94fccce 100644 --- a/src/client/http/mod.rs +++ b/src/client/http/mod.rs @@ -282,6 +282,34 @@ pub fn delete_messages(channel_id: u64, map: Value) -> Result<()> { channel_id)) } +/// Delete all of the [`Reaction`]s associated with a [`Message`]. +/// +/// # Examples +/// +/// ```rust,no_run +/// use serenity::client::http; +/// use serenity::model::{ChannelId, MessageId}; +/// +/// let channel_id = ChannelId(7); +/// let message_id = MessageId(8); +/// +/// match http::delete_message_reactions(channel_id.0, message_id.0) { +/// Ok(()) => println!("Reactions deleted"), +/// Err(why) => println!("Error deleting reactions: {:?}", why), +/// } +/// ``` +/// +/// [`Message`]: ../../model/struct.Message.html +/// [`Reaction`]: ../../model/struct.Reaction.html +pub fn delete_message_reactions(channel_id: u64, message_id: u64) + -> Result<()> { + verify(204, request!(Route::ChannelsIdMessagesIdReactions(channel_id), + delete, + "/channels/{}/messages/{}/reactions", + channel_id, + message_id)) +} + pub fn delete_permission(channel_id: u64, target_id: u64) -> Result<()> { verify(204, request!(Route::ChannelsIdPermissionsOverwriteId(channel_id), diff --git a/src/client/http/ratelimiting.rs b/src/client/http/ratelimiting.rs index 9928535..0302a7d 100644 --- a/src/client/http/ratelimiting.rs +++ b/src/client/http/ratelimiting.rs @@ -62,6 +62,7 @@ pub enum Route { ChannelsIdMessagesBulkDelete(u64), ChannelsIdMessagesId(u64), ChannelsIdMessagesIdAck(u64), + ChannelsIdMessagesIdReactions(u64), ChannelsIdMessagesIdReactionsUserIdType(u64), ChannelsIdPermissionsOverwriteId(u64), ChannelsIdPins(u64), diff --git a/src/client/mod.rs b/src/client/mod.rs index 77ec3e1..4db7a4e 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -713,6 +713,16 @@ impl Client { .on_reaction_remove = Some(Arc::new(handler)) } + /// Attached a handler for when a [`ReactionRemoveAll`] is received. + /// + /// [`ReactionRemoveAll`]: ../model/enum.Event.html#variant.ReactionRemoveAll + pub fn on_reaction_remove_all<F>(&mut self, handler: F) + where F: Fn(Context, ChannelId, MessageId) + Send + Sync + 'static { + self.event_store.lock() + .unwrap() + .on_reaction_remove_all = Some(Arc::new(handler)) + } + /// Register an event to be called whenever a Ready event is received. /// /// Registering a handler for the ready event is good for noting when your diff --git a/src/model/channel.rs b/src/model/channel.rs index 178db25..989bda8 100644 --- a/src/model/channel.rs +++ b/src/model/channel.rs @@ -404,8 +404,8 @@ impl Message { /// Returns a [`ClientError::InvalidPermissions`] if the current user does /// not have the required permissions. /// - /// [`ClientError::InvalidPermissions]: ../client/enum.ClientError.html#variant.InvalidPermissions - /// [`ClientError::InvalidUser]: ../client/enum.ClientError.html#variant.InvalidUser + /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions + /// [`ClientError::InvalidUser`]: ../client/enum.ClientError.html#variant.InvalidUser /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html pub fn delete(&self) -> Result<()> { let req = permissions::MANAGE_MESSAGES; @@ -422,6 +422,28 @@ impl Message { http::delete_message(self.channel_id.0, self.id.0) } + /// Deletes all of the [`Reaction`]s associated with the message. + /// + /// **Note**: Requires the [Manage Messages] permission. + /// + /// # Errors + /// + /// Returns a [`ClientError::InvalidPermissions`] if the current user does + /// not have the required permissions. + /// + /// [`ClientError::InvalidPermissions`]: ../client/enum.ClientError.html#variant.InvalidPermissions + /// [`Reaction`]: struct.Reaction.html + /// [Manage Messages]: permissions/constant.MANAGE_MESSAGES.html + pub fn delete_reactions(&self) -> Result<()> { + let req = permissions::MANAGE_MESSAGES; + + if !try!(utils::user_has_perms(self.channel_id, req)) { + return Err(Error::Client(ClientError::InvalidPermissions(req))); + } + + http::delete_message_reactions(self.channel_id.0, self.id.0) + } + /// Edits this message, replacing the original content with new content. /// /// **Note**: You must be the author of the message to be able to do this. diff --git a/src/model/gateway.rs b/src/model/gateway.rs index dfdddf2..35a65d0 100644 --- a/src/model/gateway.rs +++ b/src/model/gateway.rs @@ -222,6 +222,12 @@ pub struct ReactionRemoveEvent { pub reaction: Reaction, } +#[derive(Clone, Copy, Debug)] +pub struct ReactionRemoveAllEvent { + pub channel_id: ChannelId, + pub message_id: MessageId, +} + /// The "Ready" event, containing initial state #[derive(Clone, Debug)] pub struct ReadyEvent { @@ -467,6 +473,14 @@ pub enum Event { /// /// [`on_message_reaction_remove`]: ../client/struct.Client.html#method.on_message_reaction_remove ReactionRemove(ReactionRemoveEvent), + /// A request was issued to remove all [`Reaction`]s from a [`Message`]. + /// + /// Fires the [`on_reaction_remove_all`] event handler. + /// + /// [`Message`]: struct.Message.html + /// [`Reaction`]: struct.Reaction.html + /// [`on_reaction_remove_all`]: ../client/struct.Clint.html#method.on_reaction_remove_all + ReactionRemoveAll(ReactionRemoveAllEvent), /// The first event in a connection, containing the initial state. /// /// May also be received at a later time in the event of a reconnect. @@ -670,6 +684,11 @@ impl Event { Ok(Event::ReactionRemove(ReactionRemoveEvent { reaction: try!(Reaction::decode(Value::Object(value))) })) + } else if kind == "MESSAGE_REACTION_REMOVE_ALL" { + Ok(Event::ReactionRemoveAll(ReactionRemoveAllEvent { + channel_id: try!(remove(&mut value, "channel_id").and_then(ChannelId::decode)), + message_id: try!(remove(&mut value, "message_id").and_then(MessageId::decode)), + })) } else if kind == "MESSAGE_UPDATE" { missing!(value, Event::MessageUpdate(MessageUpdateEvent { id: try!(remove(&mut value, "id").and_then(MessageId::decode)), |