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

Support attachments in forum posts #2696

Merged
merged 1 commit into from
Jan 21, 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
25 changes: 25 additions & 0 deletions examples/testing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,31 @@ async fn message(ctx: &Context, msg: Message) -> Result<(), serenity::Error> {
&*guild.member(ctx, msg.author.id).await?,
);
channel_id.say(ctx, format!("{:?}", perms)).await?;
} else if let Some(forum_channel_id) = msg.content.strip_prefix("createforumpostin ") {
forum_channel_id
.parse::<ChannelId>()
.unwrap()
.create_forum_post(
ctx,
CreateForumPost::new(
"a",
CreateMessage::new()
.add_file(CreateAttachment::bytes(b"Hallo welt!", "lul.txt")),
),
// CreateForumPost::new(
// "a",
// CreateMessage::new()
// .content("test, i hope that forum posts without attachments still
// work?") .embed(CreateEmbed::new().title("hmmm").
// description("do they?")), ),
)
.await?;
} else if let Some(forum_post_url) = msg.content.strip_prefix("deleteforumpost ") {
let (_guild_id, channel_id, _message_id) =
serenity::utils::parse_message_url(forum_post_url).unwrap();
msg.channel_id.say(ctx, format!("Deleting <#{}> in 10 seconds...", channel_id)).await?;
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
channel_id.delete(ctx).await?;
} else {
return Ok(());
}
Expand Down
8 changes: 6 additions & 2 deletions src/builder/create_forum_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ impl<'a> Builder for CreateForumPost<'a> {
///
/// Returns [`Error::Http`] if the current user lacks permission, or if invalid data is given.
async fn execute(
self,
mut self,
cache_http: impl CacheHttp,
ctx: Self::Context<'_>,
) -> Result<Self::Built> {
cache_http.http().create_forum_post(ctx, &self, self.audit_log_reason).await
let files = self.message.attachments.take_files();
cache_http
.http()
.create_forum_post_with_attachments(ctx, &self, files, self.audit_log_reason)
.await
}
}
2 changes: 1 addition & 1 deletion src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct CreateMessage {
sticker_ids: Vec<StickerId>,
#[serde(skip_serializing_if = "Option::is_none")]
flags: Option<MessageFlags>,
attachments: EditAttachments,
pub(crate) attachments: EditAttachments,

// The following fields are handled separately.
#[serde(skip)]
Expand Down
21 changes: 17 additions & 4 deletions src/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,31 @@ impl Http {
.await
}

/// Creates a forum post channel in the [`GuildChannel`] given its Id.
/// Shortcut for [`Self::create_forum_post_with_attachments`]
pub async fn create_forum_post(
&self,
channel_id: ChannelId,
map: &impl serde::Serialize,
audit_log_reason: Option<&str>,
) -> Result<GuildChannel> {
let body = to_vec(map)?;
self.create_forum_post_with_attachments(channel_id, map, vec![], audit_log_reason).await
}

/// Creates a forum post channel in the [`GuildChannel`] given its Id.
pub async fn create_forum_post_with_attachments(
&self,
channel_id: ChannelId,
map: &impl serde::Serialize,
files: Vec<CreateAttachment>,
audit_log_reason: Option<&str>,
) -> Result<GuildChannel> {
self.fire(Request {
body: Some(body),
multipart: None,
body: None,
multipart: Some(Multipart {
upload: MultipartUpload::Attachments(files.into_iter().collect()),
payload_json: Some(to_string(map)?),
fields: vec![],
}),
headers: audit_log_reason.map(reason_into_header),
method: LightMethod::Post,
route: Route::ChannelForumPosts {
Expand Down