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

context_menu_command doesn't work for subcommands #178

Open
MaxenceDC opened this issue Jul 9, 2023 · 4 comments
Open

context_menu_command doesn't work for subcommands #178

MaxenceDC opened this issue Jul 9, 2023 · 4 comments
Labels
just needs work There is nothing left to discuss or decide, someone just needs to do it (probably me at some point)

Comments

@MaxenceDC
Copy link

The context_menu_command macro arguments doesn't work with subcommands

Maybe I'm doing it wrong, but this is the code I have :

use crate::{Context, Error};
use poise::serenity_prelude as serenity;

/// Get the avatar of a user or the icon of the current server.
#[poise::command(slash_command, subcommands("user", "server"))]
pub async fn picture(_: Context<'_>) -> Result<(), Error> {
    Ok(())
}

/// Get the icon of the current server.
#[poise::command(slash_command, guild_only)]
pub async fn server(ctx: Context<'_>) -> Result<(), Error> {
    ctx.say(
        ctx.guild()
            .unwrap()
            .icon_url()
            .unwrap_or("🫥 This server doesn't have an icon.".to_string())
            .replace("webp", "png?size=4096"),
    )
    .await?;
    Ok(())
}

/// Get the avatar of a user.
#[poise::command(context_menu_command = "Get avatar", slash_command)]
pub async fn user(
    ctx: Context<'_>,
    #[description = "Discord profile to query information about"] user: serenity::User,
) -> Result<(), Error> {
    let response = user.face().replace("webp?size=1024", "png?size=4096");
    ctx.say(response).await?;

    Ok(())
}

/picture user and /picture server work fine when invoking them via slash commands, but when I try to invoke Get avatar, I get no reponse (timeout) and the function is never called (but is still registered as a context menu command!)

@MaxenceDC MaxenceDC changed the title context_menu_command doesn't work for subcommands context_menu_command doesn't work for subcommands Jul 9, 2023
@kangalio
Copy link
Collaborator

Thank you for the report

fn find_matching_command<'a, 'b, U, E>(
interaction_name: &str,
interaction_options: &'b [serenity::CommandDataOption],
commands: &'a [crate::Command<U, E>],
parent_commands: &mut Vec<&'a crate::Command<U, E>>,
) -> Option<(&'a crate::Command<U, E>, &'b [serenity::CommandDataOption])> {
commands.iter().find_map(|cmd| {
if interaction_name != cmd.name && Some(interaction_name) != cmd.context_menu_name {
return None;
}
if let Some(sub_interaction) = interaction_options.iter().find(|option| {
option.kind == serenity::CommandOptionType::SubCommand
|| option.kind == serenity::CommandOptionType::SubCommandGroup
}) {
parent_commands.push(cmd);
find_matching_command(
&sub_interaction.name,
&sub_interaction.options,
&cmd.subcommands,
parent_commands,
)
} else {
Some((cmd, interaction_options))
}
})
}

I think the problem is here - subcommands are only traversed when the interaction has a subcommand too

@MaxenceDC
Copy link
Author

And thank you for this really great crate!

So can I fix it in my code or do I have to wait for a release? I'd like to make a PR with a fix but I'm really unfamiliar to Rust macros...

@kangalio
Copy link
Collaborator

This is a bug in poise, not your crate, but you can work around the bug by having two seperate functions for the slash version and the context menu version of your user / Get avatar command, where the context menu version is registered in poise as top-level, so that it doesn't hit this context menu subcommand bug

@MaxenceDC
Copy link
Author

That's kind of what I have atm, I'll keep it that way for now, thank's anyway!

@kangalio kangalio added the just needs work There is nothing left to discuss or decide, someone just needs to do it (probably me at some point) label Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
just needs work There is nothing left to discuss or decide, someone just needs to do it (probably me at some point)
Projects
None yet
Development

No branches or pull requests

2 participants