serenity/builder/
create_forum_post.rs

1#[cfg(feature = "http")]
2use super::Builder;
3use super::CreateMessage;
4#[cfg(feature = "http")]
5use crate::http::CacheHttp;
6#[cfg(feature = "http")]
7use crate::internal::prelude::*;
8use crate::model::prelude::*;
9
10/// [Discord docs](https://discord.com/developers/docs/resources/channel#start-thread-in-forum-channel).
11#[derive(Clone, Debug, Serialize)]
12#[must_use]
13pub struct CreateForumPost<'a> {
14    name: String,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    auto_archive_duration: Option<AutoArchiveDuration>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    rate_limit_per_user: Option<u16>,
19    message: CreateMessage,
20    #[serde(skip_serializing_if = "Vec::is_empty")]
21    applied_tags: Vec<ForumTagId>,
22
23    #[serde(skip)]
24    audit_log_reason: Option<&'a str>,
25}
26
27impl<'a> CreateForumPost<'a> {
28    /// Creates a builder with the given name and message content, leaving all other fields empty.
29    pub fn new(name: impl Into<String>, message: CreateMessage) -> Self {
30        Self {
31            name: name.into(),
32            message,
33            auto_archive_duration: None,
34            rate_limit_per_user: None,
35            applied_tags: Vec::new(),
36            audit_log_reason: None,
37        }
38    }
39
40    /// The name of the forum post. Replaces the current value as set in [`Self::new`].
41    ///
42    /// **Note**: Must be between 2 and 100 characters long.
43    pub fn name(mut self, name: impl Into<String>) -> Self {
44        self.name = name.into();
45        self
46    }
47
48    /// The contents of the first message in the forum post.
49    ///
50    /// See [`CreateMessage`] for restrictions around message size.
51    pub fn message(mut self, message: CreateMessage) -> Self {
52        self.message = message;
53        self
54    }
55
56    /// Duration in minutes to automatically archive the forum post after recent activity.
57    pub fn auto_archive_duration(mut self, duration: AutoArchiveDuration) -> Self {
58        self.auto_archive_duration = Some(duration);
59        self
60    }
61
62    /// How many seconds must a user wait before sending another message.
63    ///
64    /// Bots, or users with the [`MANAGE_MESSAGES`] and/or [`MANAGE_CHANNELS`] permissions are
65    /// exempt from this restriction.
66    ///
67    /// **Note**: Must be between 0 and 21600 seconds (360 minutes or 6 hours).
68    ///
69    /// [`MANAGE_MESSAGES`]: crate::model::permissions::Permissions::MANAGE_MESSAGES
70    /// [`MANAGE_CHANNELS`]: crate::model::permissions::Permissions::MANAGE_CHANNELS
71    #[doc(alias = "slowmode")]
72    pub fn rate_limit_per_user(mut self, seconds: u16) -> Self {
73        self.rate_limit_per_user = Some(seconds);
74        self
75    }
76
77    pub fn add_applied_tag(mut self, applied_tag: ForumTagId) -> Self {
78        self.applied_tags.push(applied_tag);
79        self
80    }
81
82    pub fn set_applied_tags(
83        mut self,
84        applied_tags: impl IntoIterator<Item = impl Into<ForumTagId>>,
85    ) -> Self {
86        self.applied_tags = applied_tags.into_iter().map(Into::into).collect();
87        self
88    }
89
90    /// Sets the request's audit log reason.
91    pub fn audit_log_reason(mut self, reason: &'a str) -> Self {
92        self.audit_log_reason = Some(reason);
93        self
94    }
95}
96
97#[cfg(feature = "http")]
98#[async_trait::async_trait]
99impl Builder for CreateForumPost<'_> {
100    type Context<'ctx> = ChannelId;
101    type Built = GuildChannel;
102
103    /// Creates a forum post in the given channel.
104    ///
105    /// # Errors
106    ///
107    /// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
108    async fn execute(
109        mut self,
110        cache_http: impl CacheHttp,
111        ctx: Self::Context<'_>,
112    ) -> Result<Self::Built> {
113        let files = self.message.attachments.take_files();
114        cache_http
115            .http()
116            .create_forum_post_with_attachments(ctx, &self, files, self.audit_log_reason)
117            .await
118    }
119}