Skip to content

Commit

Permalink
Change return signatures of help commands to return Results (#1412)
Browse files Browse the repository at this point in the history
This changes the return type of the default help commands from `Option` to `Result` to allow the caller to decide how they want to present the error that occurred when sending the response.
  • Loading branch information
arqunis committed Mar 15, 2022
1 parent 06d101b commit a8cd62d
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/framework/standard/help_commands.rs
Expand Up @@ -103,15 +103,6 @@ macro_rules! format_command_name {
};
}

/// Wraps around [`warn`]-macro in order to keep
/// the literal same for all formats of help.
#[cfg(all(feature = "cache", feature = "http"))]
macro_rules! warn_about_failed_send {
($customised_help:expr, $error:expr) => {
warn!("Failed to send {:?} because: {:?}", $customised_help, $error);
};
}

/// A single group containing its name and all related commands that are eligible
/// in relation of help-settings measured to the user.
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -1293,14 +1284,19 @@ async fn send_error_embed(
/// groups: &[&'static CommandGroup],
/// owners: HashSet<UserId>,
/// ) -> CommandResult {
/// let _ = with_embeds(context, msg, args, &help_options, groups, owners).await;
/// let _ = with_embeds(context, msg, args, &help_options, groups, owners).await?;
/// Ok(())
/// }
///
/// let framework = StandardFramework::new().help(&MY_HELP);
/// ```
///
/// # Errors
///
/// Returns the same errors as [`ChannelId::send_message`].
///
/// [`StandardFramework::help`]: crate::framework::standard::StandardFramework::help
/// [`ChannelId::send_message`]: crate::model::id::ChannelId::send_message
#[cfg(all(feature = "cache", feature = "http"))]
#[allow(clippy::implicit_hasher)]
pub async fn with_embeds(
Expand All @@ -1310,7 +1306,7 @@ pub async fn with_embeds(
help_options: &HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId>,
) -> Option<Message> {
) -> Result<Message, Error> {
let formatted_help =
create_customised_help_data(ctx, msg, &args, groups, &owners, help_options).await;

Expand Down Expand Up @@ -1367,13 +1363,7 @@ pub async fn with_embeds(
},
};

match response_result {
Ok(response) => Some(response),
Err(why) => {
warn_about_failed_send!(&formatted_help, why);
None
},
}
response_result
}

/// Turns grouped commands into a [`String`] taking plain help format into account.
Expand Down Expand Up @@ -1503,12 +1493,17 @@ fn single_command_to_plain_string(help_options: &HelpOptions, command: &Command<
/// groups: &[&'static CommandGroup],
/// owners: HashSet<UserId>,
/// ) -> CommandResult {
/// let _ = plain(context, msg, args, &help_options, groups, owners).await;
/// let _ = plain(context, msg, args, &help_options, groups, owners).await?;
/// Ok(())
/// }
///
/// let framework = StandardFramework::new().help(&MY_HELP);
/// ```
/// # Errors
///
/// Returns the same errors as [`ChannelId::send_message`].
///
/// [`ChannelId::send_message`]: crate::model::id::ChannelId::send_message
#[cfg(all(feature = "cache", feature = "http"))]
#[allow(clippy::implicit_hasher)]
pub async fn plain(
Expand All @@ -1518,7 +1513,7 @@ pub async fn plain(
help_options: &HelpOptions,
groups: &[&'static CommandGroup],
owners: HashSet<UserId>,
) -> Option<Message> {
) -> Result<Message, Error> {
let formatted_help =
create_customised_help_data(ctx, msg, &args, groups, &owners, help_options).await;

Expand All @@ -1539,13 +1534,7 @@ pub async fn plain(
} => single_command_to_plain_string(help_options, command),
};

match msg.channel_id.say(&ctx, result).await {
Ok(response) => Some(response),
Err(why) => {
warn_about_failed_send!(&formatted_help, why);
None
},
}
msg.channel_id.say(&ctx, result).await
}

#[cfg(test)]
Expand Down

0 comments on commit a8cd62d

Please sign in to comment.