Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(http): Add support for application emojis #2364

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions twilight-http-ratelimiting/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ pub enum Path {
ApplicationCommand(u64),
/// Operating on a specific command.
ApplicationCommandId(u64),
/// Operating on application emojis.
ApplicationEmojis(u64),
/// Operating on a specific application emoji.
ApplicationEmoji(u64),
/// Operating on commands in a guild.
ApplicationGuildCommand(u64),
/// Operating on a specific command in a guild.
Expand Down Expand Up @@ -344,6 +348,7 @@ impl FromStr for Path {
["applications", id, "commands"] => ApplicationCommand(parse_id(id)?),
["applications", id, "commands", _] => ApplicationCommandId(parse_id(id)?),
["applications", id, "entitlements"] => ApplicationIdEntitlements(parse_id(id)?),
["applications", id, "emojis"] => ApplicationEmojis(parse_id(id)?),
["applications", id, "guilds", _, "commands"]
| ["applications", id, "guilds", _, "commands", "permissions"] => {
ApplicationGuildCommand(parse_id(id)?)
Expand Down
124 changes: 121 additions & 3 deletions twilight-http/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ mod interaction;

pub use self::{builder::ClientBuilder, interaction::InteractionClient};

use crate::request::application::monetization::{
CreateTestEntitlement, CreateTestEntitlementOwner, DeleteTestEntitlement, GetEntitlements,
GetSKUs,
use crate::request::application::{
emoji::{
AddApplicationEmoji, DeleteApplicationEmoji, ListApplicationEmojis, UpdateApplicationEmoji,
},
monetization::{
CreateTestEntitlement, CreateTestEntitlementOwner, DeleteTestEntitlement, GetEntitlements,
GetSKUs,
},
};
#[allow(deprecated)]
use crate::{
Expand Down Expand Up @@ -2728,6 +2733,119 @@ impl Client {
GetSKUs::new(self, application_id)
}

/// Gets all emojis associated with an application
///
/// # Examples
///
/// ```no_run
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("my token".to_owned());
///
/// let application_id = Id::new(1);
///
/// let emojis = client.get_application_emojis(application_id).await?;
///
/// # Ok(()) }
/// ```
pub const fn get_application_emojis(
&self,
application_id: Id<ApplicationMarker>,
) -> ListApplicationEmojis<'_> {
ListApplicationEmojis::new(self, application_id)
}

/// Adds an emoji to an application
///
/// # Examples
///
/// ```no_run
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("my token".to_owned());
///
/// let application_id = Id::new(1);
///
/// client
/// .add_application_emoji(application_id, "emoji name", "emoji image")
/// .await?;
///
/// # Ok(()) }
/// ```
pub const fn add_application_emoji<'a>(
&'a self,
application_id: Id<ApplicationMarker>,
name: &'a str,
image: &'a str,
) -> AddApplicationEmoji<'a> {
AddApplicationEmoji::new(self, application_id, name, image)
}

/// Updates an emoji associated with an application.
///
/// # Examples
///
/// ```no_run
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("my token".to_owned());
///
/// let application_id = Id::new(1);
/// let emoji_id = Id::new(2);
///
/// client
/// .update_application_emoji(application_id, emoji_id, "new emoji name")
/// .await?;
///
/// # Ok(()) }
/// ```
pub const fn update_application_emoji<'a>(
&'a self,
application_id: Id<ApplicationMarker>,
emoji_id: Id<EmojiMarker>,
name: &'a str,
) -> UpdateApplicationEmoji<'a> {
UpdateApplicationEmoji::new(self, application_id, emoji_id, name)
}

/// Deletes an emoji associated with an application.
///
/// # Examples
///
/// ```no_run
/// use twilight_http::Client;
/// use twilight_model::id::Id;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = Client::new("my token".to_owned());
///
/// let application_id = Id::new(1);
/// let emoji_id = Id::new(2);
///
/// client
/// .delete_application_emoji(application_id, emoji_id)
/// .await?;
///
/// # Ok(()) }
/// ```
pub const fn delete_application_emoji(
&self,
application_id: Id<ApplicationMarker>,
emoji_id: Id<EmojiMarker>,
) -> DeleteApplicationEmoji<'_> {
DeleteApplicationEmoji::new(self, application_id, emoji_id)
}

/// Execute a request, returning a future resolving to a [`Response`].
///
/// # Errors
Expand Down
68 changes: 68 additions & 0 deletions twilight-http/src/request/application/emoji/add_emoji.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::future::IntoFuture;

