From 216fb038f336eae118c907b189f87e2345f07ec5 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Fri, 22 Nov 2024 15:34:51 -0500 Subject: [PATCH] Move QuickModal into collector module --- .../src/commands/modal.rs | 2 +- src/{collector.rs => collector/mod.rs} | 3 ++ src/{utils => collector}/quick_modal.rs | 34 ++++++++++++++++--- src/model/application/command_interaction.rs | 18 ---------- .../application/component_interaction.rs | 18 ---------- src/utils/mod.rs | 4 --- 6 files changed, 34 insertions(+), 45 deletions(-) rename src/{collector.rs => collector/mod.rs} (99%) rename src/{utils => collector}/quick_modal.rs (86%) diff --git a/examples/e05_sample_bot_structure/src/commands/modal.rs b/examples/e05_sample_bot_structure/src/commands/modal.rs index 4e9746827e1..d46a3d2fcc6 100644 --- a/examples/e05_sample_bot_structure/src/commands/modal.rs +++ b/examples/e05_sample_bot_structure/src/commands/modal.rs @@ -1,7 +1,7 @@ use serenity::builder::*; +use serenity::collector::{CreateQuickModal, QuickModal}; use serenity::model::prelude::*; use serenity::prelude::*; -use serenity::utils::CreateQuickModal; pub async fn run(ctx: &Context, interaction: &CommandInteraction) -> Result<(), serenity::Error> { let modal = CreateQuickModal::new("About you") diff --git a/src/collector.rs b/src/collector/mod.rs similarity index 99% rename from src/collector.rs rename to src/collector/mod.rs index d34652f76c7..b004106dbd5 100644 --- a/src/collector.rs +++ b/src/collector/mod.rs @@ -1,7 +1,10 @@ +mod quick_modal; + use std::sync::Arc; use futures::future::pending; use futures::{Stream, StreamExt as _}; +pub use quick_modal::*; use crate::gateway::{CollectorCallback, ShardMessenger}; use crate::internal::prelude::*; diff --git a/src/utils/quick_modal.rs b/src/collector/quick_modal.rs similarity index 86% rename from src/utils/quick_modal.rs rename to src/collector/quick_modal.rs index 2fecfe02447..2362881a914 100644 --- a/src/utils/quick_modal.rs +++ b/src/collector/quick_modal.rs @@ -1,4 +1,5 @@ use std::borrow::Cow; +use std::future::Future; use crate::builder::{CreateActionRow, CreateInputText, CreateInteractionResponse, CreateModal}; use crate::collector::ModalInteractionCollector; @@ -6,7 +7,6 @@ use crate::gateway::client::Context; use crate::internal::prelude::*; use crate::model::prelude::*; -#[cfg(feature = "collector")] pub struct QuickModalResponse { pub interaction: ModalInteraction, pub inputs: FixedArray>, @@ -15,7 +15,7 @@ pub struct QuickModalResponse { /// Convenience builder to create a modal, wait for the user to submit and parse the response. /// /// ```rust -/// # use serenity::{builder::*, model::prelude::*, prelude::*, utils::CreateQuickModal, Result}; +/// # use serenity::{builder::*, model::prelude::*, prelude::*, collector::*, Result}; /// # async fn foo_(ctx: &Context, interaction: &CommandInteraction) -> Result<()> { /// let modal = CreateQuickModal::new("About you") /// .timeout(std::time::Duration::from_secs(600)) @@ -28,7 +28,6 @@ pub struct QuickModalResponse { /// # Ok(()) /// # } /// ``` -#[cfg(feature = "collector")] #[must_use] pub struct CreateQuickModal<'a> { title: Cow<'a, str>, @@ -36,7 +35,6 @@ pub struct CreateQuickModal<'a> { input_texts: Vec>, } -#[cfg(feature = "collector")] impl<'a> CreateQuickModal<'a> { pub fn new(title: impl Into>) -> Self { Self { @@ -143,3 +141,31 @@ impl<'a> CreateQuickModal<'a> { })) } } + +pub trait QuickModal { + fn quick_modal( + &self, + ctx: &Context, + builder: CreateQuickModal<'_>, + ) -> impl Future>>; +} + +impl QuickModal for CommandInteraction { + async fn quick_modal( + &self, + ctx: &Context, + builder: CreateQuickModal<'_>, + ) -> Result> { + builder.execute(ctx, self.id, &self.token).await + } +} + +impl QuickModal for ComponentInteraction { + async fn quick_modal( + &self, + ctx: &Context, + builder: CreateQuickModal<'_>, + ) -> Result> { + builder.execute(ctx, self.id, &self.token).await + } +} diff --git a/src/model/application/command_interaction.rs b/src/model/application/command_interaction.rs index 119ea2d0d6a..361ab3dd841 100644 --- a/src/model/application/command_interaction.rs +++ b/src/model/application/command_interaction.rs @@ -12,14 +12,10 @@ use crate::builder::{ CreateInteractionResponseMessage, EditInteractionResponse, }; -#[cfg(feature = "collector")] -use crate::gateway::client::Context; #[cfg(feature = "model")] use crate::http::Http; use crate::internal::utils::lending_for_each; use crate::model::prelude::*; -#[cfg(all(feature = "collector", feature = "utils"))] -use crate::utils::{CreateQuickModal, QuickModalResponse}; /// An interaction when a user invokes a slash command. /// @@ -204,20 +200,6 @@ impl CommandInteraction { ); self.create_response(http, builder).await } - - /// See [`CreateQuickModal`]. - /// - /// # Errors - /// - /// See [`CreateQuickModal::execute()`]. - #[cfg(all(feature = "collector", feature = "utils"))] - pub async fn quick_modal( - &self, - ctx: &Context, - builder: CreateQuickModal<'_>, - ) -> Result> { - builder.execute(ctx, self.id, &self.token).await - } } // Manual impl needed to insert guild_id into resolved Role's diff --git a/src/model/application/component_interaction.rs b/src/model/application/component_interaction.rs index cbdac72d92b..8551362782c 100644 --- a/src/model/application/component_interaction.rs +++ b/src/model/application/component_interaction.rs @@ -9,13 +9,9 @@ use crate::builder::{ CreateInteractionResponseMessage, EditInteractionResponse, }; -#[cfg(feature = "collector")] -use crate::gateway::client::Context; #[cfg(feature = "model")] use crate::http::Http; use crate::model::prelude::*; -#[cfg(all(feature = "collector", feature = "utils"))] -use crate::utils::{CreateQuickModal, QuickModalResponse}; /// An interaction triggered by a message component. /// @@ -201,20 +197,6 @@ impl ComponentInteraction { ); self.create_response(http, builder).await } - - /// See [`CreateQuickModal`]. - /// - /// # Errors - /// - /// See [`CreateQuickModal::execute()`]. - #[cfg(all(feature = "collector", feature = "utils"))] - pub async fn quick_modal( - &self, - ctx: &Context, - builder: CreateQuickModal<'_>, - ) -> Result> { - builder.execute(ctx, self.id, &self.token).await - } } // Manual impl needed to insert guild_id into model data diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 7d4e2e15460..d1b9acda80f 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -8,8 +8,6 @@ mod content_safe; mod custom_message; mod formatted_timestamp; mod message_builder; -#[cfg(feature = "collector")] -mod quick_modal; use std::num::NonZeroU16; @@ -18,8 +16,6 @@ pub use argument_convert::*; #[cfg(feature = "cache")] pub use content_safe::*; pub use formatted_timestamp::*; -#[cfg(feature = "collector")] -pub use quick_modal::*; use url::Url; pub use self::custom_message::CustomMessage;