Skip to content

Commit

Permalink
Add User Apps
Browse files Browse the repository at this point in the history
  • Loading branch information
fgardt committed Jun 3, 2024
1 parent 8261d3f commit f4a2e40
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
40 changes: 40 additions & 0 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub struct CommandArgs {
category: Option<String>,
custom_data: Option<syn::Expr>,

install_context: Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,
interaction_context: Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,

// In seconds
global_cooldown: Option<u64>,
user_cooldown: Option<u64>,
Expand Down Expand Up @@ -97,6 +100,8 @@ pub struct Invocation {
default_member_permissions: syn::Expr,
required_permissions: syn::Expr,
required_bot_permissions: syn::Expr,
install_context: syn::Expr,
interaction_context: syn::Expr,
args: CommandArgs,
}

Expand Down Expand Up @@ -216,6 +221,34 @@ pub fn command(
let required_permissions = permissions_to_tokens(&args.required_permissions);
let required_bot_permissions = permissions_to_tokens(&args.required_bot_permissions);

fn build_install_context(
contexts: &Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,
) -> syn::Expr {
match contexts {
Some(contexts) => {
let contexts = contexts.iter();
syn::parse_quote! { Some(vec![ #(poise::serenity_prelude::InstallationContext::#contexts),* ]) }
}
None => syn::parse_quote! { None },
}
}

let install_context = build_install_context(&args.install_context);

fn build_interaction_context(
contexts: &Option<syn::punctuated::Punctuated<syn::Ident, syn::Token![|]>>,
) -> syn::Expr {
match contexts {
Some(contexts) => {
let contexts = contexts.iter();
syn::parse_quote! { Some(vec![ #(poise::serenity_prelude::InteractionContext::#contexts),* ]) }
}
None => syn::parse_quote! { None },
}
}

let interaction_context = build_interaction_context(&args.interaction_context);

let inv = Invocation {
parameters,
description,
Expand All @@ -225,6 +258,8 @@ pub fn command(
default_member_permissions,
required_permissions,
required_bot_permissions,
install_context,
interaction_context,
};

Ok(TokenStream::from(generate_command(inv)?))
Expand Down Expand Up @@ -291,6 +326,9 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
let dm_only = inv.args.dm_only;
let nsfw_only = inv.args.nsfw_only;

let install_context = &inv.install_context;
let interaction_context = &inv.interaction_context;

let help_text = match &inv.args.help_text_fn {
Some(help_text_fn) => quote::quote! { Some(#help_text_fn()) },
None => match &inv.help_text {
Expand Down Expand Up @@ -364,6 +402,8 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
guild_only: #guild_only,
dm_only: #dm_only,
nsfw_only: #nsfw_only,
install_context: #install_context,
interaction_context: #interaction_context,
checks: vec![ #( |ctx| Box::pin(#checks(ctx)) ),* ],
on_error: #on_error,
parameters: vec![ #( #parameters ),* ],
Expand Down
28 changes: 26 additions & 2 deletions src/structs/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ pub struct Command<U, E> {
pub context_menu_name: Option<String>,
/// Whether responses to this command should be ephemeral by default (application-only)
pub ephemeral: bool,
/// List of installation contexts for this command (application-only)
pub install_context: Option<Vec<serenity::InstallationContext>>,
/// List of interaction contexts for this command (application-only)
pub interaction_context: Option<Vec<serenity::InteractionContext>>,

// Like #[non_exhaustive], but #[poise::command] still needs to be able to create an instance
#[doc(hidden)]
Expand Down Expand Up @@ -196,7 +200,17 @@ impl<U, E> Command<U, E> {
}

if self.guild_only {
builder = builder.dm_permission(false);
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(interaction_context) = self.interaction_context.clone() {
builder = builder.contexts(interaction_context);
}

if self.subcommands.is_empty() {
Expand Down Expand Up @@ -230,7 +244,17 @@ impl<U, E> Command<U, E> {
});

if self.guild_only {
builder = builder.dm_permission(false);
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(interaction_context) = self.interaction_context.clone() {
builder = builder.contexts(interaction_context);
}

Some(builder)
Expand Down

0 comments on commit f4a2e40

Please sign in to comment.