Skip to main content

zaun_community_protocol/command/object/component/
find.rs

1//! Finding components for interactions (ยง8.14).
2
3use super::Component;
4
5/// The enabled interactive component (button with a `custom_id`, or a select) with
6/// `custom_id` among a message's components: what a `MESSAGE_COMPONENT` interaction may
7/// name.
8#[must_use]
9pub fn find_interactive<'a>(components: &'a [Component], custom_id: &str) -> Option<&'a Component> {
10    let mut stack: Vec<&Component> = components.iter().collect();
11    while let Some(c) = stack.pop() {
12        let enabled = match c {
13            Component::Button(b) => !b.disabled && b.custom_id.is_some(),
14            Component::StringSelect(s) => !s.disabled,
15            Component::UserSelect(s)
16            | Component::RoleSelect(s)
17            | Component::MentionableSelect(s)
18            | Component::ChannelSelect(s) => !s.disabled,
19            _ => {
20                stack.extend(c.children());
21                continue;
22            }
23        };
24        if enabled && c.custom_id() == Some(custom_id) {
25            return Some(c);
26        }
27    }
28    None
29}
30
31/// The input components of a modal (everything inside its labels), in order.
32#[must_use]
33pub fn modal_inputs(components: &[Component]) -> Vec<&Component> {
34    components
35        .iter()
36        .filter_map(|c| match c {
37            Component::Label(l) => Some(l.component.as_ref()),
38            _ => None,
39        })
40        .collect()
41}