From 47d1c52014d7eb9b76cb04aa6ec3359dd13b89a9 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:20:06 +0200 Subject: [PATCH 1/5] Change file structure --- src/api/channels/reactions.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 0fc265b..0dd2a57 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -7,12 +7,11 @@ use crate::{ }; /** -Extends the [`types::Reaction`] struct with useful metadata. +Useful metadata for working with [`types::Reaction`], bundled together nicely. */ pub struct ReactionMeta { pub message_id: types::Snowflake, pub channel_id: types::Snowflake, - pub reaction: types::Reaction, } impl ReactionMeta { @@ -131,9 +130,7 @@ impl ReactionMeta { ) .await } -} -impl types::Reaction { /** Create a reaction for the message. @@ -156,8 +153,7 @@ impl types::Reaction { See [https://discord.com/developers/docs/resources/channel#create-reaction](https://discord.com/developers/docs/resources/channel#create-reaction) */ pub async fn create( - channel_id: &Snowflake, - message_id: &Snowflake, + &self, emoji: &str, user: &mut UserMeta, ) -> Result { @@ -165,8 +161,8 @@ impl types::Reaction { let url = format!( "{}/channels/{}/messages/{}/reactions/{}/@me/", belongs_to.urls.get_api(), - channel_id, - message_id, + self.channel_id, + self.message_id, emoji ); let request = Client::new().put(url).bearer_auth(user.token()); From 213220c8e05f588550aa8151a2d4b868e37f93fc Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:29:43 +0200 Subject: [PATCH 2/5] Add delete_user method --- src/api/channels/reactions.rs | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 0dd2a57..c043f22 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -176,4 +176,51 @@ impl ReactionMeta { ) .await } + + /** + Delete a user's reaction to a message. + + This endpoint requires the MANAGE_MESSAGES permission to be present on the current user. + + # Arguments + * `user_id` - A string slice containing the ID of the user whose reaction is to be deleted. + * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or + the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the + format name:id with the emoji name and emoji id. + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Returns a 204 empty response on success. + Fires a Message Reaction Remove Gateway event. + + # Reference + See [https://discord.com/developers/docs/resources/channel#delete-own-reaction](https://discord.com/developers/docs/resources/channel#delete-own-reaction) + */ + pub async fn delete_user( + &self, + user_id: &str, + emoji: &str, + user: &mut UserMeta, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!( + "{}/channels/{}/messages/{}/reactions/{}/{}", + belongs_to.urls.get_api(), + self.channel_id, + self.message_id, + emoji, + user_id + ); + let request = Client::new().delete(url).bearer_auth(user.token()); + LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Channel, + &mut belongs_to.limits, + &mut user.limits, + ) + .await + } } From fd2b9e670fde71413335f80ac809159111a0e719 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:32:57 +0200 Subject: [PATCH 3/5] improve existing documentation --- src/api/channels/reactions.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index c043f22..52a0134 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -16,16 +16,19 @@ pub struct ReactionMeta { impl ReactionMeta { /** - Deletes all reactions for a message. - # Arguments - * `user` - A mutable reference to a [`UserMeta`] instance. + Deletes all reactions for a message. + This endpoint requires the `MANAGE_MESSAGES` permission to be present on the current user. - # Returns - A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + # Arguments + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Fires a `Message Reaction Remove All` Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#delete-all-reactions](https://discord.com/developers/docs/resources/channel#delete-all-reactions) - */ + */ pub async fn delete_all( &self, user: &mut UserMeta, @@ -91,8 +94,7 @@ impl ReactionMeta { /** Deletes all the reactions for a given `emoji` on a message. This endpoint requires the - MANAGE_MESSAGES permission to be present on the current user. Fires a `Message Reaction - Remove Emoji` Gateway event. + MANAGE_MESSAGES permission to be present on the current user. # Arguments * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or @@ -102,6 +104,7 @@ impl ReactionMeta { # Returns A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Fires a `Message Reaction Remove Emoji` Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji](https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji) @@ -137,8 +140,7 @@ impl ReactionMeta { This endpoint requires the READ_MESSAGE_HISTORY permission to be present on the current user. Additionally, if nobody else has reacted to the message using this emoji, this endpoint requires the ADD_REACTIONS permission to be present on the current - user. Fires a Message Reaction Add Gateway event. - + user. # Arguments * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the @@ -148,6 +150,7 @@ impl ReactionMeta { # Returns A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. Returns a 204 empty response on success. + Fires a Message Reaction Add Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#create-reaction](https://discord.com/developers/docs/resources/channel#create-reaction) From 471abc88a62fc70994bdd0befe4da4dac065ef43 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:39:22 +0200 Subject: [PATCH 4/5] Add method to remove own reaction --- src/api/channels/reactions.rs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 52a0134..f95db3a 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -180,6 +180,45 @@ impl ReactionMeta { .await } + /** + Delete a reaction the current user has made for the message. + + # Arguments + * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or + the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the + format name:id with the emoji name and emoji id. + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Returns a 204 empty response on success. + Fires a `Message Reaction Remove` Gateway event. + */ + pub async fn remove( + &self, + emoji: &str, + user: &mut UserMeta, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!( + "{}/channels/{}/messages/{}/reactions/{}/@me/", + belongs_to.urls.get_api(), + self.channel_id, + self.message_id, + emoji + ); + let request = Client::new().delete(url).bearer_auth(user.token()); + LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Channel, + &mut belongs_to.limits, + &mut user.limits, + ) + .await + } + /** Delete a user's reaction to a message. From 7a3f485580f218d424973451129e79e15cf89993 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:41:24 +0200 Subject: [PATCH 5/5] Add reference to docs --- src/api/channels/reactions.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index f95db3a..0ca8e26 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -193,6 +193,9 @@ impl ReactionMeta { A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. Returns a 204 empty response on success. Fires a `Message Reaction Remove` Gateway event. + + # Reference + See [https://discord.com/developers/docs/resources/channel#delete-own-reaction](https://discord.com/developers/docs/resources/channel#delete-own-reaction) */ pub async fn remove( &self,