pub fn search_flow<'a>(
    event: &Event,
    bot: &'a CsmlBot,
    client: &Client,
    db: &mut Database,
) -> Result<(&'a CsmlFlow, String), EngineError> {
    match event {
        event if event.content_type == "flow_trigger" => {
            delete_state_key(&client, "hold", "position", db)?;

            let flow_trigger: FlowTrigger = serde_json::from_str(&event.content_value)?;

            match get_flow_by_id(&flow_trigger.flow_id, &bot.flows) {
                Ok(flow) => match flow_trigger.step_id {
                    Some(step_id) => Ok((flow, step_id)),
                    None => Ok((flow, "start".to_owned())),
                },
                Err(_) => Ok((
                    get_flow_by_id(&bot.default_flow, &bot.flows)?,
                    "start".to_owned(),
                )),
            }
        }
        event if event.content_type == "regex" => {
            let mut random_flows = vec![];

            for flow in bot.flows.iter() {
                let contains_command = flow.commands.iter().any(|cmd| {
                    if let Ok(action) = Regex::new(&event.content_value) {
                        action.is_match(&cmd)
                    } else {
                        false
                    }
                });

                if contains_command {
                    random_flows.push(flow)
                }
            }

            match random_flows.choose(&mut rand::thread_rng()) {
                Some(flow) => {
                    delete_state_key(&client, "hold", "position", db)?;
                    Ok((flow, "start".to_owned()))
                }
                None => Err(EngineError::Interpreter(format!(
                    "no match found for regex: {}",
                    event.content_value
                ))),
            }
        }
        event => {
            let mut random_flows = vec![];

            for flow in bot.flows.iter() {
                let contains_command = flow
                    .commands
                    .iter()
                    .any(|cmd| &cmd.as_str().to_lowercase() == &event.content_value.to_lowercase());

                if contains_command {
                    random_flows.push(flow)
                }
            }

            match random_flows.choose(&mut rand::thread_rng()) {
                Some(flow) => {
                    delete_state_key(&client, "hold", "position", db)?;
                    Ok((flow, "start".to_owned()))
                }
                None => Err(EngineError::Interpreter(format!(
                    "Flow '{}' does not exist",
                    event.content_value
                ))),
            }
        }
    }
}
/**
 * Find a flow in a bot based on the user's input.
 * - flow_trigger events must will match a flow's id or name and reset the hold position
 * - other events will try to match a flow trigger
 */
Find a flow in a bot based on the user's input.
- flow_trigger events must will match a flow's id or name and reset the hold position
- other events will try to match a flow trigger
Find a flow in a bot based on the user ' s input . - flow_trigger events must will match a flow ' s id or name and reset the hold position - other events will try to match a flow trigger
pub fn search_flow<'a>(
    event: &Event,
    bot: &'a CsmlBot,
    client: &Client,
    db: &mut Database,
) -> Result<(&'a CsmlFlow, String), EngineError> {
    match event {
        event if event.content_type == "flow_trigger" => {
            delete_state_key(&client, "hold", "position", db)?;
            let flow_trigger: FlowTrigger = serde_json::from_str(&event.content_value)?;
            match get_flow_by_id(&flow_trigger.flow_id, &bot.flows) {
                Ok(flow) => match flow_trigger.step_id {
                    Some(step_id) => Ok((flow, step_id)),
                    None => Ok((flow, "start".to_owned())),
                },
                Err(_) => Ok((
                    get_flow_by_id(&bot.default_flow, &bot.flows)?,
                    "start".to_owned(),
                )),
            }
        }
        event if event.content_type == "regex" => {
            let mut random_flows = vec![];
            for flow in bot.flows.iter() {
                let contains_command = flow.commands.iter().any(|cmd| {
                    if let Ok(action) = Regex::new(&event.content_value) {
                        action.is_match(&cmd)
                    } else {
                        false
                    }
                });
                if contains_command {
                    random_flows.push(flow)
                }
            }
            match random_flows.choose(&mut rand::thread_rng()) {
                Some(flow) => {
                    delete_state_key(&client, "hold", "position", db)?;
                    Ok((flow, "start".to_owned()))
                }
                None => Err(EngineError::Interpreter(format!(
                    "no match found for regex: {}",
                    event.content_value
                ))),
            }
        }
        event => {
            let mut random_flows = vec![];
            for flow in bot.flows.iter() {
                let contains_command = flow
                    .commands
                    .iter()
                    .any(|cmd| &cmd.as_str().to_lowercase() == &event.content_value.to_lowercase());
                if contains_command {
                    random_flows.push(flow)
                }
            }
            match random_flows.choose(&mut rand::thread_rng()) {
                Some(flow) => {
                    delete_state_key(&client, "hold", "position", db)?;
                    Ok((flow, "start".to_owned()))
                }
                None => Err(EngineError::Interpreter(format!(
                    "Flow '{}' does not exist",
                    event.content_value
                ))),
            }
        }
    }
}
pub fn search_flow < ' a > ( event : & Event , bot : & ' a CsmlBot , client : & Client , db : & mut Database , ) -> Result < ( & ' a CsmlFlow , String ) , EngineError > { match event { event if event . content_type == " " => { delete_state_key ( & client , " " , " " , db ) ? ; let flow_trigger : FlowTrigger = serde_json :: from_str ( & event . content_value ) ? ; match get_flow_by_id ( & flow_trigger . flow_id , & bot . flows ) { Ok ( flow ) => match flow_trigger . step_id { Some ( step_id ) => Ok ( ( flow , step_id ) ) , None => Ok ( ( flow , " " . to_owned ( ) ) ) , } , Err ( _ ) => Ok ( ( get_flow_by_id ( & bot . default_flow , & bot . flows ) ? , " " . to_owned ( ) , ) ) , } } event if event . content_type == " " => { let mut random_flows = vec ! [ ] ; for flow in bot . flows . iter ( ) { let contains_command = flow . commands . iter ( ) . any ( | cmd | { if let Ok ( action ) = Regex :: new ( & event . content_value ) { action . is_match ( & cmd ) } else { false } } ) ; if contains_command { random_flows . push ( flow ) } } match random_flows . choose ( & mut rand :: thread_rng ( ) ) { Some ( flow ) => { delete_state_key ( & client , " " , " " , db ) ? ; Ok ( ( flow , " " . to_owned ( ) ) ) } None => Err ( EngineError :: Interpreter ( format ! ( " " event content_value ) ) ) , } } event => { let mut random_flows = vec ! [ ] ; for flow in bot . flows . iter ( ) { let contains_command = flow . commands . iter ( ) . any ( | cmd | & cmd . as_str ( ) . to_lowercase ( ) == & event . content_value . to_lowercase ( ) ) ; if contains_command { random_flows . push ( flow ) } } match random_flows . choose ( & mut rand :: thread_rng ( ) ) { Some ( flow ) => { delete_state_key ( & client , " " , " " , db ) ? ; Ok ( ( flow , " " . to_owned ( ) ) ) } None => Err ( EngineError :: Interpreter ( format ! ( " " event content_value ) ) ) , } } } }
Find a flow in a bot based on the user's input.
Find a flow in a bot based on the user ' s input .
event
&Event
bot
&'a CsmlBot
client
&Client
db
&mut Database
event
&Event
bot
&'a CsmlBot
client
&Client
db
&mut Database