Skip to main content

zaun_bot/
export.rs

1//! [`export_bot!`](crate::export_bot).
2
3/// Exports a [`Bot`](crate::Bot) type as the component's `zaun:bot/bot` implementation.
4///
5/// The type must implement [`Bot`](crate::Bot) (and so [`Default`]). Use it once, in the
6/// bot crate's root.
7#[macro_export]
8macro_rules! export_bot {
9    ($bot:ty) => {
10        struct __ZaunBotExport;
11
12        ::std::thread_local! {
13            static __ZAUN_BOT: ::core::cell::RefCell<$bot> =
14                ::core::cell::RefCell::new(<$bot as ::core::default::Default>::default());
15        }
16
17        impl $crate::bindings::Guest for __ZaunBotExport {
18            fn init(
19                config: ::std::vec::Vec<(::std::string::String, ::std::string::String)>,
20            ) -> $crate::types::Registration {
21                __ZAUN_BOT.with_borrow_mut(|bot| $crate::run::init(bot, config))
22            }
23            fn on_config_change(
24                config: ::std::vec::Vec<(::std::string::String, ::std::string::String)>,
25            ) {
26                __ZAUN_BOT.with_borrow_mut(|bot| $crate::run::config(bot, config))
27            }
28            fn on_event(event: $crate::types::Event) {
29                __ZAUN_BOT.with_borrow_mut(|bot| $crate::run::event(bot, event))
30            }
31            fn on_interaction(interaction: $crate::types::Interaction) {
32                __ZAUN_BOT.with_borrow_mut(|bot| $crate::run::interaction(bot, interaction))
33            }
34            fn on_autocomplete(
35                interaction: $crate::types::Interaction,
36            ) -> ::std::vec::Vec<$crate::types::Choice> {
37                __ZAUN_BOT.with_borrow_mut(|bot| $crate::run::autocomplete(bot, interaction))
38            }
39            fn on_timer(id: u64, tag: ::std::string::String) {
40                __ZAUN_BOT.with_borrow_mut(|bot| $crate::Bot::on_timer(bot, id, tag))
41            }
42            fn on_voice_event(event: $crate::voice::VoiceEvent) {
43                __ZAUN_BOT.with_borrow_mut(|bot| $crate::Bot::on_voice_event(bot, event))
44            }
45        }
46
47        $crate::bindings::export!(__ZaunBotExport with_types_in $crate::bindings);
48    };
49}