Skip to content

Commit 2845b4f

Browse files
Merge pull request #1545 from interactions-py/unstable
5.10.0
2 parents 125ead2 + 6592f82 commit 2845b4f

27 files changed

+508
-330
lines changed

docs/src/Guides/03 Creating Commands.md

+58-60
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Creating Slash Commands
1+
# Slash Commands
22

33
So you want to make a slash command (or interaction, as they are officially called), but don't know how to get started?
44
Then this is the right place for you.
@@ -107,7 +107,7 @@ For all of these, the "group" parts are optional, allowing you to do `/base comm
107107
You cannot mix group subcommands and non-group subcommands into one base command - you must either use all group subcommands or normal subcommands.
108108

109109

110-
## But I Need More Options
110+
## Options
111111

112112
Interactions can also have options. There are a bunch of different [types of options](/interactions.py/API Reference/API Reference/models/Internal/application_commands/#interactions.models.internal.application_commands.OptionType):
113113

@@ -157,19 +157,21 @@ async def my_command_function(ctx: SlashContext, integer_option: int = 5):
157157

158158
For more information, please visit the API reference [here](/interactions.py/API Reference/API Reference/models/Internal/application_commands/#interactions.models.internal.application_commands.slash_option).
159159

160-
## Restricting Options
160+
### Restricting Options
161161

162162
If you are using an `OptionType.CHANNEL` option, you can restrict the channel a user can choose by setting `channel_types`:
163163
```python
164-
@slash_command(name="my_command", ...)
164+
from interactions import ChannelType, GuildText, OptionType, SlashContext, slash_command, slash_option
165+
166+
@slash_command(name="my_command")
165167
@slash_option(
166168
name="channel_option",
167169
description="Channel Option",
168170
required=True,
169171
opt_type=OptionType.CHANNEL,
170-
channel_types=[ChannelType.GUILD_TEXT]
172+
channel_types=[ChannelType.GUILD_TEXT],
171173
)
172-
async def my_command_function(ctx: SlashContext, channel_option: GUILD_TEXT):
174+
async def my_command_function(ctx: SlashContext, channel_option: GuildText):
173175
await channel_option.send("This is a text channel in a guild")
174176

175177
await ctx.send("...")
@@ -209,12 +211,12 @@ async def my_command_function(ctx: SlashContext, string_option: str):
209211
Be aware that the option `name` and the function parameter need to be the same (In this example both are `integer_option`).
210212

211213

212-
## But I Want A Choice
214+
## Option Choices
213215

214216
If your users ~~are dumb~~ constantly misspell specific strings, it might be wise to set up choices.
215-
With choices, the user can no longer freely input whatever they want, instead, they must choose from a curated list.
217+
With choices, the user can no longer freely input whatever they want, instead, they must choose from a pre-defined list.
216218

217-
To create a choice, simply fill `choices` in `@slash_option()`. An option can have up to 25 choices:
219+
To create a choice, simply fill `choices` in `@slash_option()`. An option can have up to 25 choices. The name of a choice is what will be shown in the Discord client of the user, while the value is what the bot will receive in its callback. Both can be the same.
218220
```python
219221
from interactions import SlashCommandChoice
220222

@@ -235,9 +237,9 @@ async def my_command_function(ctx: SlashContext, integer_option: int):
235237

236238
For more information, please visit the API reference [here](/interactions.py/API Reference/API Reference/models/Internal/application_commands/#interactions.models.internal.application_commands.SlashCommandChoice).
237239

238-
## I Need More Than 25 Choices
240+
## Autocomplete / More than 25 choices needed
239241

240-
Looks like you want autocomplete options. These dynamically show users choices based on their input.
242+
If you have more than 25 choices the user can choose from, or you want to give a dynamic list of choices depending on what the user is currently typing, then you will need autocomplete options.
241243
The downside is that you need to supply the choices on request, making this a bit more tricky to set up.
242244

243245
To use autocomplete options, set `autocomplete=True` in `@slash_option()`:
@@ -260,10 +262,10 @@ In there, you have three seconds to return whatever choices you want to the user
260262
```python
261263
from interactions import AutocompleteContext
262264

