Skip to content

Commit 1d5a99f

Browse files
authored
feat: add in hybrid slash commands (#1399)
* 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>
1 parent 60edb6f commit 1d5a99f

File tree

10 files changed

+1183
-0
lines changed

10 files changed

+1183
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: interactions.ext.hybrid_commands.context
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: interactions.ext.hybrid_commands.hybrid_slash
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Hybrid Commands Index
2+
3+
- [Context](context)
4+
- [Hybrid Slash](hybrid_slash)
5+
- [Manager](manager)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
::: interactions.ext.hybrid_commands.manager

docs/src/API Reference/API Reference/ext/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ These files contain useful features that help you develop a bot
1313

1414
- [Prefixed Commands](prefixed_commands)
1515
- An extension to allow prefixed/text commands
16+
17+
- [Hybrid Commands](hybrid_commands)
18+
- An extension that makes hybrid slash/prefixed commands

docs/src/Guides/03 Creating Commands.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,38 @@ There also is `on_command` which you can overwrite too. That fires on every inte
520520
If your bot is complex enough, you might find yourself wanting to use custom models in your commands.
521521

522522
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).
523+
524+
## I Want To Make A Prefixed/Text Command Too
525+
526+
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.
527+
528+
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.
529+
530+
Your setup can (but doesn't necessarily have to) look like this:
531+
532+
```python
533+
import interactions
534+
from interactions.ext import prefixed_commands as prefixed
535+
from interactions.ext import hybrid_commands as hybrid
536+
537+
bot = interactions.Client(...) # may want to enable the message content intent
538+
prefixed.setup(bot) # normal step for prefixed commands
539+
hybrid.setup(bot) # note its usage AFTER prefixed commands have been set up
540+
```
541+
542+
To actually make slash commands, simply replace `@slash_command` with `@hybrid_slash_command`, and `SlashContext` with `HybridContext`, like so:
543+
544+
```python
545+
from interactions.ext.hybrid_commands import hybrid_slash_command, HybridContext
546+
547+
@hybrid_slash_command(name="my_command", description="My hybrid command!")
548+
async def my_command_function(ctx: HybridContext):
549+
await ctx.send("Hello World")
550+
```
551+
552+
Suggesting you are using the default mention settings for your bot, you should be able to run this command by `@BotPing my_command`.
553+
554+
As you can see, the only difference between hybrid commands and slash commands, from a developer perspective, is that they use `HybridContext`, which attempts
555+
to seamlessly allow using the same context for slash and prefixed commands. You can always get the underlying context via `inner_context`, though.
556+
557+
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.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .context import HybridContext
2+
from .hybrid_slash import HybridSlashCommand, hybrid_slash_command, hybrid_slash_subcommand
3+
from .manager import HybridManager, setup
4+
5+
__all__ = (
6+
"HybridContext",
7+
"HybridManager",
8+
"HybridSlashCommand",
9+
"hybrid_slash_command",
10+
"hybrid_slash_subcommand",
11+
"setup",
12+
)

0 commit comments

Comments
 (0)