Skip to content

Commit 952519a

Browse files
committed
New enumeration, SlashCommandOptionType
This is the equivalent of [ApplicationCommandOptionType](https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoptiontype) in the Discord API. Having this means the programmer doesnʼt have to remember which integer refers to which type.
1 parent 3bc95bf commit 952519a

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

discord_slash/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from .client import SlashCommand
12+
from .client import SlashCommandOptionType
1213
from .model import SlashContext
1314
from .utils import manage_commands
1415

discord_slash/client.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import model
88
from . import error
99
from .utils import manage_commands
10+
from enum import IntEnum
1011

1112

1213
class SlashCommand:
@@ -427,9 +428,10 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
427428
428429
.. code-block:: python
429430
430-
{"option_role": "role", # For key put name of the option and for value put type of the option.
431-
"option_user": 6, # Also can use number for type
432-
"option_channel": "CHANNEL"} # and all upper case.
431+
{"option_role": "role", # For key put name of the option and for value put type of the option.
432+
"option_user": SlashCommandOptionType.USER, # Also can use an enumeration member for the type
433+
"option_user_two": 6, # or number
434+
"option_channel": "CHANNEL"} # or upper case string.
433435
434436
:param name: Name of the slash command. Default name of the coroutine.
435437
:type name: str
@@ -565,15 +567,15 @@ async def process_options(self, guild: discord.Guild, options: list, auto_conver
565567
types = {
566568
"user": 0,
567569
"USER": 0,
568-
6: 0,
570+
SlashCommandOptionType.USER: 0,
569571
"6": 0,
570572
"channel": 1,
571573
"CHANNEL": 1,
572-
7: 1,
574+
SlashCommandOptionType.CHANNEL: 1,
573575
"7": 1,
574576
"role": 2,
575577
"ROLE": 2,
576-
8: 2,
578+
SlashCommandOptionType.ROLE: 2,
577579
"8": 2
578580
}
579581

@@ -731,3 +733,17 @@ async def on_slash_command_error(ctx, ex):
731733
return
732734
# Prints exception if not overrided or has no listener for error.
733735
self.logger.exception(f"An exception has occurred while executing command `{ctx.name}`:")
736+
737+
738+
class SlashCommandOptionType(IntEnum):
739+
"""
740+
Equivalent of `ApplicationCommandOptionType <https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoptiontype>`_ in the Discord API.
741+
"""
742+
SUB_COMMAND = 1
743+
SUB_COMMAND_GROUP = 2
744+
STRING = 3
745+
INTEGER = 4
746+
BOOLEAN = 5
747+
USER = 6
748+
CHANNEL = 7
749+
ROLE = 8

docs/quickstart.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Let's do some more complicated things:
7272
7373
import discord
7474
from discord_slash import SlashCommand
75+
from discord_slash import SlashCommandOptionType
7576
from discord_slash.utils import manage_commands
7677
7778
client = discord.Client(intents=discord.Intents.all())
@@ -87,7 +88,7 @@ Let's do some more complicated things:
8788
async def _ping(ctx):
8889
await ctx.send(content=f"Pong! ({client.latency*1000}ms)")
8990
90-
@slash.slash(name="echo", guild_ids=guild_ids, options=[manage_commands.create_option("string", "A random string.", 3, True)])
91+
@slash.slash(name="echo", guild_ids=guild_ids, options=[manage_commands.create_option("string", "A random string.", SlashCommandOptionType.STRING, True)])
9192
async def _echo(ctx, string):
9293
await ctx.send(content=string)
9394

0 commit comments

Comments
 (0)