serenity/builder/
create_sticker.rs

1#[cfg(feature = "http")]
2use super::Builder;
3use super::CreateAttachment;
4#[cfg(feature = "http")]
5use crate::http::CacheHttp;
6#[cfg(feature = "http")]
7use crate::internal::prelude::*;
8#[cfg(feature = "http")]
9use crate::model::prelude::*;
10
11/// A builder to create a guild sticker
12///
13/// [Discord docs](https://discord.com/developers/docs/resources/sticker#create-guild-sticker)
14#[derive(Clone, Debug)]
15#[must_use]
16pub struct CreateSticker<'a> {
17    name: String,
18    description: String,
19    tags: String,
20    file: CreateAttachment,
21    audit_log_reason: Option<&'a str>,
22}
23
24impl<'a> CreateSticker<'a> {
25    /// Creates a new builder with the given data. All of this builder's fields are required.
26    pub fn new(name: impl Into<String>, file: CreateAttachment) -> Self {
27        Self {
28            name: name.into(),
29            tags: String::new(),
30            description: String::new(),
31            file,
32            audit_log_reason: None,
33        }
34    }
35
36    /// Set the name of the sticker, replacing the current value as set in [`Self::new`].
37    ///
38    /// **Note**: Must be between 2 and 30 characters long.
39    pub fn name(mut self, name: impl Into<String>) -> Self {
40        self.name = name.into();
41        self
42    }
43
44    /// Set the description of the sticker.
45    ///
46    /// **Note**: Must be empty or 2-100 characters.
47    pub fn description(mut self, description: impl Into<String>) -> Self {
48        self.description = description.into();
49        self
50    }
51
52    /// The Discord name of a unicode emoji representing the sticker's expression.
53    ///
54    /// **Note**: Max 200 characters long.
55    pub fn tags(mut self, tags: impl Into<String>) -> Self {
56        self.tags = tags.into();
57        self
58    }
59
60    /// Set the sticker file. Replaces the current value as set in [`Self::new`].
61    ///
62    /// **Note**: Must be a PNG, APNG, or Lottie JSON file, max 500 KB.
63    pub fn file(mut self, file: CreateAttachment) -> Self {
64        self.file = file;
65        self
66    }
67
68    /// Sets the request's audit log reason.
69    pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
70        self.audit_log_reason = Some(reason);
71        self
72    }
73}
74
75#[cfg(feature = "http")]
76#[async_trait::async_trait]
77impl Builder for CreateSticker<'_> {
78    type Context<'ctx> = GuildId;
79    type Built = Sticker;
80
81    /// Creates a new sticker in the guild with the data set, if any.
82    ///
83    /// **Note**: Requires the [Create Guild Expressions] permission.
84    ///
85    /// # Errors
86    ///
87    /// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] if the current user
88    /// lacks permission. Otherwise returns [`Error::Http`], as well as if invalid data is given.
89    ///
90    /// [Create Guild Expressions]: Permissions::CREATE_GUILD_EXPRESSIONS
91    async fn execute(
92        self,
93        cache_http: impl CacheHttp,
94        ctx: Self::Context<'_>,
95    ) -> Result<Self::Built> {
96        #[cfg(feature = "cache")]
97        crate::utils::user_has_guild_perms(
98            &cache_http,
99            ctx,
100            Permissions::CREATE_GUILD_EXPRESSIONS,
101        )?;
102
103        let map = [("name", self.name), ("tags", self.tags), ("description", self.description)];
104        cache_http.http().create_sticker(ctx, map, self.file, self.audit_log_reason).await
105    }
106}