From 524d962cba34f24d73dfe43d595b4c395f7270ad Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 17 May 2024 11:44:44 +0200 Subject: [PATCH] Add unstable feature flag --- Cargo.toml | 1 + macros/Cargo.toml | 3 ++ macros/src/command/mod.rs | 70 ++++++++++++++++++++++++++++++++++++++- src/structs/command.rs | 54 ++++++++++++++++++++---------- 4 files changed, 109 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 913c2216fbc1..ddefa04d1868 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ collector = [] # This feature exists because some users want to disable the mere possibility of catching panics at # build time for peace of mind. handle_panics = [] +unstable = ["serenity/unstable_discord_api", "poise_macros/unstable"] [package.metadata.docs.rs] all-features = true diff --git a/macros/Cargo.toml b/macros/Cargo.toml index cc3512de1d56..73e740e35f48 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -15,3 +15,6 @@ syn = { version = "2", features = ["fold"] } quote = "1.0.9" proc-macro2 = "1.0.24" darling = "0.20" + +[features] +unstable = [] diff --git a/macros/src/command/mod.rs b/macros/src/command/mod.rs index 80682fec57f4..1ff54683fbea 100644 --- a/macros/src/command/mod.rs +++ b/macros/src/command/mod.rs @@ -49,7 +49,9 @@ pub struct CommandArgs { category: Option, custom_data: Option, + #[cfg(feature = "unstable")] install_context: Option>, + #[cfg(feature = "unstable")] interaction_context: Option>, // In seconds @@ -100,7 +102,9 @@ pub struct Invocation { default_member_permissions: syn::Expr, required_permissions: syn::Expr, required_bot_permissions: syn::Expr, + #[cfg(feature = "unstable")] install_context: syn::Expr, + #[cfg(feature = "unstable")] interaction_context: syn::Expr, args: CommandArgs, } @@ -221,6 +225,7 @@ pub fn command( let required_permissions = permissions_to_tokens(&args.required_permissions); let required_bot_permissions = permissions_to_tokens(&args.required_bot_permissions); + #[cfg(feature = "unstable")] fn build_install_context( contexts: &Option>, ) -> syn::Expr { @@ -233,8 +238,10 @@ pub fn command( } } + #[cfg(feature = "unstable")] let install_context = build_install_context(&args.install_context); + #[cfg(feature = "unstable")] fn build_interaction_context( contexts: &Option>, ) -> syn::Expr { @@ -247,6 +254,7 @@ pub fn command( } } + #[cfg(feature = "unstable")] let interaction_context = build_interaction_context(&args.interaction_context); let inv = Invocation { @@ -258,7 +266,9 @@ pub fn command( default_member_permissions, required_permissions, required_bot_permissions, + #[cfg(feature = "unstable")] install_context, + #[cfg(feature = "unstable")] interaction_context, }; @@ -326,7 +336,9 @@ fn generate_command(mut inv: Invocation) -> Result Result ::poise::Command< <#ctx_type_with_static as poise::_GetGenerics>::U, @@ -417,6 +431,60 @@ fn generate_command(mut inv: Invocation) -> Result ::poise::Command< + <#ctx_type_with_static as poise::_GetGenerics>::U, + <#ctx_type_with_static as poise::_GetGenerics>::E, + > { + #function + + ::poise::Command { + prefix_action: #prefix_action, + slash_action: #slash_action, + context_menu_action: #context_menu_action, + + subcommands: vec![ #( #subcommands() ),* ], + subcommand_required: #subcommand_required, + name: #command_name.to_string(), + name_localizations: #name_localizations, + qualified_name: String::from(#command_name), // properly filled in later by Framework + identifying_name: String::from(#identifying_name), + source_code_name: String::from(#function_name), + category: #category, + description: #description, + description_localizations: #description_localizations, + help_text: #help_text, + hide_in_help: #hide_in_help, + cooldowns: std::sync::Mutex::new(::poise::Cooldowns::new()), + cooldown_config: #cooldown_config, + reuse_response: #reuse_response, + default_member_permissions: #default_member_permissions, + required_permissions: #required_permissions, + required_bot_permissions: #required_bot_permissions, + owners_only: #owners_only, + guild_only: #guild_only, + dm_only: #dm_only, + nsfw_only: #nsfw_only, + checks: vec![ #( |ctx| Box::pin(#checks(ctx)) ),* ], + on_error: #on_error, + parameters: vec![ #( #parameters ),* ], + custom_data: #custom_data, + + aliases: vec![ #( #aliases.to_string(), )* ], + invoke_on_edit: #invoke_on_edit, + track_deletion: #track_deletion, + broadcast_typing: #broadcast_typing, + + context_menu_name: #context_menu_name, + ephemeral: #ephemeral, + __non_exhaustive: (), } } diff --git a/src/structs/command.rs b/src/structs/command.rs index 647e175e6ee1..0dd7b530b720 100644 --- a/src/structs/command.rs +++ b/src/structs/command.rs @@ -122,8 +122,10 @@ pub struct Command { pub context_menu_name: Option, /// Whether responses to this command should be ephemeral by default (application-only) pub ephemeral: bool, + #[cfg(feature = "unstable")] /// List of installation contexts for this command (application-only) pub install_context: Option>, + #[cfg(feature = "unstable")] /// List of interaction contexts for this command (application-only) pub interaction_context: Option>, @@ -199,18 +201,26 @@ impl Command { builder = builder.default_member_permissions(self.default_member_permissions); } - if self.guild_only { - builder = builder.contexts(vec![serenity::InteractionContext::Guild]); - } else if self.dm_only { - builder = builder.contexts(vec![serenity::InteractionContext::BotDm]); - } + #[cfg(feature = "unstable")] + { + if self.guild_only { + builder = builder.contexts(vec![serenity::InteractionContext::Guild]); + } else if self.dm_only { + builder = builder.contexts(vec![serenity::InteractionContext::BotDm]); + } - if let Some(install_context) = self.install_context.clone() { - builder = builder.integration_types(install_context); + if let Some(install_context) = self.install_context.clone() { + builder = builder.integration_types(install_context); + } + + if let Some(interaction_context) = self.interaction_context.clone() { + builder = builder.contexts(interaction_context); + } } - if let Some(interaction_context) = self.interaction_context.clone() { - builder = builder.contexts(interaction_context); + #[cfg(not(feature = "unstable"))] + if self.guild_only { + builder = builder.dm_permission(false); } if self.subcommands.is_empty() { @@ -243,18 +253,26 @@ impl Command { crate::ContextMenuCommandAction::__NonExhaustive => unreachable!(), }); - if self.guild_only { - builder = builder.contexts(vec![serenity::InteractionContext::Guild]); - } else if self.dm_only { - builder = builder.contexts(vec![serenity::InteractionContext::BotDm]); - } + #[cfg(feature = "unstable")] + { + if self.guild_only { + builder = builder.contexts(vec![serenity::InteractionContext::Guild]); + } else if self.dm_only { + builder = builder.contexts(vec![serenity::InteractionContext::BotDm]); + } - if let Some(install_context) = self.install_context.clone() { - builder = builder.integration_types(install_context); + if let Some(install_context) = self.install_context.clone() { + builder = builder.integration_types(install_context); + } + + if let Some(interaction_context) = self.interaction_context.clone() { + builder = builder.contexts(interaction_context); + } } - if let Some(interaction_context) = self.interaction_context.clone() { - builder = builder.contexts(interaction_context); + #[cfg(not(feature = "unstable"))] + if self.guild_only { + builder = builder.dm_permission(false); } Some(builder)