Skip to content

Modal derive macros#39

Draft
gpluscb wants to merge 6 commits intobaptiste0928:mainfrom
gpluscb:main
Draft

Modal derive macros#39
gpluscb wants to merge 6 commits intobaptiste0928:mainfrom
gpluscb:main

Conversation

@gpluscb
Copy link
Copy Markdown

@gpluscb gpluscb commented Jul 25, 2025

Adds two derive macros ModalModel and CreateModal as analogues to CommandModel and CreateCommand.

Short example:

#[derive(CreateModal, ModalModel)]
#[modal(
    title = "Give me your credit card information.",
    custom_id = "modal_demo::ExampleModal"
)]
pub struct ExampleModal {
    #[modal(label = "Credit card information here", style = "short")]
    credit_card_information: String,
}

let custom_id = interaction.id.to_string();
let response = ExampleModal::create_modal(custom_id.clone()).into();
interaction_client.create_response(interaction.id, &interaction.token, response).await?;

magic_event_provider.on_event(|event| async {
    let Event::InteractionCreate(interaction) = event else {
        return;
    };
    
    let interaction = interaction.0;
    if InteractionType::ModalSubmit != interaction.kind {
        return;
    }
    
    let Some(InteractionData::ModalSubmit(modal_data)) = interaction.data else {
        return;
    };
    
    if custom_id != modal_data.custom_id {
        return;
    }
    
    let modal = match ExampleModal::from_interaction(modal_data.into()).expect("couldn't parse modal");
    
    println!("Got someone's credit card info, it's {}!", modal.credit_card_information);
})

Draft pending

Regarding the custom_id handling:
A custom_id needs to be able to be set at runtime, otherwise it is impossible to distinguish replies for different instances of the same modal from each other.
To solve this, I plan to remove the custom_id attribute and instead give the create_modal function a custom_id parameter.
This has been solved by removing the custom_id attribute and instead giving the create_modal function a custom_id parameter.

Closes #15

@gpluscb
Copy link
Copy Markdown
Author

gpluscb commented Jul 25, 2025

I also am not sure if main is the correct target branch, so feel free to change that

@baptiste0928 baptiste0928 self-requested a review July 28, 2025 07:35
@gpluscb
Copy link
Copy Markdown
Author

gpluscb commented Aug 26, 2025

PR will need a rework for the new modal changes (https://discord.com/developers/docs/change-log#introducing-new-modal-components)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support modal interactions

1 participant