Skip to content

Commit

Permalink
Add modal submit collector (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalharp committed Feb 22, 2022
1 parent 16c2089 commit e5073ae
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/client/bridge/gateway/shard_messenger.rs
Expand Up @@ -3,7 +3,7 @@ use futures::channel::mpsc::{TrySendError, UnboundedSender as Sender};

use super::{ChunkGuildFilter, ShardClientMessage, ShardRunnerMessage};
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
use crate::collector::ComponentInteractionFilter;
use crate::collector::{ComponentInteractionFilter, ModalInteractionFilter};
#[cfg(feature = "collector")]
use crate::collector::{EventFilter, MessageFilter, ReactionFilter};
use crate::gateway::InterMessage;
Expand Down Expand Up @@ -279,6 +279,13 @@ impl ShardMessenger {
#[allow(clippy::let_underscore_must_use)]
let _ = self.send_to_shard(ShardRunnerMessage::SetComponentInteractionFilter(collector));
}

/// Sets a new filter for a modal interaction collector.
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
pub fn set_modal_interaction_filter(&self, collector: ModalInteractionFilter) {
#[allow(clippy::let_underscore_must_use)]
let _ = self.send_to_shard(ShardRunnerMessage::SetModalInteractionFilter(collector));
}
}

impl AsRef<ShardMessenger> for ShardMessenger {
Expand Down
31 changes: 24 additions & 7 deletions src/client/bridge/gateway/shard_runner.rs
Expand Up @@ -19,7 +19,7 @@ use crate::client::bridge::voice::VoiceGatewayManager;
use crate::client::dispatch::{dispatch, DispatchEvent};
use crate::client::{EventHandler, RawEventHandler};
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
use crate::collector::ComponentInteractionFilter;
use crate::collector::{ComponentInteractionFilter, ModalInteractionFilter};
#[cfg(feature = "collector")]
use crate::collector::{EventFilter, LazyArc, LazyReactionAction, MessageFilter, ReactionFilter};
#[cfg(feature = "framework")]
Expand All @@ -29,7 +29,7 @@ use crate::internal::prelude::*;
use crate::internal::ws_impl::{ReceiverExt, SenderExt};
use crate::model::event::{Event, GatewayEvent};
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
use crate::model::interactions::{Interaction, InteractionType};
use crate::model::interactions::Interaction;
use crate::CacheAndHttp;

/// A runner for managing a [`Shard`] and its respective WebSocket client.
Expand All @@ -56,6 +56,8 @@ pub struct ShardRunner {
reaction_filters: Vec<ReactionFilter>,
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
component_interaction_filters: Vec<ComponentInteractionFilter>,
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
modal_interaction_filters: Vec<ModalInteractionFilter>,
}

impl ShardRunner {
Expand Down Expand Up @@ -84,6 +86,8 @@ impl ShardRunner {
reaction_filters: Vec::new(),
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
component_interaction_filters: vec![],
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
modal_interaction_filters: vec![],
}
}

Expand Down Expand Up @@ -233,15 +237,20 @@ impl ShardRunner {
},
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
Event::InteractionCreate(ref interaction_event) => {
if interaction_event.interaction.kind() == InteractionType::MessageComponent {
if let Interaction::MessageComponent(ref interaction) =
interaction_event.interaction
{
match &interaction_event.interaction {
Interaction::MessageComponent(interaction) => {
let mut interaction = LazyArc::new(interaction);
retain(&mut self.component_interaction_filters, |f| {
f.send_interaction(&mut interaction)
});
}
},
Interaction::ModalSubmit(interaction) => {
let mut interaction = LazyArc::new(interaction);
retain(&mut self.modal_interaction_filters, |f| {
f.send_interaction(&mut interaction)
});
},
_ => (),
}
},
_ => {},
Expand Down Expand Up @@ -466,6 +475,14 @@ impl ShardRunner {
)) => {
self.component_interaction_filters.push(collector);

true
},
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
ShardClientMessage::Runner(ShardRunnerMessage::SetModalInteractionFilter(
collector,
)) => {
self.modal_interaction_filters.push(collector);

true
},
},
Expand Down
5 changes: 4 additions & 1 deletion src/client/bridge/gateway/shard_runner_message.rs
@@ -1,7 +1,7 @@
use async_tungstenite::tungstenite::Message;

#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
use crate::collector::ComponentInteractionFilter;
use crate::collector::{ComponentInteractionFilter, ModalInteractionFilter};
#[cfg(feature = "collector")]
use crate::collector::{EventFilter, MessageFilter, ReactionFilter};
use crate::model::{
Expand Down Expand Up @@ -73,4 +73,7 @@ pub enum ShardRunnerMessage {
/// Sends a new filter for component interactions to the shard.
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
SetComponentInteractionFilter(ComponentInteractionFilter),
/// Sends a new filter for modal interactions to the shard.
#[cfg(all(feature = "unstable_discord_api", feature = "collector"))]
SetModalInteractionFilter(ModalInteractionFilter),
}
4 changes: 4 additions & 0 deletions src/collector/mod.rs
Expand Up @@ -11,12 +11,16 @@ pub use error::Error as CollectorError;
pub mod component_interaction_collector;
pub mod event_collector;
pub mod message_collector;
#[cfg(feature = "unstable_discord_api")]
pub mod modal_interaction_collector;
pub mod reaction_collector;

#[cfg(feature = "unstable_discord_api")]
pub use component_interaction_collector::*;
pub use event_collector::*;
pub use message_collector::*;
#[cfg(feature = "unstable_discord_api")]
pub use modal_interaction_collector::*;
pub use reaction_collector::*;

/// Wraps a &T and clones the value into an Arc<T> lazily. Used with collectors to allow inspecting
Expand Down

0 comments on commit e5073ae

Please sign in to comment.