Skip to content

Commit

Permalink
Add support for Application Emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Jul 20, 2024
1 parent 8bbc1de commit cd03cc9
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 4 deletions.
56 changes: 56 additions & 0 deletions src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::gateway::ActivityData;
#[cfg(feature = "gateway")]
use crate::gateway::{ShardMessenger, ShardRunner};
use crate::http::Http;
use crate::json::json;
use crate::model::prelude::*;

/// The context is a general utility struct provided on event dispatches, which helps with dealing
Expand Down Expand Up @@ -308,6 +309,61 @@ impl Context {
pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus) {
self.shard.set_presence(activity, status);
}

/// Gets all emojis for the current application.
///
/// # Errors
///
/// Returns an error if the Application ID is not known.
pub async fn get_application_emojis(&self) -> Result<Vec<Emoji>> {
self.http.get_application_emojis().await
}

/// Gets information about an application emoji.
///
/// # Errors
///
/// Returns an error if the emoji does not exist.
pub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji> {
self.http.get_application_emoji(emoji_id).await
}

/// Creates an application emoji with a name and base64-encoded image.
///
/// # Errors
///
/// See [`Guild::create_emoji`] for information about name and filesize requirements. This
/// method will error if said requirements are not met.
pub async fn create_application_emoji(&self, name: &str, image: &str) -> Result<Emoji> {
let map = json!({
"name": name,
"image": image,
});

self.http.create_application_emoji(&map).await
}

/// Changes the name of an application emoji.
///
/// # Errors
///
/// Returns an error if the emoji does not exist.
pub async fn edit_application_emoji(&self, emoji_id: EmojiId, name: &str) -> Result<Emoji> {
let map = json!({
"name": name,
});

self.http.edit_application_emoji(emoji_id, &map).await
}

/// Deletes an application emoji.
///
/// # Errors
///
/// Returns an error if the emoji does not exist.
pub async fn delete_application_emoji(&self, emoji_id: EmojiId) -> Result<()> {
self.http.delete_application_emoji(emoji_id).await
}
}

impl AsRef<Http> for Context {
Expand Down
103 changes: 99 additions & 4 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ impl Http {

/// Creates an emoji in the given [`Guild`] with the given data.
///
/// View the source code for [`Guild::create_emoji`] method to see what fields this requires.
/// See [`Guild::create_emoji`] for required fields.
///
/// **Note**: Requires the [Create Guild Expressions] permission.
///
Expand All @@ -511,6 +511,25 @@ impl Http {
.await
}

/// Creates an application emoji with the given data.
///
/// See [`Context::create_application_emoji`] for required fields.
///
/// [`Context::create_application_emoji`]: crate::client::Context::create_application_emoji
pub async fn create_application_emoji(&self, map: &Value) -> Result<Emoji> {
self.fire(Request {
body: Some(to_vec(map)?),
multipart: None,
headers: None,
method: LightMethod::Post,
route: Route::Emojis {
application_id: self.try_application_id()?,
},
params: None,
})
.await
}

/// Create a follow-up message for an Interaction.
///
/// Functions the same as [`Self::execute_webhook`]
Expand Down Expand Up @@ -1055,9 +1074,9 @@ impl Http {
.await
}

/// Deletes an emoji from a server.
/// Deletes an emoji from a guild.
///
/// See [`GuildId::edit_emoji`] for permissions requirements.
/// See [`GuildId::delete_emoji`] for permissions requirements.
pub async fn delete_emoji(
&self,
guild_id: GuildId,
Expand All @@ -1078,6 +1097,22 @@ impl Http {
.await
}

/// Deletes an application emoji.
pub async fn delete_application_emoji(&self, emoji_id: EmojiId) -> Result<()> {
self.wind(204, Request {
body: None,
multipart: None,
headers: None,
method: LightMethod::Delete,
route: Route::Emoji {
application_id: self.try_application_id()?,
emoji_id,
},
params: None,
})
.await
}

/// Deletes a follow-up message for an interaction.
pub async fn delete_followup_message(
&self,
Expand Down Expand Up @@ -1583,7 +1618,7 @@ impl Http {
.await
}

/// Changes emoji information.
/// Changes guild emoji information.
///
/// See [`GuildId::edit_emoji`] for permissions requirements.
pub async fn edit_emoji(
Expand All @@ -1609,6 +1644,26 @@ impl Http {
.await
}

/// Changes application emoji information.
///
/// See [`Context::edit_application_emoji`] for required fields.
///
/// [`Context::edit_application_emoji`]: crate::client::Context::edit_application_emoji
pub async fn edit_application_emoji(&self, emoji_id: EmojiId, map: &Value) -> Result<Emoji> {
self.fire(Request {
body: Some(to_vec(map)?),
multipart: None,
headers: None,
method: LightMethod::Patch,
route: Route::Emoji {
application_id: self.try_application_id()?,
emoji_id,
},
params: None,
})
.await
}

/// Edits a follow-up message for an interaction.
///
/// Refer to Discord's [docs] for Edit Webhook Message for field information.
Expand Down Expand Up @@ -3263,6 +3318,46 @@ impl Http {
.await
}

/// Gets all emojis for the current application.
pub async fn get_application_emojis(&self) -> Result<Vec<Emoji>> {
// Why, discord...
#[derive(Deserialize)]
struct ApplicationEmojis {
items: Vec<Emoji>,
}

let result: ApplicationEmojis = self
.fire(Request {
body: None,
multipart: None,
headers: None,
method: LightMethod::Get,
route: Route::Emojis {
application_id: self.try_application_id()?,
},
params: None,
})
.await?;

Ok(result.items)
}

/// Gets information about an application emoji.
pub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji> {
self.fire(Request {
body: None,
multipart: None,
headers: None,
method: LightMethod::Get,
route: Route::Emoji {
application_id: self.try_application_id()?,
emoji_id,
},
params: None,
})
.await
}

#[allow(clippy::too_many_arguments)]
/// Gets all entitlements for the current app, active and expired.
pub async fn get_entitlements(
Expand Down
8 changes: 8 additions & 0 deletions src/http/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ routes! ('a, {
api!("/applications/{}/skus", application_id),
Some(RatelimitingKind::PathAndId(application_id.into()));

Emoji { application_id: ApplicationId, emoji_id: EmojiId },
api!("/applications/{}/emojis/{}", application_id, emoji_id),
Some(RatelimitingKind::PathAndId(application_id.into()));

Emojis { application_id: ApplicationId },
api!("/applications/{}/emojis", application_id),
Some(RatelimitingKind::PathAndId(application_id.into()));

Entitlement { application_id: ApplicationId, entitlement_id: EntitlementId },
api!("/applications/{}/entitlements/{}", application_id, entitlement_id),
Some(RatelimitingKind::PathAndId(application_id.into()));
Expand Down

0 comments on commit cd03cc9

Please sign in to comment.