|
| 1 | +# Discord Slash Commands |
| 2 | +### `create_option()` and Command Types |
| 3 | + |
| 4 | +## `create_option()` Syntax |
| 5 | +```python |
| 6 | +create_option(name="something", description="Something else", option_type=3, required=True) |
| 7 | +``` |
| 8 | + |
| 9 | +In this snippet, an example option `something` is provided here for reference. |
| 10 | + |
| 11 | +* name: The option's name |
| 12 | +* description: What the use of the option is, or what it does. Make sure it is understandable by all users |
| 13 | +* option_type: Can only be defined as an `int`. The argument type, whether it is a string, integer, user mention, channel, role, etc. |
| 14 | +* required: Whether the option has to be inputted by the user or not |
| 15 | + |
| 16 | +## What is Discord Slash Command types? |
| 17 | +Discord slash command types define what kind of argument is expected in a slash command. |
| 18 | +For example, if I wanted to restrict integers to an argument `number`, I would write the command like this: |
| 19 | + |
| 20 | +```python |
| 21 | +import discord |
| 22 | +from discord_slash import SlashCommand |
| 23 | +from discord_slash.utils.manage_commands import create_option |
| 24 | +client = discord.Client() |
| 25 | +slash = SlashCommand(client) |
| 26 | +@slash.slash( |
| 27 | + name="some_command", |
| 28 | + description="Does something", |
| 29 | + options=[ |
| 30 | + create_option(name="number", description="Place to input a number", option_type=4, required=True) |
| 31 | + ] |
| 32 | +) |
| 33 | +async def some_command(ctx, number): |
| 34 | + await ctx.send(number + 1) |
| 35 | +``` |
| 36 | + |
| 37 | +Here, see line 10. You can see in `create_option()` that the `option_type` argument was passed as a `4`. This is because 4 refers to an integer in the discord_slash library. |
| 38 | + |
| 39 | +However, you can also directly define it in the method itself. |
| 40 | + |
| 41 | +```python |
| 42 | +import discord |
| 43 | +from discord_slash import SlashCommand |
| 44 | +client = discord.Client() |
| 45 | +slash = SlashCommand(client) |
| 46 | +@slash.slash( |
| 47 | + name="some_command", |
| 48 | + description="Does something" |
| 49 | +) |
| 50 | +async def some_command(ctx, number:int): |
| 51 | + await ctx.send(number + 1) |
| 52 | +``` |
| 53 | + |
| 54 | +In this snippet, see line 9. In this example, the argument type was directly defined in the command itself. The discord_slash library reads this and automatically sets that as the slash command `option_type`. However, to avoid confusion, it is best to just define it in `create_option()`. |
| 55 | + |
| 56 | +## Some Option Types |
| 57 | +Not all option types are provided here, just the ones that will be used the most. |
| 58 | + |
| 59 | +* sub-command: 1 |
| 60 | +* sub-command group: 2 |
| 61 | +* string: 3 |
| 62 | +* int: 4 |
| 63 | +* bool: 5 |
| 64 | +* user: 6 |
| 65 | +* channel: 7 |
| 66 | +* role: 8 |
| 67 | + |
| 68 | +<h6>NKA 2022 | Py | Discord | discord_slash | Discord library documentation</h6> |
0 commit comments