263-
@my_command.autocomplete("string_option")
265+
@my_command_function.autocomplete("string_option")
264266
async def autocomplete(self, ctx: AutocompleteContext):
265267
string_option_input = ctx.input_text # can be empty
266-
# you can use ctx.kwargs.get("name") for other options - note they can be empty too
268+
# you can use ctx.kwargs.get("name") to get the current state of other options - note they can be empty too
267269

268270
# make sure you respond within three seconds
269271
await ctx.send(
@@ -284,11 +286,12 @@ async def autocomplete(self, ctx: AutocompleteContext):
284286
)
285287
```
286288

287-
## But I Don't Like Decorators
289+
## Command definition without decorators
288290

289-
You are in luck. There are currently four different ways to create interactions, one does not need any decorators at all.
291+
There are currently four different ways to define interactions, one does not need any decorators at all.
290292

291293
=== ":one: Multiple Decorators"
294+
292295
```python
293296
@slash_command(name="my_command", description="My first command :)")
294297
@slash_option(
@@ -302,6 +305,7 @@ You are in luck. There are currently four different ways to create interactions,
302305
```
303306

304307
=== ":two: Single Decorator"
308+
305309
```python
306310
from interactions import SlashCommandOption
307311

@@ -322,6 +326,7 @@ You are in luck. There are currently four different ways to create interactions,
322326
```
323327

324328
=== ":three: Function Annotations"
329+
325330
```python
326331
from interactions import slash_int_option
327332

@@ -331,6 +336,7 @@ You are in luck. There are currently four different ways to create interactions,
331336
```
332337

333338
=== ":four: Manual Registration"
339+
334340
```python
335341
from interactions import SlashCommandOption
336342

@@ -353,34 +359,34 @@ You are in luck. There are currently four different ways to create interactions,
353359
)
354360
```
355361

356-
## I Don't Want My Friends Using My Commands
362+
## Restrict commands using permissions
357363

358-
How rude.
364+
It is possible to disable interactions (slash commands as well as context menus) for users that do not have a set of permissions.
359365

360-
Anyway, this is somewhat possible with command permissions.
361-
While you cannot explicitly block / allow certain roles / members / channels to use your commands on the bot side, you can define default permissions which members need to have to use the command.
366+
This functionality works for **permissions**, not to confuse with roles. If you want to restrict some command if the user does not have a certain role, this cannot be done on the bot side. However, it can be done on the Discord server side, in the Server Settings > Integrations page.
362367

363-
However, these default permissions can be overwritten by server admins, so this system is not safe for stuff like owner only eval commands.
364-
This system is designed to limit access to admin commands after a bot is added to a server, before admins have a chance to customise the permissions they want.
368+
!!!warning Administrators
369+
Remember that administrators of a Discord server have all permissions and therefore will always see the commands.
365370

366-
If you do not want admins to be able to overwrite your permissions, or the permissions are not flexible enough for you, you should use [checks][check-this-out].
371+
If you do not want admins to be able to overwrite your permissions, or the permissions are not flexible enough for you, you should use [checks][checks].
367372

368373
In this example, we will limit access to the command to members with the `MANAGE_EVENTS` and `MANAGE_THREADS` permissions.
369374
There are two ways to define permissions.
370375

371376
=== ":one: Decorators"
372-
```py
377+
378+
```python
373379
from interactions import Permissions, slash_default_member_permission
374380

375381
@slash_command(name="my_command")
376-
@slash_default_member_permission(Permissions.MANAGE_EVENTS)
377-
@slash_default_member_permission(Permissions.MANAGE_THREADS)
382+
@slash_default_member_permission(Permissions.MANAGE_EVENTS | Permissions.MANAGE_THREADS)
378383
async def my_command_function(ctx: SlashContext):
379384
...
380385
```
381386

382387
=== ":two: Function Definition"
383-
```py
388+
389+
```python
384390
from interactions import Permissions
385391

