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

feat: add subcommand support #686

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

arily
Copy link
Contributor

@arily arily commented Dec 11, 2024

Describe your changes

  • add subcommand supports.
  • backwards compatible with old commands.
  • provided command-pretty-print util.

Related Issues / Projects

usage in production:
https://github.com/ppy-sb/bancho.py/blob/akat-0302-rebased-remove-query-builder/app/sb/streamer_commands.py

# top level command
streamer_commands = CommandSet("streamer", "Streamer commands.")

# register sub command
streamer_list_commands = streamer_commands.subcommand(
    CommandSet(
        "list", # subcommand keyword, "!streamer list" in this case
        "manage streamer list.",
    )
)

# register handlers like normal
@streamer_commands.add(Privileges.VERIFIED, aliases=["", "h"])
async def streamer_help(ctx: Context) -> str | None:
    """Show this message."""

    # provided help_pure will pretty-print commands as well as sub commands,
    # I'm open for better naming
    return help_pure(
        ctx,
        [*streamer_commands.commands, *streamer_commands.subcommands],
        app.settings.COMMAND_PREFIX + "streamer",
    )

# register subcommands
@streamer_list_commands.add(Privileges.DEVELOPER, aliases=["", "h"])
@streamer_list_commands.add(Privileges.MODERATOR, aliases=["", "h"])
@streamer_list_commands.add(Privileges.ADMINISTRATOR, aliases=["", "h"])
async def streamer_list_help(ctx: Context) -> str | None:
    """Show this message."""
    return help_pure(
        ctx,
        [*streamer_list_commands.commands, *streamer_list_commands.subcommands],
        app.settings.COMMAND_PREFIX + "streamer list",
    )

both commands can now be used in-game:

2024-12-11 10:49 arily: !streamer
2024-12-11 10:49 ChinoBot: !streamer help: Show this message.
!streamer list: manage streamer list.
2024-12-11 10:49 arily: !streamer list
2024-12-11 10:49 ChinoBot: !streamer list help: Show this message.

Checklist

  • I've manually tested my code

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.

1 participant