Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User Apps #266

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/feature_showcase/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod response_with_reply;
mod subcommand_required;
mod subcommands;
mod track_edits;
mod user_apps;

use poise::serenity_prelude as serenity;

Expand Down Expand Up @@ -75,6 +76,11 @@ async fn main() {
subcommand_required::parent_subcommand_required(),
track_edits::test_reuse_response(),
track_edits::add(),
user_apps::everywhere(),
user_apps::everywhere_context(),
user_apps::user_install(),
user_apps::not_in_guilds(),
user_apps::user_install_guild(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("~".into()),
Expand Down
60 changes: 60 additions & 0 deletions examples/feature_showcase/user_apps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{Context, Error};
use poise::serenity_prelude as serenity;

// `install_context` determines how the bot has to be installed for a command to be available.
// `interaction_context` determines where a command can be used.

/// Available everywhere
#[poise::command(
slash_command,
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel"
)]
pub async fn everywhere(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("This command is available everywhere!").await?;
Ok(())
}

// also works with `context_menu_command`
/// Available everywhere
#[poise::command(
context_menu_command = "Everywhere",
install_context = "Guild|User",
interaction_context = "Guild|BotDm|PrivateChannel"
)]
pub async fn everywhere_context(ctx: Context<'_>, msg: serenity::Message) -> Result<(), Error> {
msg.reply(ctx, "This context menu is available everywhere!")
.await?;
Ok(())
}

/// Available with a user install only
#[poise::command(
slash_command,
install_context = "User",
interaction_context = "Guild|BotDm|PrivateChannel"
)]
pub async fn user_install(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("This command is available only with a user install!")
.await?;
Ok(())
}

/// Not available in guilds
#[poise::command(
slash_command,
install_context = "User",
interaction_context = "BotDm|PrivateChannel"
)]
pub async fn not_in_guilds(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("This command is not available in guilds!").await?;
Ok(())
}

/// User install only in guilds
#[poise::command(slash_command, install_context = "User", interaction_context = "Guild")]
pub async fn user_install_guild(ctx: Context<'_>) -> Result<(), Error> {
ctx.say("This command is available in guilds only with a user install!")
.await?;
Ok(())
}
27 changes: 27 additions & 0 deletions macros/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct CommandArgs {

manual_cooldowns: Option<bool>,

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 @@ -99,6 +102,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 @@ -218,6 +223,20 @@ pub fn command(
let required_permissions = permissions_to_tokens(&args.required_permissions);
let required_bot_permissions = permissions_to_tokens(&args.required_bot_permissions);

let install_context = if let Some(contexts) = &args.install_context {
let contexts = contexts.iter();
syn::parse_quote! { Some(vec![ #(poise::serenity_prelude::InstallationContext::#contexts),* ]) }
} else {
syn::parse_quote! { None }
};

let interaction_context = if let Some(contexts) = &args.interaction_context {
let contexts = contexts.iter();
syn::parse_quote! { Some(vec![ #(poise::serenity_prelude::InteractionContext::#contexts),* ]) }
} else {
syn::parse_quote! { None }
};

let inv = Invocation {
parameters,
description,
Expand All @@ -227,6 +246,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 @@ -294,6 +315,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 @@ -332,6 +356,7 @@ fn generate_command(mut inv: Invocation) -> Result<proc_macro2::TokenStream, dar
let function_generics = &inv.function.sig.generics;
let function_visibility = &inv.function.vis;
let function = &inv.function;

Ok(quote::quote! {
#[allow(clippy::str_to_string)]
#function_visibility fn #function_ident #function_generics() -> ::poise::Command<
Expand Down Expand Up @@ -368,6 +393,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
2 changes: 2 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ for example for command-specific help (i.e. `~help command_name`). Escape newlin
- `category`: Category of this command which affects placement in the help command
- `custom_data`: Arbitrary expression that will be boxed and stored in `Command::custom_data`
- `identifying_name`: Optionally, a unique identifier for this command for your personal usage
- `install_context`: Installation contexts where this command is available (slash-only)
- `interaction_context`: Interaction contexts where this command is available (slash-only)

## Checks

Expand Down
28 changes: 26 additions & 2 deletions src/structs/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,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 @@ -201,7 +205,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 @@ -235,7 +249,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
Loading