|
1 | 1 | //! Modal trait and utility items for implementing it (mainly for the derive macro) |
2 | 2 |
|
3 | 3 | use crate::serenity_prelude as serenity; |
| 4 | +use serenity::all::{Component, LabelComponent}; |
4 | 5 |
|
5 | 6 | /// Meant for use in derived [`Modal::parse`] implementation |
6 | 7 | /// |
7 | | -/// _Takes_ the String out of the data. Logs warnings on unexpected state |
| 8 | +/// _Takes_ the String out of the first InputText component that has the given `custom_id`. |
| 9 | +/// Logs warnings on unexpected state. |
8 | 10 | #[doc(hidden)] |
9 | 11 | pub fn find_modal_text( |
10 | 12 | data: &mut serenity::ModalInteractionData, |
11 | 13 | custom_id: &str, |
12 | 14 | ) -> Option<String> { |
13 | | - for row in data.components.iter_mut() { |
14 | | - let text = match row.components.get_mut(0) { |
15 | | - Some(serenity::ActionRowComponent::InputText(text)) => text, |
16 | | - Some(_) => { |
| 15 | + for component in data.components.iter_mut() { |
| 16 | + // text inputs can either exist in Labels or Containers |
| 17 | + match component { |
| 18 | + Component::Label(label) => match &mut label.component { |
| 19 | + LabelComponent::InputText(input_text) => { |
| 20 | + if input_text.custom_id == custom_id { |
| 21 | + return match std::mem::take(&mut input_text.value) { |
| 22 | + Some(val) if val.is_empty() => None, |
| 23 | + Some(val) => Some(val.into_string()), |
| 24 | + None => None, |
| 25 | + }; |
| 26 | + } |
| 27 | + } |
| 28 | + _ => { |
| 29 | + tracing::warn!("unexpected non input text component in modal response"); |
| 30 | + continue; |
| 31 | + } |
| 32 | + }, |
| 33 | + _ => { |
17 | 34 | tracing::warn!("unexpected non input text component in modal response"); |
18 | 35 | continue; |
19 | 36 | } |
20 | | - None => { |
21 | | - tracing::warn!("empty action row in modal response"); |
22 | | - continue; |
23 | | - } |
24 | | - }; |
25 | | - |
26 | | - if text.custom_id == custom_id { |
27 | | - return match std::mem::take(&mut text.value) { |
28 | | - Some(val) if val.is_empty() => None, |
29 | | - Some(val) => Some(val.into_string()), |
30 | | - None => None, |
31 | | - }; |
32 | 37 | } |
33 | 38 | } |
34 | 39 | tracing::warn!( |
|
0 commit comments