use crate::{
request::{Request, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
Client, Error,
};

use serde::Serialize;
use twilight_model::{
guild::Emoji,
id::{marker::ApplicationMarker, Id},
};

#[derive(Serialize)]
struct AddApplicationEmojiFields<'a> {
image: &'a str,
name: &'a str,
}

pub struct AddApplicationEmoji<'a> {
fields: AddApplicationEmojiFields<'a>,
application_id: Id<ApplicationMarker>,
http: &'a Client,
}

impl<'a> AddApplicationEmoji<'a> {
pub(crate) const fn new(
http: &'a Client,
application_id: Id<ApplicationMarker>,
name: &'a str,
image: &'a str,
) -> Self {
Self {
fields: AddApplicationEmojiFields { image, name },
application_id,
http,
}
}
}

impl IntoFuture for AddApplicationEmoji<'_> {
type Output = Result<Response<Emoji>, Error>;

type IntoFuture = ResponseFuture<Emoji>;

fn into_future(self) -> Self::IntoFuture {
let http = self.http;

match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}

impl TryIntoRequest for AddApplicationEmoji<'_> {
fn try_into_request(self) -> Result<Request, Error> {
let mut request = Request::builder(&Route::AddApplicationEmoji {
application_id: self.application_id.get(),
});

request = request.json(&self.fields);

request.build()
}
}
56 changes: 56 additions & 0 deletions twilight-http/src/request/application/emoji/delete_emoji.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::future::IntoFuture;
use twilight_model::id::{
marker::{ApplicationMarker, EmojiMarker},
Id,
};

use crate::{
request::{Request, TryIntoRequest},
response::{Response, ResponseFuture},
routing::Route,
Client, Error,
};

pub struct DeleteApplicationEmoji<'a> {
application_id: Id<ApplicationMarker>,
emoji_id: Id<EmojiMarker>,
http: &'a Client,
}

impl<'a> DeleteApplicationEmoji<'a> {
pub(crate) const fn new(
http: &'a Client,
application_id: Id<ApplicationMarker>,
emoji_id: Id<EmojiMarker>,
) -> Self {
Self {
application_id,
emoji_id,
http,
}
}
}

impl IntoFuture for DeleteApplicationEmoji<'_> {
type Output = Result<Response<()>, Error>;

type IntoFuture = ResponseFuture<()>;

fn into_future(self) -> Self::IntoFuture {
let http = self.http;

match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}

impl TryIntoRequest for DeleteApplicationEmoji<'_> {
fn try_into_request(self) -> Result<Request, Error> {
Ok(Request::from_route(&Route::DeleteApplicationEmoji {
application_id: self.application_id.get(),
emoji_id: self.emoji_id.get(),
}))
}
}
50 changes: 50 additions & 0 deletions twilight-http/src/request/application/emoji/list_emojis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::future::IntoFuture;

use crate::{
request::{Request, TryIntoRequest},
response::{marker::ListBody, ResponseFuture},
routing::Route,
Client, Error, Response,
};
use twilight_model::{
guild::Emoji,
id::{marker::ApplicationMarker, Id},
};

#[must_use = "requests must be configured and executed"]
pub struct ListApplicationEmojis<'a> {
http: &'a Client,
application_id: Id<ApplicationMarker>,
}

impl<'a> ListApplicationEmojis<'a> {
pub(crate) const fn new(http: &'a Client, application_id: Id<ApplicationMarker>) -> Self {
Self {
http,
application_id,
}
}
}

impl IntoFuture for ListApplicationEmojis<'_> {
type Output = Result<Response<ListBody<Emoji>>, Error>;

type IntoFuture = ResponseFuture<ListBody<Emoji>>;

fn into_future(self) -> Self::IntoFuture {
let http = self.http;

match self.try_into_request() {
Ok(request) => http.request(request),
Err(source) => ResponseFuture::error(source),
}
}
}

impl TryIntoRequest for ListApplicationEmojis<'_> {
fn try_into_request(self) -> Result<Request, Error> {
Ok(Request::from_route(&Route::GetApplicationEmojis {
application_id: self.application_id.get(),
}))
}
}
9 changes: 9 additions & 0 deletions twilight-http/src/request/application/emoji/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod add_emoji;
mod delete_emoji;
mod list_emojis;
mod update_emoji;

pub use self::{
add_emoji::AddApplicationEmoji, delete_emoji::DeleteApplicationEmoji,
list_emojis::ListApplicationEmojis, update_emoji::UpdateApplicationEmoji,
};
Loading