composure_macros 0.2.0

Procedural macros for Composure Discord bots: #[command], #[app], #[view] and rsx! markup
Documentation

composure_macros

The macros behind Composure: write a Discord command as a function, a message as markup.

Crates.io Docs.rs CI License MSRV

Don't depend on this crate directly: enable composure's macros feature, which re-exports everything here.

cargo add composure --features macros
use composure::{app, command, rsx, view, command::View};

/// Repeat some text
#[command]
fn echo(#[option("What to say", max_len = 200)] text: &str) -> String {
    text.to_string()
}

/// A button that counts its clicks, no database required
#[view]
pub fn Counter(count: u32) -> View {
    rsx! {
        <Text>"Clicked " {count} " times"</Text>
        <Row>
            <Button primary on:click:add=move |_| Counter { count: count + 1 }>"+1"</Button>
        </Row>
    }
}

#[app(handlers(echo, Counter))]
pub struct Bot;
Macro What it gives you
#[command] A slash or context-menu command from a function: Options from its typed parameters, the description from its doc comment
#[group] Subcommands: /mod ban, /mod kick
#[autocomplete] Suggestions while the user types
#[derive(Choice)] An enum as a fixed set of choices
#[component], #[modal] Handlers for buttons, selects and modal submits, with state carried in the custom_id
#[derive(Modal)] A form whose submit arrives as your struct
rsx! JSX-like markup for Components V2 messages and modals
#[view] A reusable layout with inline event handlers
#[app] Collects everything into the handler an Adapter serves

Mistakes are caught at compile time with errors that say what to do: a missing description, an Option bound outside its type, two commands with the same name, a button in the wrong place, a modal input without a matching parameter.

Learn them in the guide; every rsx! tag and attribute is in the Views reference.

License

Licensed under the Apache License, Version 2.0.