386392
@slash_command(
@@ -406,47 +412,43 @@ async def my_command_function(ctx: SlashContext):
406412
...
407413
```
408414

409-
### Context Menus
410-
411-
Both default permissions and DM blocking can be used the same way for context menus, since they are normal slash commands under the hood.
412-
413-
### Check This Out
415+
## Checks
414416

415417
Checks allow you to define who can use your commands however you want.
416418

417419
There are a few pre-made checks for you to use, and you can simply create your own custom checks.
418420

419-
=== "Build-In Check"
421+
=== ":one: Built-In Check"
420422
Check that the author is the owner of the bot:
421423

422-
```py
423-
from interactions import is_owner
424+
```python
425+
from interactions import SlashContext, check, is_owner, slash_command
424426

425-
@is_owner()
426427
@slash_command(name="my_command")
427-
async def my_command_function(ctx: SlashContext):
428-
...
428+
@check(is_owner())
429+
async def command(ctx: SlashContext):
430+
await ctx.send("You are the owner of the bot!", ephemeral=True)
429431
```
430432

431-
=== "Custom Check"
432-
Check that the author's name starts with `a`:
433+
=== ":two: Custom Check"
434+
Check that the author's username starts with `a`:
433435

434-
```py
435-
from interactions import check
436+
```python
437+
from interactions import BaseContext, SlashContext, check, slash_command
436438

437-
async def my_check(ctx: Context):
438-
return ctx.author.name.startswith("a")
439+
async def my_check(ctx: BaseContext):
440+
return ctx.author.username.startswith("a")
439441

440-
@check(check=my_check)
441442
@slash_command(name="my_command")
442-
async def my_command_function(ctx: SlashContext):
443-
...
443+
@check(my_check)
444+
async def command(ctx: SlashContext):
445+
await ctx.send("Your username starts with an 'a'!", ephemeral=True)
444446
```
445447

446-
=== "Reusing Checks"
448+
=== ":three: Reusing Checks"
447449
You can reuse checks in extensions by adding them to the extension check list
448450

449-
```py
451+
```python
450452
from interactions import Extension
451453

452454
class MyExtension(Extension):
@@ -461,18 +463,14 @@ There are a few pre-made checks for you to use, and you can simply create your o
461463
@slash_command(name="my_command2")
462464
async def my_command_function2(ctx: SlashContext):
463465
...
464-
465-
def setup(bot) -> None:
466-
MyExtension(bot)
467466
```
468467

469468
The check will be checked for every command in the extension.
470469

471470

471+
## Avoid redefining the same option everytime
472472

473-
## I Don't Want To Define The Same Option Every Time
474-
475-
If you are like me, you find yourself reusing options in different commands and having to redefine them every time which is both annoying and bad programming.
473+
If you have multiple commands that all use the same option, it might be both annoying and bad programming to redefine it multiple times.
476474

477475
Luckily, you can simply make your own decorators that themselves call `@slash_option()`:
478476
```python
@@ -517,17 +515,17 @@ async def on_command_error(self, event: CommandError):
517515

518516
There also is `CommandCompletion` which you can overwrite too. That fires on every interactions usage.
519517

520-
## I Need A Custom Parameter Type
518+
## Custom Parameter Type
521519

522520
If your bot is complex enough, you might find yourself wanting to use custom models in your commands.
523521

524-
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).
522+
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](../08 Converters).
525523

526-
## I Want To Make A Prefixed/Text Command Too
524+
## Prefixed/Text Commands
527525

528-
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.
526+
To use prefixed commands, instead of typing `/my_command`, you will need to type instead `!my_command`, provided that the prefix you set is `!`.
529527

530-
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.
528+
Hybrid commands are are slash commands that also get converted to an equivalent prefixed command under the hood. They are their own extension, and require [prefixed commands to be 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.
531529

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

@@ -556,4 +554,4 @@ Suggesting you are using the default mention settings for your bot, you should b
556554
As you can see, the only difference between hybrid commands and slash commands, from a developer perspective, is that they use `HybridContext`, which attempts
557555
to seamlessly allow using the same context for slash and prefixed commands. You can always get the underlying context via `inner_context`, though.
558556

559-
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.
557+
Of course, keep in mind that supporting two different types of commands is hard - some features may not get represented well in prefixed commands, and autocomplete is not possible at all.

docs/src/Guides/04 Context Menus.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Creating Context Menus
1+
# Context Menus
22

33
Context menus are interactions under the hood. Defining them is very similar.
44
Context menus work off `ctx.target` which contains the object the user interacted with.

0 commit comments

Comments
 (0)