@@ -6,7 +6,8 @@ use futures::future::pending;
66use futures:: { Stream , StreamExt as _} ;
77pub use quick_modal:: * ;
88
9- use crate :: gateway:: { CollectorCallback , ShardMessenger } ;
9+ use crate :: gateway:: CollectorCallback ;
10+ use crate :: gateway:: client:: Context ;
1011use crate :: internal:: prelude:: * ;
1112use crate :: model:: prelude:: * ;
1213
@@ -18,10 +19,10 @@ use crate::model::prelude::*;
1819/// # use std::time::Duration;
1920/// # use futures::StreamExt as _;
2021/// # use serenity::model::prelude::Event;
21- /// # use serenity::gateway::ShardMessenger ;
22+ /// # use serenity::gateway::client::Context ;
2223/// # use serenity::collector::collect;
23- /// # async fn example_(shard : &ShardMessenger ) {
24- /// let stream = collect(shard , |event| match event {
24+ /// # async fn example_(ctx : &Context ) {
25+ /// let stream = collect(ctx , |event| match event {
2526/// Event::ReactionRemove(event) => Some(event.reaction.clone()),
2627/// _ => None,
2728/// });
@@ -33,18 +34,20 @@ use crate::model::prelude::*;
3334/// .await;
3435/// # }
3536/// ```
36- pub fn collect < T , F > ( shard : & ShardMessenger , extractor : F ) -> impl Stream < Item = T > + use < T , F >
37+ pub fn collect < T , F > ( ctx : & Context , extractor : F ) -> impl Stream < Item = T > + use < T , F >
3738where
3839 T : Send + ' static ,
3940 F : Fn ( & Event ) -> Option < T > + Send + Sync + ' static ,
4041{
4142 let ( sender, mut receiver) = tokio:: sync:: mpsc:: unbounded_channel ( ) ;
4243
4344 // Register an event callback in the shard. It's kept alive as long as we return `true`
44- shard. add_collector ( CollectorCallback ( Arc :: new ( move |event| match extractor ( event) {
45- // If this event matches, we send it to the receiver stream
46- Some ( item) => sender. send ( item) . is_ok ( ) ,
47- None => !sender. is_closed ( ) ,
45+ ctx. collectors . write ( ) . push ( CollectorCallback ( Arc :: new ( move |event| {
46+ match extractor ( event) {
47+ // If this event matches, we send it to the receiver stream
48+ Some ( item) => sender. send ( item) . is_ok ( ) ,
49+ None => !sender. is_closed ( ) ,
50+ }
4851 } ) ) ) ;
4952
5053 // Convert the mpsc Receiver into a Stream
@@ -62,18 +65,18 @@ macro_rules! make_specific_collector {
6265 #[ doc = concat!( "A [`" , stringify!( $collector_type) , "`] receives [`" , stringify!( $item_type) , "`]'s match the given filters for a set duration." ) ]
6366 $( #[ $( $meta) * ] ) *
6467 #[ must_use]
65- pub struct $collector_type {
66- shard : ShardMessenger ,
68+ pub struct $collector_type< ' a> {
69+ ctx : & ' a Context ,
6770 duration: Option <std:: time:: Duration >,
6871 filter: Option <Box <dyn Fn ( & $item_type) -> bool + Send + Sync >>,
6972 $( $filter_name: Option <$filter_type>, ) *
7073 }
7174
72- impl $collector_type {
75+ impl < ' a> $collector_type< ' a> {
7376 /// Creates a new collector without any filters configured.
74- pub fn new( shard : ShardMessenger ) -> Self {
77+ pub fn new( ctx : & ' a Context ) -> Self {
7578 Self {
76- shard ,
79+ ctx ,
7780 duration: None ,
7881 filter: None ,
7982 $( $filter_name: None , ) *
@@ -124,7 +127,7 @@ macro_rules! make_specific_collector {
124127 None => pending:: <( ) >( ) . await ,
125128 } } ;
126129
127- let stream = collect( & self . shard , move |event| match event {
130+ let stream = collect( & self . ctx , move |event| match event {
128131 $extractor if filters_pass( $extracted_item) => Some ( $extracted_item. clone( ) ) ,
129132 _ => None ,
130133 } ) ;
@@ -139,23 +142,23 @@ macro_rules! make_specific_collector {
139142 }
140143 }
141144
142- impl IntoFuture for $collector_type {
145+ impl < ' a> IntoFuture for $collector_type< ' a> {
143146 type Output = Option <$item_type>;
144- type IntoFuture = futures:: future:: BoxFuture <' static , Self :: Output >;
147+ type IntoFuture = futures:: future:: BoxFuture <' a , Self :: Output >;
145148
146149 fn into_future( self ) -> Self :: IntoFuture {
147150 Box :: pin( self . next( ) )
148151 }
149152 }
150153
151154 pub trait $collector_trait {
152- fn $method_name( self , shard_messenger : ShardMessenger ) -> $collector_type;
155+ fn $method_name( self , ctx : & Context ) -> $collector_type< ' _> ;
153156 }
154157
155158 $(
156159 impl $collector_trait for $filter_type {
157- fn $method_name( self , shard_messenger : ShardMessenger ) -> $collector_type {
158- $collector_type:: new( shard_messenger ) . $filter_name( self )
160+ fn $method_name( self , ctx : & Context ) -> $collector_type< ' _> {
161+ $collector_type:: new( ctx ) . $filter_name( self )
159162 }
160163 }
161164 ) *
0 commit comments