Skip to content

Commit

Permalink
Allow setting both normal and raw event handlers (#2933)
Browse files Browse the repository at this point in the history
  • Loading branch information
tazz4843 authored Aug 16, 2024
1 parent 0330ebd commit 9d41131
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ pub(crate) async fn dispatch_model(
#[cfg(feature = "framework")] framework: Option<Arc<dyn Framework>>,
event_handler: Option<InternalEventHandler>,
) {
let handler = match event_handler {
Some(InternalEventHandler::Normal(handler)) => Some(handler),
Some(InternalEventHandler::Raw(raw_handler)) => {
return raw_handler.raw_event(context, event).await;
},
None => None,
let (handler, raw_handler) = match event_handler {
Some(InternalEventHandler::Normal(handler)) => (Some(handler), None),
Some(InternalEventHandler::Both {
raw,
normal,
}) => (Some(normal), Some(raw)),
Some(InternalEventHandler::Raw(raw_handler)) => (None, Some(raw_handler)),
None => (None, None),
};
if let Some(raw_handler) = raw_handler {
raw_handler.raw_event(context.clone(), &event).await;
}

let (full_event, extra_event) = update_cache_with_event(
#[cfg(feature = "cache")]
Expand Down
3 changes: 2 additions & 1 deletion src/client/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ event_handler! {
#[async_trait]
pub trait RawEventHandler: Send + Sync {
/// Dispatched when any event occurs
async fn raw_event(&self, _ctx: Context, _ev: Event) {}
async fn raw_event(&self, _ctx: Context, _ev: &Event) {}

/// Checks if the `event` should be dispatched (`true`) or ignored (`false`).
///
Expand All @@ -540,4 +540,5 @@ pub trait RawEventHandler: Send + Sync {
pub enum InternalEventHandler {
Raw(Arc<dyn RawEventHandler>),
Normal(Arc<dyn EventHandler>),
Both { raw: Arc<dyn RawEventHandler>, normal: Arc<dyn EventHandler> },
}
5 changes: 4 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ impl IntoFuture for ClientBuilder {
let http = self.http;

let event_handler = match (self.event_handler, self.raw_event_handler) {
(Some(_), Some(_)) => panic!("Cannot provide both a normal and raw event handlers"),
(Some(normal), Some(raw)) => Some(InternalEventHandler::Both {
normal,
raw,
}),
(Some(h), None) => Some(InternalEventHandler::Normal(h)),
(None, Some(h)) => Some(InternalEventHandler::Raw(h)),
(None, None) => None,
Expand Down
6 changes: 6 additions & 0 deletions src/gateway/bridge/shard_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ impl ShardRunner {
Some(InternalEventHandler::Raw(handler)) => {
handler.filter_event(&context, &event)
},
Some(InternalEventHandler::Both {
raw,
normal,
}) => {
raw.filter_event(&context, &event) && normal.filter_event(&context, &event)
},
None => true,
};

Expand Down

0 comments on commit 9d41131

Please sign in to comment.