Skip to content

Commit

Permalink
Non-command message handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kangalio committed Jan 12, 2024
1 parent 8c3a649 commit 1f0abfb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/feature_showcase/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ async fn main() {
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
non_command_message: Some(|_, _, msg| Box::pin(async move {
println!("non command message!: {}", msg.content);
Ok(())
})),
..Default::default()
},
on_error: |error| {
Expand Down
3 changes: 3 additions & 0 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ pub async fn on_error<U, E: std::fmt::Display + std::fmt::Debug>(
crate::FrameworkError::UnknownInteraction { interaction, .. } => {
tracing::warn!("received unknown interaction \"{}\"", interaction.data.name);
}
crate::FrameworkError::NonCommandMessage { error, .. } => {
tracing::warn!("error in non-command message handler: {}", error);
}
crate::FrameworkError::__NonExhaustive(unreachable) => match unreachable {},
}

Expand Down
15 changes: 15 additions & 0 deletions src/dispatch/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,27 @@ pub async fn dispatch_message<'a, U: Send + Sync, E>(
payload,
ctx: ctx.into(),
})??;
} else {
if let Some(non_command_message) = framework.options.prefix_options.non_command_message {
non_command_message(&framework, ctx, msg).await.map_err(|e| {
crate::FrameworkError::NonCommandMessage {
error: e,
ctx,
framework,
msg,
}
})?;
}
}
Ok(())
}

/// Given a Message and some context data, parses prefix, command etc. out of the message and
/// returns the resulting [`crate::PrefixContext`]. To run the command, see [`run_invocation`].
///
/// Returns `Ok(None)` if the message does not look like a command invocation.
/// Returns `Err(...)` if the message _does_ look like a command invocation, but cannot be
/// fully parsed.
pub async fn parse_invocation<'a, U: Send + Sync, E>(
framework: crate::FrameworkContext<'a, U, E>,
ctx: &'a serenity::Context,
Expand Down
20 changes: 20 additions & 0 deletions src/structs/framework_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ pub enum FrameworkError<'a, U, E> {
/// The interaction in question
interaction: &'a serenity::CommandInteraction,
},
/// An error occurred in [`crate::PrefixFrameworkOptions::non_command_message`]
#[non_exhaustive]
NonCommandMessage {
/// The error thrown by user code
error: E,
#[derivative(Debug = "ignore")]
/// Serenity's Context
ctx: &'a serenity::Context,
/// Framework context
#[derivative(Debug = "ignore")]
framework: crate::FrameworkContext<'a, U, E>,
/// The interaction in question
msg: &'a serenity::Message,
},
// #[non_exhaustive] forbids struct update syntax for ?? reason
#[doc(hidden)]
__NonExhaustive(std::convert::Infallible),
Expand Down Expand Up @@ -223,6 +237,7 @@ impl<'a, U, E> FrameworkError<'a, U, E> {
Self::DynamicPrefix { ctx, .. } => ctx.serenity_context,
Self::UnknownCommand { ctx, .. } => ctx,
Self::UnknownInteraction { ctx, .. } => ctx,
Self::NonCommandMessage { ctx, .. } => ctx,
Self::__NonExhaustive(unreachable) => match unreachable {},
}
}
Expand All @@ -247,6 +262,7 @@ impl<'a, U, E> FrameworkError<'a, U, E> {
| Self::EventHandler { .. }
| Self::UnknownCommand { .. }
| Self::UnknownInteraction { .. }
| Self::NonCommandMessage { .. }
| Self::DynamicPrefix { .. } => return None,
Self::__NonExhaustive(unreachable) => match unreachable {},
})
Expand Down Expand Up @@ -404,6 +420,9 @@ impl<U, E: std::fmt::Display> std::fmt::Display for FrameworkError<'_, U, E> {
Self::UnknownInteraction { interaction, .. } => {
write!(f, "unknown interaction `{}`", interaction.data.name)
}
Self::NonCommandMessage { msg, .. } => {
write!(f, "error in non-command message handler in <@{}> (message ID {})", msg.channel_id, msg.id)
}
Self::__NonExhaustive(unreachable) => match *unreachable {},
}
}
Expand Down Expand Up @@ -432,6 +451,7 @@ impl<'a, U: std::fmt::Debug, E: std::error::Error + 'static> std::error::Error
Self::DynamicPrefix { error, .. } => Some(error),
Self::UnknownCommand { .. } => None,
Self::UnknownInteraction { .. } => None,
Self::NonCommandMessage { error, .. } => Some(error),
Self::__NonExhaustive(unreachable) => match *unreachable {},
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/structs/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ pub struct PrefixFrameworkOptions<U, E> {
pub ignore_thread_creation: bool,
/// Whether command names should be compared case-insensitively.
pub case_insensitive_commands: bool,
/// Callback for all non-command messages. Useful if you want to run code on any message that
/// is not a command
pub non_command_message: Option<
for<'a> fn(
&'a crate::FrameworkContext<'a, U, E>,
&'a serenity::Context,
&'a serenity::Message,
) -> crate::BoxFuture<'a, Result<(), E>>,
>,
/* // TODO: implement
/// Whether to invoke help command when someone sends a message with just a bot mention
pub help_when_mentioned: bool,
Expand Down Expand Up @@ -181,6 +190,7 @@ impl<U, E> Default for PrefixFrameworkOptions<U, E> {
ignore_bots: true,
ignore_thread_creation: true,
case_insensitive_commands: true,
non_command_message: None,
// help_when_mentioned: true,
// help_commmand: None,
// command_specific_help_commmand: None,
Expand Down

0 comments on commit 1f0abfb

Please sign in to comment.