Skip to content

Commit

Permalink
Move QuickModal into collector module
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Nov 22, 2024
1 parent 9e3d21b commit 216fb03
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/e05_sample_bot_structure/src/commands/modal.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
3 changes: 3 additions & 0 deletions src/collector.rs → src/collector/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
34 changes: 30 additions & 4 deletions src/utils/quick_modal.rs → src/collector/quick_modal.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::borrow::Cow;
use std::future::Future;

use crate::builder::{CreateActionRow, CreateInputText, CreateInteractionResponse, CreateModal};
use crate::collector::ModalInteractionCollector;
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<FixedString<u16>>,
Expand All @@ -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))
Expand All @@ -28,15 +28,13 @@ pub struct QuickModalResponse {
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "collector")]
#[must_use]
pub struct CreateQuickModal<'a> {
title: Cow<'a, str>,
timeout: Option<std::time::Duration>,
input_texts: Vec<CreateInputText<'a>>,
}

#[cfg(feature = "collector")]
impl<'a> CreateQuickModal<'a> {
pub fn new(title: impl Into<Cow<'a, str>>) -> Self {
Self {
Expand Down Expand Up @@ -143,3 +141,31 @@ impl<'a> CreateQuickModal<'a> {
}))
}
}

pub trait QuickModal {
fn quick_modal(
&self,
ctx: &Context,
builder: CreateQuickModal<'_>,
) -> impl Future<Output = Result<Option<QuickModalResponse>>>;
}

impl QuickModal for CommandInteraction {
async fn quick_modal(
&self,
ctx: &Context,
builder: CreateQuickModal<'_>,
) -> Result<Option<QuickModalResponse>> {
builder.execute(ctx, self.id, &self.token).await
}
}

impl QuickModal for ComponentInteraction {
async fn quick_modal(
&self,
ctx: &Context,
builder: CreateQuickModal<'_>,
) -> Result<Option<QuickModalResponse>> {
builder.execute(ctx, self.id, &self.token).await
}
}
18 changes: 0 additions & 18 deletions src/model/application/command_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Option<QuickModalResponse>> {
builder.execute(ctx, self.id, &self.token).await
}
}

// Manual impl needed to insert guild_id into resolved Role's
Expand Down
18 changes: 0 additions & 18 deletions src/model/application/component_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Option<QuickModalResponse>> {
builder.execute(ctx, self.id, &self.token).await
}
}

// Manual impl needed to insert guild_id into model data
Expand Down
4 changes: 0 additions & 4 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 216fb03

Please sign in to comment.