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

Add User Apps #266

merged 7 commits into from
Nov 13, 2024

Conversation

fgardt
Copy link
Contributor

@fgardt fgardt commented May 14, 2024

this is the implementation for user apps that I've been using like this since 3 days after user apps got announced / released to beta.

I like the flexibility that having install_context and interaction_context being separate properties brings but there were concerns regarding usability / it being too complex.

example:

#[poise::command(
    slash_command,
    install_context = "Guild|User",
    interaction_context = "Guild|BotDm|PrivateChannel"
)]
pub async fn info(ctx: Ctx<'_>) -> Result<(), CommandError> {
  // ...command impl...
}

note: this added the unstable_discord_api feature to the serenity dependency, that should probably be removed again(?) I have not found a nice way to feature guard this without having to chain the feature flag in everywhere and some other issues.

@fgardt fgardt marked this pull request as ready for review May 14, 2024 21:07
@GnomedDev
Copy link
Member

I would recommend adding an unstable feature flag to poise, which enables the unstable_discord_api flag in serenity.

@C0D3-M4513R
Copy link

Also probably the docs should be updated with the new install_context and interaction_context options, if the unstable feature is enabled?

I can also confirm, that this pr works as expected, when using the current branch of serenity (as is expected).

@GnomedDev
Copy link
Member

Hey, @fgardt do you plan on fixing the CI failures?

@fgardt
Copy link
Contributor Author

fgardt commented May 26, 2024

I'll look into it yeah

@fgardt fgardt force-pushed the feat/user_apps branch 4 times, most recently from b238074 to 3809973 Compare June 1, 2024 22:40
@techs-sus
Copy link

techs-sus commented Jun 15, 2024

hello, i've been testing this pr more in depth and subcommands work weirdly:

  • the "parent" of the subcommands have full authority over interaction and install contexts
  • the subcommands themselves cannot override the interaction context which results in interesting behavior

lets say you have a parent command: /bot and you want to make a "reload" command for the bot owner which reloads the bot

#[poise::command(
    slash_command,
    subcommands("reload"),
    install_context = "Guild|User",
    // hmm... only 1 command needs to be different; so lets do a non-restrictive selection
    interaction_context = "Guild|BotDm|PrivateChannel"
)]
pub async fn bot(_: Ctx<'_>) -> Result<(), CommandError> {}

#[poise::command(
    slash_command,
    owners_only,
    // great! now we can make this a botdm command to prevent confusion?
    install_context = "User",
    interaction_context = "BotDm"
)]
pub async fn reload(ctx: Ctx<'_>) -> Result<(), CommandError> {
  // ... reload logic
}

sadly, the subcommand has no control over its interaction_context or its install_context, which means the command will show up in guilds and in group dms, which is not the intended behavior

@jamesbt365
Copy link
Member

jamesbt365 commented Jun 15, 2024

Subcommands inherit context and permissions from the parent, this is a limitation of discord itself. At most extra documentation (if there isn't any of inheriting context) can be added, but its already mentioned for permissions in our docs.

@SpeckledFleebeedoo
Copy link

Any idea when this will be merged/released? I'd like to see this functionality become available.

@jamesbt365
Copy link
Member

Any idea when this will be merged/released? I'd like to see this functionality become available.

You are welcome to use this as your base for the mean time but it would be nice to see the API in serenity be updated first before this rolls out. It'll happen when it does and there is no ETA for this feature.

@sawa-ko
Copy link

sawa-ko commented Jul 14, 2024

Any update on this?

@jamesbt365
Copy link
Member

jamesbt365 commented Jul 14, 2024

Considering this isn't merged or hasn't had a single commit since your question answers itself, no, there is no update on this.

@jamesbt365 jamesbt365 added blocked on serenity Depends on a feature from serenity that is yet to be released on crates.io and removed blocked on serenity Depends on a feature from serenity that is yet to be released on crates.io labels Jul 29, 2024
@Zickles
Copy link

Zickles commented Aug 31, 2024

What is missing from this pr?

@jamesbt365
Copy link
Member

User Apps should reasonably be stabilized on serenity first as the upstream discord API is stable now.

@sawa-ko
Copy link

sawa-ko commented Oct 17, 2024

I just tested this PR and it only registers 2 commands out of 15 at the user level.

#[poise::command(
    slash_command,
    prefix_command,
    rename = "avatar",
    category = "utils",
    required_bot_permissions = "SEND_MESSAGES | EMBED_LINKS",
    ephemeral,
    install_context = "Guild | User"
)]
pub async fn command(...) ...

@zkxs
Copy link

zkxs commented Oct 17, 2024

@sawa-ko I've been using this PR's User install context feature in a couple of my bots and it appears to work fine. You might need to restart your Discord client (or Ctrl+R) when you add new global commands to your bot, as it doesn't do a good job of picking them up automatically.

@sawa-ko
Copy link

sawa-ko commented Oct 17, 2024

