Skip to content

Commit 54e5449

Browse files
authored
Support ?with_components for webhooks (#3307)
1 parent 12c7c9b commit 54e5449

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/builder/execute_webhook.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub struct ExecuteWebhook {
7979

8080
#[serde(skip)]
8181
thread_id: Option<ChannelId>,
82+
#[serde(skip)]
83+
with_components: Option<bool>,
8284
}
8385

8486
impl ExecuteWebhook {
@@ -222,6 +224,9 @@ impl ExecuteWebhook {
222224
/// the webhook's `kind` field is set to [`WebhookType::Application`], or it was created by an
223225
/// application (and has kind [`WebhookType::Incoming`]).
224226
///
227+
/// If [`Self::with_components`] is set, non-interactive components can be used on non
228+
/// application-owned webhooks.
229+
///
225230
/// [`WebhookType::Application`]: crate::model::webhook::WebhookType
226231
/// [`WebhookType::Incoming`]: crate::model::webhook::WebhookType
227232
pub fn components(mut self, components: Vec<CreateActionRow>) -> Self {
@@ -337,6 +342,12 @@ impl ExecuteWebhook {
337342
self.thread_name = Some(thread_name);
338343
self
339344
}
345+
346+
/// Allows sending non interactive components on non application owned webhooks.
347+
pub fn with_components(mut self, with_components: bool) -> Self {
348+
self.with_components = Some(with_components);
349+
self
350+
}
340351
}
341352

342353
#[cfg(feature = "http")]
@@ -367,6 +378,11 @@ impl Builder for ExecuteWebhook {
367378
self.allowed_mentions.clone_from(&http.default_allowed_mentions);
368379
}
369380

370-
http.execute_webhook(ctx.0, self.thread_id, ctx.1, ctx.2, files, &self).await
381+
if self.with_components.unwrap_or_default() {
382+
http.execute_webhook_with_components(ctx.0, self.thread_id, ctx.1, ctx.2, files, &self)
383+
.await
384+
} else {
385+
http.execute_webhook(ctx.0, self.thread_id, ctx.1, ctx.2, files, &self).await
386+
}
371387
}
372388
}

src/http/client.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,12 +2572,48 @@ impl Http {
25722572
wait: bool,
25732573
files: Vec<CreateAttachment>,
25742574
map: &impl serde::Serialize,
2575+
) -> Result<Option<Message>> {
2576+
self.execute_webhook_(webhook_id, thread_id, token, wait, files, map, false).await
2577+
}
2578+
2579+
/// Same as [`Self::execute_webhook`] but allows sending non interactive components on non
2580+
/// app-owned webhooks.
2581+
///
2582+
/// Refer to the [Discord docs] for more information on how this works.
2583+
///
2584+
/// [Discord docs]: https://discord.com/developers/docs/resources/webhook#execute-webhook-query-string-params
2585+
pub async fn execute_webhook_with_components(
2586+
&self,
2587+
webhook_id: WebhookId,
2588+
thread_id: Option<ChannelId>,
2589+
token: &str,
2590+
wait: bool,
2591+
files: Vec<CreateAttachment>,
2592+
map: &impl serde::Serialize,
2593+
) -> Result<Option<Message>> {
2594+
self.execute_webhook_(webhook_id, thread_id, token, wait, files, map, true).await
2595+
}
2596+
2597+
#[expect(clippy::too_many_arguments)]
2598+
async fn execute_webhook_(
2599+
&self,
2600+
webhook_id: WebhookId,
2601+
thread_id: Option<ChannelId>,
2602+
token: &str,
2603+
wait: bool,
2604+
files: Vec<CreateAttachment>,
2605+
map: &impl serde::Serialize,
2606+
with_components: bool,
25752607
) -> Result<Option<Message>> {
25762608
let mut params = vec![("wait", wait.to_string())];
25772609
if let Some(thread_id) = thread_id {
25782610
params.push(("thread_id", thread_id.to_string()));
25792611
}
25802612

2613+
if with_components {
2614+
params.push(("with_components", with_components.to_string()));
2615+
}
2616+
25812617
let mut request = Request {
25822618
body: None,
25832619
multipart: None,

0 commit comments

Comments
 (0)