Skip to content

Commit

Permalink
properly support interaction subcommands w/ default subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Jun 12, 2024
1 parent 4c56240 commit 8a05968
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
36 changes: 27 additions & 9 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,12 +502,21 @@ impl ParseArgument for ImageUrl {
};
}

handle!(ctxt.commit_if_ok(ImageUrl::from_mention_raw_message).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_url_argument_raw_message).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_attachment).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_reply).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_emoji_raw_message).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_sticker).await);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_mention_raw_message(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_url_argument_raw_message(v).await)
.await
);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_attachment(v).await).await);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_reply(v).await).await);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_emoji_raw_message(v).await)
.await
);
handle!(ctxt.commit_if_ok(async |v| ImageUrl::from_sticker(v).await).await);
handle!(ImageUrl::from_channel_history(ctxt.cx.assyst(), ctxt.cx.data.message.channel_id).await);
Err(TagParseError::NoImageFound)
}
Expand Down Expand Up @@ -538,9 +547,18 @@ impl ParseArgument for ImageUrl {
};
}

handle!(ctxt.commit_if_ok(ImageUrl::from_mention_command_option).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_url_argument_command_option).await);
handle!(ctxt.commit_if_ok(ImageUrl::from_emoji_command_option).await);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_mention_command_option(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_url_argument_command_option(v).await)
.await
);
handle!(
ctxt.commit_if_ok(async |v| ImageUrl::from_emoji_command_option(v).await)
.await
);
handle!(ImageUrl::from_channel_history(ctxt.cx.assyst(), ctxt.cx.data.message.channel_id).await);
Err(TagParseError::NoImageFound)
}
Expand Down
11 changes: 10 additions & 1 deletion assyst-core/src/command/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub enum TagParseError {
NoImageFound,
MediaDownloadFail,
InvalidSubcommand,
NoInteractionSubcommandProvided,
InteractionCommandIsBaseSubcommand,
MismatchedCommandOptionType((String, CommandOptionValue)),
}

Expand All @@ -105,7 +107,8 @@ impl GetErrorSeverity for TagParseError {
| Self::TwilightDeserialize(..)
| Self::DownloadError(..)
| Self::UnsupportedSticker(..)
| Self::Reqwest(..) => ErrorSeverity::High,
| Self::Reqwest(..)
| Self::NoInteractionSubcommandProvided => ErrorSeverity::High,
_ => ErrorSeverity::Low,
}
}
Expand Down Expand Up @@ -150,6 +153,12 @@ impl Display for TagParseError {
"Command option mismatch between expected ({expected}) and received ({received:?})"
)
},
TagParseError::NoInteractionSubcommandProvided => {
f.write_str("Attempted to execute an interaction base command on a command group")
},
TagParseError::InteractionCommandIsBaseSubcommand => {
f.write_str("Interaction subcommand is base subcommand")
},
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions assyst-core/src/command/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ macro_rules! define_commandgroup {
$subcommand:literal => $commandfn:expr
),*
]
default_interaction_subcommand: $default_interaction_subcommand:expr
$(,default: $default:expr)?
) => {
paste::paste! {
Expand Down Expand Up @@ -82,6 +83,7 @@ macro_rules! define_commandgroup {
}

fn interaction_info(&self) -> crate::command::CommandInteractionInfo {
// todo
todo!()
}

Expand Down Expand Up @@ -128,10 +130,10 @@ macro_rules! define_commandgroup {

async fn execute_interaction_command(&self, ctxt: crate::command::InteractionCommandParseCtxt<'_>) -> Result<(), crate::command::ExecutionError> {
#![allow(unreachable_code)]
match crate::command::group::execute_subcommand_interaction_command(ctxt.fork(), Self::SUBCOMMANDS).await {
match crate::command::group::execute_subcommand_interaction_command(ctxt.fork(), Self::SUBCOMMANDS, $default_interaction_subcommand).await {
Ok(res) => Ok(res),
Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InvalidSubcommand)) => {
// No subcommand was found, call either the default if provided, or error out
Err(crate::command::ExecutionError::Parse(crate::command::errors::TagParseError::InteractionCommandIsBaseSubcommand)) => {
// Subcommand was "defau;t" command, call either the default if provided, or error out
$(
return [<$default _command>].execute_interaction_command(ctxt).await;
)?
Expand Down Expand Up @@ -171,14 +173,19 @@ pub fn find_subcommand_interaction_command(sub: &str, cmds: &[(&str, TCommand)])
pub async fn execute_subcommand_interaction_command(
ctxt: InteractionCommandParseCtxt<'_>,
commands: &[(&str, TCommand)],
default_interaction_subcommand: &str
) -> Result<(), ExecutionError> {
let subcommand = ctxt.cx.data.interaction_subcommand.clone().ok_or(ExecutionError::Parse(TagParseError::ArgsExhausted))?;
let subcommand = ctxt.cx.data.interaction_subcommand.clone().ok_or(ExecutionError::Parse(TagParseError::NoInteractionSubcommandProvided))?;
let subcommand = if let CommandOptionValue::SubCommand(c) = subcommand {
c.get(0).map(|x| x.name.clone()).unwrap()
} else {
unreachable!()
};

if subcommand == default_interaction_subcommand {
return Err(ExecutionError::Parse(TagParseError::InteractionCommandIsBaseSubcommand));
}

let command =
find_subcommand_interaction_command(&subcommand, commands).ok_or(ExecutionError::Parse(TagParseError::InvalidSubcommand))?;

Expand Down
3 changes: 2 additions & 1 deletion assyst-core/src/command/misc/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ define_commandgroup! {
usage: "<create>",
commands: [
"create" => create
],
]
default_interaction_subcommand: "view",
default: default
}

0 comments on commit 8a05968

Please sign in to comment.