Skip to content

Commit

Permalink
feat: add in hybrid slash commands (#1399)
Browse files Browse the repository at this point in the history
* feat: add in hybrid slash commands

Still relatively untested. Needs a lot of polish.

* feat: simulate specific slash restrains

* fix: oops, scope is weird

* fix: handle no options correctly

* feat: add use_slash_command_msg argument

* docs: add docs for hybrid command manager

* docs: add snippet for hybrid cmds in guide

* docs: finialize docs for hybrid commands

* fix: properly set x_id properties

* docs: adjust title of hybrid command section

* fix: handle dummy base command functions

* fix: parse subcommands correctly

* feat: add silence_autocomplete_errors

* fix: make options not keyword-only

This threw the prefixed command parser in for a loop.

* fix: use more logic to determine right kind of kind

* fix: properly handle keyword only

* feat: add support for aliases

* fix: add aliases to base commands too

* refactor: black learns how to not be dumb

* feat: remove hybrid command dm app permission handling

This wasn't matching slash command behavior

---------

Co-authored-by: Astrea49 <25420078+Astrea49@users.noreply.github.com>
  • Loading branch information
AstreaTSS and AstreaTSS authored Jul 5, 2023
1 parent 60edb6f commit 1d5a99f
Show file tree
Hide file tree
Showing 10 changed files with 1,183 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: interactions.ext.hybrid_commands.context
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: interactions.ext.hybrid_commands.hybrid_slash
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Hybrid Commands Index

- [Context](context)
- [Hybrid Slash](hybrid_slash)
- [Manager](manager)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
::: interactions.ext.hybrid_commands.manager
3 changes: 3 additions & 0 deletions docs/src/API Reference/API Reference/ext/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ These files contain useful features that help you develop a bot

- [Prefixed Commands](prefixed_commands)
- An extension to allow prefixed/text commands

- [Hybrid Commands](hybrid_commands)
- An extension that makes hybrid slash/prefixed commands
35 changes: 35 additions & 0 deletions docs/src/Guides/03 Creating Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,38 @@ There also is `on_command` which you can overwrite too. That fires on every inte
If your bot is complex enough, you might find yourself wanting to use custom models in your commands.

To do this, you'll want to use a string option, and define a converter. Information on how to use converters can be found [on the converter page](/Guides/08 Converters).

## I Want To Make A Prefixed/Text Command Too

You're in luck! You can use a hybrid command, which is a slash command that also gets converted to an equivalent prefixed command under the hood.

Hybrid commands are their own extension, and require [prefixed commands to set up beforehand](/interactions.py/Guides/26 Prefixed Commands). After that, use the `setup` function in the `hybrid_commands` extension in your main bot file.

Your setup can (but doesn't necessarily have to) look like this:

```python
import interactions
from interactions.ext import prefixed_commands as prefixed
from interactions.ext import hybrid_commands as hybrid

bot = interactions.Client(...) # may want to enable the message content intent
prefixed.setup(bot) # normal step for prefixed commands
hybrid.setup(bot) # note its usage AFTER prefixed commands have been set up
```

To actually make slash commands, simply replace `@slash_command` with `@hybrid_slash_command`, and `SlashContext` with `HybridContext`, like so:

```python
from interactions.ext.hybrid_commands import hybrid_slash_command, HybridContext

@hybrid_slash_command(name="my_command", description="My hybrid command!")
async def my_command_function(ctx: HybridContext):
await ctx.send("Hello World")
```

Suggesting you are using the default mention settings for your bot, you should be able to run this command by `@BotPing my_command`.

As you can see, the only difference between hybrid commands and slash commands, from a developer perspective, is that they use `HybridContext`, which attempts
to seamlessly allow using the same context for slash and prefixed commands. You can always get the underlying context via `inner_context`, though.

Of course, keep in mind that support two different types of commands is hard - some features may not get represented well in prefixed commands, and autocomplete is not possible at all.
12 changes: 12 additions & 0 deletions interactions/ext/hybrid_commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .context import HybridContext
from .hybrid_slash import HybridSlashCommand, hybrid_slash_command, hybrid_slash_subcommand
from .manager import HybridManager, setup

__all__ = (
"HybridContext",
"HybridManager",
"HybridSlashCommand",
"hybrid_slash_command",
"hybrid_slash_subcommand",
"setup",
)
Loading

0 comments on commit 1d5a99f

Please sign in to comment.