pub struct FrameworkBuilder<U, E> { /* private fields */ }Expand description
A builder to configure a framework.
If one of the following required values is missing, the builder will panic on start:
Implementations§
Source§impl<U, E> FrameworkBuilder<U, E>
impl<U, E> FrameworkBuilder<U, E>
Sourcepub fn setup<F>(self, setup: F) -> Self
pub fn setup<F>(self, setup: F) -> Self
Sets the setup callback which also returns the user data struct.
Examples found in repository?
examples/quickstart/main.rs (lines 29-34)
20async fn main() {
21 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
22 let intents = serenity::GatewayIntents::non_privileged();
23
24 let framework = poise::Framework::builder()
25 .options(poise::FrameworkOptions {
26 commands: vec![age()],
27 ..Default::default()
28 })
29 .setup(|ctx, _ready, framework| {
30 Box::pin(async move {
31 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
32 Ok(Data {})
33 })
34 })
35 .build();
36
37 let client = serenity::ClientBuilder::new(token, intents)
38 .framework(framework)
39 .await;
40 client.unwrap().start().await.unwrap();
41}More examples
examples/event_handler/main.rs (lines 26-32)
17async fn main() {
18 tracing_subscriber::fmt::init();
19
20 let token = var("DISCORD_TOKEN")
21 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
22 let intents =
23 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
24
25 let framework = poise::Framework::builder()
26 .setup(move |_ctx, _ready, _framework| {
27 Box::pin(async move {
28 Ok(Data {
29 poise_mentions: AtomicU32::new(0),
30 })
31 })
32 })
33 .options(poise::FrameworkOptions {
34 event_handler: |framework, event| Box::pin(event_handler(framework, event)),
35 ..Default::default()
36 })
37 .build();
38
39 let client = serenity::ClientBuilder::new(token, intents)
40 .framework(framework)
41 .await;
42
43 client.unwrap().start().await.unwrap();
44}examples/advanced_cooldowns/main.rs (lines 41-46)
31async fn main() {
32 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
33 let framework = poise::Framework::builder()
34 .options(poise::FrameworkOptions {
35 commands: vec![dynamic_cooldowns()],
36 // This is important! Or else, the command will be marked as invoked before our custom
37 // cooldown code has run - even if the command ends up not running!
38 manual_cooldowns: true,
39 ..Default::default()
40 })
41 .setup(|ctx, _ready, framework| {
42 Box::pin(async move {
43 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
44 Ok(Data {})
45 })
46 })
47 .build();
48
49 let client = serenity::Client::builder(token, serenity::GatewayIntents::non_privileged())
50 .framework(framework)
51 .await;
52
53 client.unwrap().start().await.unwrap();
54}examples/fluent_localization/main.rs (line 80)
60async fn main() {
61 tracing_subscriber::fmt::init();
62
63 let mut commands = vec![welcome(), info(), register()];
64 let translations = translation::read_ftl().expect("failed to read translation files");
65
66 // We leak the translations so we can easily copy around `&'static str`s, to the downside
67 // that the OS will reclaim the memory at the end of `main` instead of the Drop implementation.
68 let translations: &'static translation::Translations = Box::leak(Box::new(translations));
69
70 translation::apply_translations(translations, &mut commands);
71
72 let token = std::env::var("TOKEN").unwrap();
73 let intents = serenity::GatewayIntents::non_privileged();
74
75 let framework = poise::Framework::builder()
76 .options(poise::FrameworkOptions {
77 commands,
78 ..Default::default()
79 })
80 .setup(move |_, _, _| Box::pin(async move { Ok(Data { translations }) }))
81 .build();
82
83 let client = serenity::ClientBuilder::new(token, intents)
84 .framework(framework)
85 .await;
86
87 client.unwrap().start().await.unwrap()
88}examples/invocation_data/main.rs (lines 57-68)
51async fn main() {
52 let token = std::env::var("TOKEN").unwrap();
53 let intents =
54 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
55
56 let framework = poise::Framework::builder()
57 .setup(move |ctx, _, framework| {
58 Box::pin(async move {
59 let commands =
60 poise::builtins::create_application_commands(&framework.options().commands);
61
62 serenity::GuildId::new(703332075914264606)
63 .set_commands(ctx, commands)
64 .await
65 .unwrap();
66 Ok(())
67 })
68 })
69 .options(poise::FrameworkOptions {
70 pre_command: |ctx| {
71 Box::pin(async move {
72 println!(
73 "In pre_command: {:?}",
74 ctx.invocation_data::<&str>().await.as_deref()
75 );
76 })
77 },
78 command_check: Some(|ctx| {
79 Box::pin(async move {
80 // Global command check is the first callback that's invoked, so let's set the
81 // data here
82 println!("Writing invocation data!");
83 ctx.set_invocation_data("hello").await;
84
85 println!(
86 "In global check: {:?}",
87 ctx.invocation_data::<&str>().await.as_deref()
88 );
89
90 Ok(true)
91 })
92 }),
93 post_command: |ctx| {
94 Box::pin(async move {
95 println!(
96 "In post_command: {:?}",
97 ctx.invocation_data::<&str>().await.as_deref()
98 );
99 })
100 },
101 on_error: |err| {
102 Box::pin(async move {
103 match err {
104 poise::FrameworkError::Command { ctx, .. } => {
105 println!(
106 "In on_error: {:?}",
107 ctx.invocation_data::<&str>().await.as_deref()
108 );
109 }
110 err => poise::builtins::on_error(err).await.unwrap(),
111 }
112 })
113 },
114
115 commands: vec![invocation_data_test()],
116 prefix_options: poise::PrefixFrameworkOptions {
117 prefix: Some("~".into()),
118 ..Default::default()
119 },
120 ..Default::default()
121 })
122 .build();
123
124 let client = serenity::ClientBuilder::new(token, intents)
125 .framework(framework)
126 .await;
127
128 client.unwrap().start().await.unwrap();
129}examples/basic_structure/main.rs (lines 97-105)
40async fn main() {
41 tracing_subscriber::fmt::init();
42
43 // FrameworkOptions contains all of poise's configuration option in one struct
44 // Every option can be omitted to use its default value
45 let options = poise::FrameworkOptions {
46 commands: vec![commands::vote(), commands::getvotes()],
47 prefix_options: poise::PrefixFrameworkOptions {
48 prefix: Some("~".into()),
49 edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
50 Duration::from_secs(3600),
51 ))),
52 additional_prefixes: vec![
53 poise::Prefix::Literal("hey bot,"),
54 poise::Prefix::Literal("hey bot"),
55 ],
56 ..Default::default()
57 },
58 // The global error handler for all error cases that may occur
59 on_error: |error| Box::pin(on_error(error)),
60 // This code is run before every command
61 pre_command: |ctx| {
62 Box::pin(async move {
63 println!("Executing command {}...", ctx.command().qualified_name);
64 })
65 },
66 // This code is run after a command if it was successful (returned Ok)
67 post_command: |ctx| {
68 Box::pin(async move {
69 println!("Executed command {}!", ctx.command().qualified_name);
70 })
71 },
72 // Every command invocation must pass this check to continue execution
73 command_check: Some(|ctx| {
74 Box::pin(async move {
75 if ctx.author().id == 123456789 {
76 return Ok(false);
77 }
78 Ok(true)
79 })
80 }),
81 // Enforce command checks even for owners (enforced by default)
82 // Set to true to bypass checks, which is useful for testing
83 skip_checks_for_owners: false,
84 event_handler: |_framework, event| {
85 Box::pin(async move {
86 println!(
87 "Got an event in event handler: {:?}",
88 event.snake_case_name()
89 );
90 Ok(())
91 })
92 },
93 ..Default::default()
94 };
95
96 let framework = poise::Framework::builder()
97 .setup(move |ctx, ready, framework| {
98 Box::pin(async move {
99 println!("Logged in as {}", ready.user.name);
100 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
101 Ok(Data {
102 votes: Mutex::new(HashMap::new()),
103 })
104 })
105 })
106 .options(options)
107 .build();
108
109 let token = var("DISCORD_TOKEN")
110 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
111 let intents =
112 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
113
114 let client = serenity::ClientBuilder::new(token, intents)
115 .framework(framework)
116 .await;
117
118 client.unwrap().start().await.unwrap()
119}Additional examples can be found in:
Sourcepub fn options(self, options: FrameworkOptions<U, E>) -> Self
pub fn options(self, options: FrameworkOptions<U, E>) -> Self
Configure framework options
Examples found in repository?
examples/quickstart/main.rs (lines 25-28)
20async fn main() {
21 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
22 let intents = serenity::GatewayIntents::non_privileged();
23
24 let framework = poise::Framework::builder()
25 .options(poise::FrameworkOptions {
26 commands: vec![age()],
27 ..Default::default()
28 })
29 .setup(|ctx, _ready, framework| {
30 Box::pin(async move {
31 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
32 Ok(Data {})
33 })
34 })
35 .build();
36
37 let client = serenity::ClientBuilder::new(token, intents)
38 .framework(framework)
39 .await;
40 client.unwrap().start().await.unwrap();
41}More examples
examples/event_handler/main.rs (lines 33-36)
17async fn main() {
18 tracing_subscriber::fmt::init();
19
20 let token = var("DISCORD_TOKEN")
21 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
22 let intents =
23 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
24
25 let framework = poise::Framework::builder()
26 .setup(move |_ctx, _ready, _framework| {
27 Box::pin(async move {
28 Ok(Data {
29 poise_mentions: AtomicU32::new(0),
30 })
31 })
32 })
33 .options(poise::FrameworkOptions {
34 event_handler: |framework, event| Box::pin(event_handler(framework, event)),
35 ..Default::default()
36 })
37 .build();
38
39 let client = serenity::ClientBuilder::new(token, intents)
40 .framework(framework)
41 .await;
42
43 client.unwrap().start().await.unwrap();
44}examples/advanced_cooldowns/main.rs (lines 34-40)
31async fn main() {
32 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
33 let framework = poise::Framework::builder()
34 .options(poise::FrameworkOptions {
35 commands: vec![dynamic_cooldowns()],
36 // This is important! Or else, the command will be marked as invoked before our custom
37 // cooldown code has run - even if the command ends up not running!
38 manual_cooldowns: true,
39 ..Default::default()
40 })
41 .setup(|ctx, _ready, framework| {
42 Box::pin(async move {
43 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
44 Ok(Data {})
45 })
46 })
47 .build();
48
49 let client = serenity::Client::builder(token, serenity::GatewayIntents::non_privileged())
50 .framework(framework)
51 .await;
52
53 client.unwrap().start().await.unwrap();
54}examples/fluent_localization/main.rs (lines 76-79)
60async fn main() {
61 tracing_subscriber::fmt::init();
62
63 let mut commands = vec![welcome(), info(), register()];
64 let translations = translation::read_ftl().expect("failed to read translation files");
65
66 // We leak the translations so we can easily copy around `&'static str`s, to the downside
67 // that the OS will reclaim the memory at the end of `main` instead of the Drop implementation.
68 let translations: &'static translation::Translations = Box::leak(Box::new(translations));
69
70 translation::apply_translations(translations, &mut commands);
71
72 let token = std::env::var("TOKEN").unwrap();
73 let intents = serenity::GatewayIntents::non_privileged();
74
75 let framework = poise::Framework::builder()
76 .options(poise::FrameworkOptions {
77 commands,
78 ..Default::default()
79 })
80 .setup(move |_, _, _| Box::pin(async move { Ok(Data { translations }) }))
81 .build();
82
83 let client = serenity::ClientBuilder::new(token, intents)
84 .framework(framework)
85 .await;
86
87 client.unwrap().start().await.unwrap()
88}examples/invocation_data/main.rs (lines 69-121)
51async fn main() {
52 let token = std::env::var("TOKEN").unwrap();
53 let intents =
54 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
55
56 let framework = poise::Framework::builder()
57 .setup(move |ctx, _, framework| {
58 Box::pin(async move {
59 let commands =
60 poise::builtins::create_application_commands(&framework.options().commands);
61
62 serenity::GuildId::new(703332075914264606)
63 .set_commands(ctx, commands)
64 .await
65 .unwrap();
66 Ok(())
67 })
68 })
69 .options(poise::FrameworkOptions {
70 pre_command: |ctx| {
71 Box::pin(async move {
72 println!(
73 "In pre_command: {:?}",
74 ctx.invocation_data::<&str>().await.as_deref()
75 );
76 })
77 },
78 command_check: Some(|ctx| {
79 Box::pin(async move {
80 // Global command check is the first callback that's invoked, so let's set the
81 // data here
82 println!("Writing invocation data!");
83 ctx.set_invocation_data("hello").await;
84
85 println!(
86 "In global check: {:?}",
87 ctx.invocation_data::<&str>().await.as_deref()
88 );
89
90 Ok(true)
91 })
92 }),
93 post_command: |ctx| {
94 Box::pin(async move {
95 println!(
96 "In post_command: {:?}",
97 ctx.invocation_data::<&str>().await.as_deref()
98 );
99 })
100 },
101 on_error: |err| {
102 Box::pin(async move {
103 match err {
104 poise::FrameworkError::Command { ctx, .. } => {
105 println!(
106 "In on_error: {:?}",
107 ctx.invocation_data::<&str>().await.as_deref()
108 );
109 }
110 err => poise::builtins::on_error(err).await.unwrap(),
111 }
112 })
113 },
114
115 commands: vec![invocation_data_test()],
116 prefix_options: poise::PrefixFrameworkOptions {
117 prefix: Some("~".into()),
118 ..Default::default()
119 },
120 ..Default::default()
121 })
122 .build();
123
124 let client = serenity::ClientBuilder::new(token, intents)
125 .framework(framework)
126 .await;
127
128 client.unwrap().start().await.unwrap();
129}examples/basic_structure/main.rs (line 106)
40async fn main() {
41 tracing_subscriber::fmt::init();
42
43 // FrameworkOptions contains all of poise's configuration option in one struct
44 // Every option can be omitted to use its default value
45 let options = poise::FrameworkOptions {
46 commands: vec![commands::vote(), commands::getvotes()],
47 prefix_options: poise::PrefixFrameworkOptions {
48 prefix: Some("~".into()),
49 edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
50 Duration::from_secs(3600),
51 ))),
52 additional_prefixes: vec![
53 poise::Prefix::Literal("hey bot,"),
54 poise::Prefix::Literal("hey bot"),
55 ],
56 ..Default::default()
57 },
58 // The global error handler for all error cases that may occur
59 on_error: |error| Box::pin(on_error(error)),
60 // This code is run before every command
61 pre_command: |ctx| {
62 Box::pin(async move {
63 println!("Executing command {}...", ctx.command().qualified_name);
64 })
65 },
66 // This code is run after a command if it was successful (returned Ok)
67 post_command: |ctx| {
68 Box::pin(async move {
69 println!("Executed command {}!", ctx.command().qualified_name);
70 })
71 },
72 // Every command invocation must pass this check to continue execution
73 command_check: Some(|ctx| {
74 Box::pin(async move {
75 if ctx.author().id == 123456789 {
76 return Ok(false);
77 }
78 Ok(true)
79 })
80 }),
81 // Enforce command checks even for owners (enforced by default)
82 // Set to true to bypass checks, which is useful for testing
83 skip_checks_for_owners: false,
84 event_handler: |_framework, event| {
85 Box::pin(async move {
86 println!(
87 "Got an event in event handler: {:?}",
88 event.snake_case_name()
89 );
90 Ok(())
91 })
92 },
93 ..Default::default()
94 };
95
96 let framework = poise::Framework::builder()
97 .setup(move |ctx, ready, framework| {
98 Box::pin(async move {
99 println!("Logged in as {}", ready.user.name);
100 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
101 Ok(Data {
102 votes: Mutex::new(HashMap::new()),
103 })
104 })
105 })
106 .options(options)
107 .build();
108
109 let token = var("DISCORD_TOKEN")
110 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
111 let intents =
112 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
113
114 let client = serenity::ClientBuilder::new(token, intents)
115 .framework(framework)
116 .await;
117
118 client.unwrap().start().await.unwrap()
119}Additional examples can be found in:
Sourcepub fn initialize_owners(self, initialize_owners: bool) -> Self
pub fn initialize_owners(self, initialize_owners: bool) -> Self
Whether to add this bot application’s owner and team members to
crate::FrameworkOptions::owners automatically
true by default
Sourcepub fn build(self) -> Framework<U, E>
pub fn build(self) -> Framework<U, E>
Build the framework with the specified configuration.
For more information, see FrameworkBuilder
Examples found in repository?
examples/quickstart/main.rs (line 35)
20async fn main() {
21 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
22 let intents = serenity::GatewayIntents::non_privileged();
23
24 let framework = poise::Framework::builder()
25 .options(poise::FrameworkOptions {
26 commands: vec![age()],
27 ..Default::default()
28 })
29 .setup(|ctx, _ready, framework| {
30 Box::pin(async move {
31 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
32 Ok(Data {})
33 })
34 })
35 .build();
36
37 let client = serenity::ClientBuilder::new(token, intents)
38 .framework(framework)
39 .await;
40 client.unwrap().start().await.unwrap();
41}More examples
examples/event_handler/main.rs (line 37)
17async fn main() {
18 tracing_subscriber::fmt::init();
19
20 let token = var("DISCORD_TOKEN")
21 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
22 let intents =
23 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
24
25 let framework = poise::Framework::builder()
26 .setup(move |_ctx, _ready, _framework| {
27 Box::pin(async move {
28 Ok(Data {
29 poise_mentions: AtomicU32::new(0),
30 })
31 })
32 })
33 .options(poise::FrameworkOptions {
34 event_handler: |framework, event| Box::pin(event_handler(framework, event)),
35 ..Default::default()
36 })
37 .build();
38
39 let client = serenity::ClientBuilder::new(token, intents)
40 .framework(framework)
41 .await;
42
43 client.unwrap().start().await.unwrap();
44}examples/advanced_cooldowns/main.rs (line 47)
31async fn main() {
32 let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
33 let framework = poise::Framework::builder()
34 .options(poise::FrameworkOptions {
35 commands: vec![dynamic_cooldowns()],
36 // This is important! Or else, the command will be marked as invoked before our custom
37 // cooldown code has run - even if the command ends up not running!
38 manual_cooldowns: true,
39 ..Default::default()
40 })
41 .setup(|ctx, _ready, framework| {
42 Box::pin(async move {
43 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
44 Ok(Data {})
45 })
46 })
47 .build();
48
49 let client = serenity::Client::builder(token, serenity::GatewayIntents::non_privileged())
50 .framework(framework)
51 .await;
52
53 client.unwrap().start().await.unwrap();
54}examples/fluent_localization/main.rs (line 81)
60async fn main() {
61 tracing_subscriber::fmt::init();
62
63 let mut commands = vec![welcome(), info(), register()];
64 let translations = translation::read_ftl().expect("failed to read translation files");
65
66 // We leak the translations so we can easily copy around `&'static str`s, to the downside
67 // that the OS will reclaim the memory at the end of `main` instead of the Drop implementation.
68 let translations: &'static translation::Translations = Box::leak(Box::new(translations));
69
70 translation::apply_translations(translations, &mut commands);
71
72 let token = std::env::var("TOKEN").unwrap();
73 let intents = serenity::GatewayIntents::non_privileged();
74
75 let framework = poise::Framework::builder()
76 .options(poise::FrameworkOptions {
77 commands,
78 ..Default::default()
79 })
80 .setup(move |_, _, _| Box::pin(async move { Ok(Data { translations }) }))
81 .build();
82
83 let client = serenity::ClientBuilder::new(token, intents)
84 .framework(framework)
85 .await;
86
87 client.unwrap().start().await.unwrap()
88}examples/invocation_data/main.rs (line 122)
51async fn main() {
52 let token = std::env::var("TOKEN").unwrap();
53 let intents =
54 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
55
56 let framework = poise::Framework::builder()
57 .setup(move |ctx, _, framework| {
58 Box::pin(async move {
59 let commands =
60 poise::builtins::create_application_commands(&framework.options().commands);
61
62 serenity::GuildId::new(703332075914264606)
63 .set_commands(ctx, commands)
64 .await
65 .unwrap();
66 Ok(())
67 })
68 })
69 .options(poise::FrameworkOptions {
70 pre_command: |ctx| {
71 Box::pin(async move {
72 println!(
73 "In pre_command: {:?}",
74 ctx.invocation_data::<&str>().await.as_deref()
75 );
76 })
77 },
78 command_check: Some(|ctx| {
79 Box::pin(async move {
80 // Global command check is the first callback that's invoked, so let's set the
81 // data here
82 println!("Writing invocation data!");
83 ctx.set_invocation_data("hello").await;
84
85 println!(
86 "In global check: {:?}",
87 ctx.invocation_data::<&str>().await.as_deref()
88 );
89
90 Ok(true)
91 })
92 }),
93 post_command: |ctx| {
94 Box::pin(async move {
95 println!(
96 "In post_command: {:?}",
97 ctx.invocation_data::<&str>().await.as_deref()
98 );
99 })
100 },
101 on_error: |err| {
102 Box::pin(async move {
103 match err {
104 poise::FrameworkError::Command { ctx, .. } => {
105 println!(
106 "In on_error: {:?}",
107 ctx.invocation_data::<&str>().await.as_deref()
108 );
109 }
110 err => poise::builtins::on_error(err).await.unwrap(),
111 }
112 })
113 },
114
115 commands: vec![invocation_data_test()],
116 prefix_options: poise::PrefixFrameworkOptions {
117 prefix: Some("~".into()),
118 ..Default::default()
119 },
120 ..Default::default()
121 })
122 .build();
123
124 let client = serenity::ClientBuilder::new(token, intents)
125 .framework(framework)
126 .await;
127
128 client.unwrap().start().await.unwrap();
129}examples/basic_structure/main.rs (line 107)
40async fn main() {
41 tracing_subscriber::fmt::init();
42
43 // FrameworkOptions contains all of poise's configuration option in one struct
44 // Every option can be omitted to use its default value
45 let options = poise::FrameworkOptions {
46 commands: vec![commands::vote(), commands::getvotes()],
47 prefix_options: poise::PrefixFrameworkOptions {
48 prefix: Some("~".into()),
49 edit_tracker: Some(Arc::new(poise::EditTracker::for_timespan(
50 Duration::from_secs(3600),
51 ))),
52 additional_prefixes: vec![
53 poise::Prefix::Literal("hey bot,"),
54 poise::Prefix::Literal("hey bot"),
55 ],
56 ..Default::default()
57 },
58 // The global error handler for all error cases that may occur
59 on_error: |error| Box::pin(on_error(error)),
60 // This code is run before every command
61 pre_command: |ctx| {
62 Box::pin(async move {
63 println!("Executing command {}...", ctx.command().qualified_name);
64 })
65 },
66 // This code is run after a command if it was successful (returned Ok)
67 post_command: |ctx| {
68 Box::pin(async move {
69 println!("Executed command {}!", ctx.command().qualified_name);
70 })
71 },
72 // Every command invocation must pass this check to continue execution
73 command_check: Some(|ctx| {
74 Box::pin(async move {
75 if ctx.author().id == 123456789 {
76 return Ok(false);
77 }
78 Ok(true)
79 })
80 }),
81 // Enforce command checks even for owners (enforced by default)
82 // Set to true to bypass checks, which is useful for testing
83 skip_checks_for_owners: false,
84 event_handler: |_framework, event| {
85 Box::pin(async move {
86 println!(
87 "Got an event in event handler: {:?}",
88 event.snake_case_name()
89 );
90 Ok(())
91 })
92 },
93 ..Default::default()
94 };
95
96 let framework = poise::Framework::builder()
97 .setup(move |ctx, ready, framework| {
98 Box::pin(async move {
99 println!("Logged in as {}", ready.user.name);
100 poise::builtins::register_globally(ctx, &framework.options().commands).await?;
101 Ok(Data {
102 votes: Mutex::new(HashMap::new()),
103 })
104 })
105 })
106 .options(options)
107 .build();
108
109 let token = var("DISCORD_TOKEN")
110 .expect("Missing `DISCORD_TOKEN` env var, see README for more information.");
111 let intents =
112 serenity::GatewayIntents::non_privileged() | serenity::GatewayIntents::MESSAGE_CONTENT;
113
114 let client = serenity::ClientBuilder::new(token, intents)
115 .framework(framework)
116 .await;
117
118 client.unwrap().start().await.unwrap()
119}Additional examples can be found in:
Trait Implementations§
Auto Trait Implementations§
impl<U, E> !RefUnwindSafe for FrameworkBuilder<U, E>
impl<U, E> !UnwindSafe for FrameworkBuilder<U, E>
impl<U, E> Freeze for FrameworkBuilder<U, E>
impl<U, E> Send for FrameworkBuilder<U, E>
impl<U, E> Sync for FrameworkBuilder<U, E>
impl<U, E> Unpin for FrameworkBuilder<U, E>
impl<U, E> UnsafeUnpin for FrameworkBuilder<U, E>where
Option<Box<dyn for<'a> FnOnce(&'a Context, &'a Ready, &'a Framework<U, E>) -> Pin<Box<dyn Future<Output = Result<U, E>> + Send + 'a>> + Sync + Send>>: UnsafeUnpin,
Option<FrameworkOptions<U, E>>: UnsafeUnpin,
Vec<Command<U, E>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more