1use poise::serenity_prelude as serenity;
8
9type Error = &'static str;
10type Context<'a> = poise::Context<'a, (), Error>;
11
12async fn my_check(ctx: Context<'_>) -> Result<bool, Error> {
13 println!(
14 "In command specific check: {:?}",
15 ctx.invocation_data::<&str>().await.as_deref()
16 );
17
18 Ok(true)
19}
20
21async fn my_autocomplete(ctx: Context<'_>, _: &str) -> serenity::CreateAutocompleteResponse {
22 println!(
23 "In autocomplete: {:?}",
24 ctx.invocation_data::<&str>().await.as_deref()
25 );
26
27 serenity::CreateAutocompleteResponse::new()
28}
29
30#[poise::command(prefix_command, slash_command, check = "my_check")]
32pub async fn invocation_data_test(
33 ctx: Context<'_>,
34 #[description = "Whether this command should succeed (yes if 0, no otherwise)"]
35 #[autocomplete = "my_autocomplete"]
36 should_succeed: u32,
37) -> Result<(), Error> {
38 println!(
39 "In command: {:?}",
40 ctx.invocation_data::<&str>().await.as_deref()
41 );
42
43 if should_succeed > 0 {
44 Ok(())
45 } else {
46 Err("")
47 }
48}
49
50#[tokio::main]
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 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}