@zkxs I did it, I also reinstalled the app and the same commands still appear, I also tried from the web / ios app and same result.

@fgardt
Copy link
Contributor Author

fgardt commented Oct 18, 2024

@sawa-ko did you set interaction_context? I'm not really sure what discord does when you only specify install_context.

@sawa-ko
Copy link

sawa-ko commented Oct 19, 2024

@fgardt Hum, I have tried it and same result. I have noticed that in some servers the commands appear and in others only 2 appear, what can it be?

@zkxs
Copy link

zkxs commented Oct 19, 2024

@fgardt The Discord docs say:

By default, contexts includes all interaction context types.

So unless serenity has its own special behavior when interaction_context is unset, I'd expect it to enable the commands in all interaction contexts. This matches what I've been seeing in my use of this PR: I don't set interaction_context and the commands can be used in both bot DMs and in guild channels.

@sawa-ko It might be helpful if you can share the command setup for a working and a broken command so we can compare. You only showed one example in #266 (comment), and it's unclear if that's one of the 2 working or the 13 broken. Or are all your commands set up identically? Is the bot installed to any of the servers you're testing against? Or is it only installed to your user? Is your command registration code registering all 15 commands globally?

@sawa-ko
Copy link

sawa-ko commented Oct 19, 2024

@zkxs @fgardt

Command working:

/// Check the bot's latency
///
/// This command will show you the bot's latency, the server it is currently on,
/// the number of members on the server, the bot's memory usage, and the bot's
/// ping with discord.
#[poise::command(
    slash_command,
    prefix_command,
    rename = "ping",
    category = "general",
    required_bot_permissions = "SEND_MESSAGES",
    ephemeral,
    install_context = "Guild | User",
    interaction_context = "Guild | BotDm | PrivateChannel"
)]
pub async fn command(ctx: CommandContext<'_>) -> CommandResult {}

Command not working:

#[poise::command(
    slash_command,
    prefix_command,
    rename = "avatar",
    category = "utils",
    required_bot_permissions = "SEND_MESSAGES | EMBED_LINKS",
    ephemeral,
    install_context = "Guild | User",
    interaction_context = "Guild | BotDm | PrivateChannel"
)]
pub async fn command(
    ctx: CommandContext<'_>,
    #[description = "User with the avatar that you need."] user: User,
) -> CommandResult {}

image

Correction: no more than two commands appear on any server, I did not realize that the bot was installed on the server where the bot was tested.

@zkxs
Copy link

zkxs commented Oct 19, 2024

@sawa-ko Seeing as those commands have identical setups (the difference in required_bot_permissions wouldn't prevent the command from not appearing) the only remaining idea I have is that your command registration code isn't registering the 13 missing commands. User commands need to be registered globally.

@sawa-ko
Copy link

sawa-ko commented Oct 19, 2024

@zkxs I don't think it's that because if I add the bot to the server they all appear.

@sawa-ko
Copy link

sawa-ko commented Oct 19, 2024

Ok, I saw what the problem was, I forgot that they are subcommands, not proper commands. 😭 Sorryyyyy.

@zkxs
Copy link

zkxs commented Oct 19, 2024

Huh, does anyone know if subcommands are supposed to work in user-installable apps? It's unclear to me if this is functionality that's missing in Discord itself or if this is an oversight in serenity, poise, or this PR.

@fgardt
Copy link
Contributor Author

fgardt commented Oct 19, 2024

@zkxs my use case is using subcommands (only 1 layer tho) and they work perfectly fine as userapps

@asibahi
Copy link
Contributor

asibahi commented Nov 9, 2024

Hello. My understanding is that User apps are now stable in Discord and serenity. Is there a way I can help to move this forward as I'd like to have my bot be a User App as well.

@jamesbt365
Copy link
Member

User apps are now stable in Discord and serenity

Not until serenity-rs/serenity#3018 and a release. You can speed things along by reviewing the aforementioned PR.

@asibahi
Copy link
Contributor

asibahi commented Nov 9, 2024

User apps are now stable in Discord and serenity

Not until serenity-rs/serenity#3018 and a release. You can speed things along by reviewing the aforementioned PR.

Unfortunately I do not have write access or I would look through it. It does seem mostly bookkeeping.

@asibahi
Copy link
Contributor

asibahi commented Nov 11, 2024

Ayy the serenity pr was merged

@fgardt
Copy link
Contributor Author

fgardt commented Nov 12, 2024

yeah I'll update this PR accordingly

@jamesbt365
Copy link
Member

A new serenity release has (finally) happened, nothing should be blocking this anymore.

Copy link
Member

@GnomedDev GnomedDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small mistake I spotted

macros/src/lib.rs Outdated Show resolved Hide resolved
macros/src/command/mod.rs Outdated Show resolved Hide resolved
macros/src/command/mod.rs Outdated Show resolved Hide resolved
@GnomedDev GnomedDev merged commit 773da6c into serenity-rs:current Nov 13, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants