From 1bc4b803c1a82bdf3cd89092a88ad373130a07fe Mon Sep 17 00:00:00 2001 From: Michael Flaherty Date: Wed, 20 Apr 2022 17:30:52 -0700 Subject: [PATCH] Allow users to reply to messages without codeblocks --- src/utls/parser.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/utls/parser.rs b/src/utls/parser.rs index f5030f6..370e3cf 100644 --- a/src/utls/parser.rs +++ b/src/utls/parser.rs @@ -138,23 +138,26 @@ pub async fn get_components(input: &str, author : &User, compilation_manager : O } else if find_code_block(&mut result, input, author).await? { // If we find a code block from our executor's message, and it's also a reply - // let's assume we found the stdin and what they're quoting is the code. + // let's assume we found the stdin and what they're replying to is the code. // Anything else probably doesn't make sense. if let Some(replied_msg) = reply { - result.stdin = result.code; - result.code = String::default(); - let attachment = get_message_attachment(&replied_msg.attachments).await?; if !attachment.0.is_empty() { if !result.target.is_empty() { result.target = attachment.1; } + // shift previos code to stdin, we have found code + result.stdin = result.code; + result.code = String::default(); result.code = attachment.0; } - else if !find_code_block(&mut result, &replied_msg.content, author).await? { - return Err(CommandError::from( - "Cannot find code to compile assuming your code block is the program's stdin.", - )) + else { + let mut fake_result = ParserResult::default(); + if find_code_block(&mut fake_result, &replied_msg.content, author).await? { + // we found a code block - lets assume the reply's codeblock is our actual code + result.stdin = result.code; + result.code = fake_result.code; + } } } } @@ -169,10 +172,10 @@ pub async fn get_components(input: &str, author : &User, compilation_manager : O } result.code = attachment.0; } - // no reply in the attachment, lets check for a code-block.. + // no attachment in the reply, lets check for a code-block.. else if !find_code_block(&mut result, &replied_msg.content, author).await? { return Err(CommandError::from( - "You must attach a code-block containing code to your message or quote a message that has one.", + "You must attach a code-block containing code to your message or reply to a message that has one.", )) } }