Skip to content

Commit f4b0d07

Browse files
authored
Add docs for AutocompleteContext, Option, OptionChoice, and on_application_command* events. (#1103)
* add docs for `AutocompleteContext`, `Option`, `OptionChoice`, and `on_application_command*` events. * revert unnecessary additional typing * add versionadded to more docstrings
1 parent 0fc0afd commit f4b0d07

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

discord/commands/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class AutocompleteContext:
290290
The option the user is currently typing.
291291
value: :class:`.str`
292292
The content of the focused option.
293-
options :class:`.dict`
293+
options: :class:`.dict`
294294
A name to value mapping of the options that the user has selected before this option.
295295
"""
296296

discord/commands/options.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,58 @@ def __name__(self):
5757

5858

5959
class Option:
60+
"""Represents a selectable option for a slash command.
61+
62+
Examples
63+
--------
64+
Basic usage: ::
65+
66+
@bot.slash_command(guild_ids=[...])
67+
async def hello(
68+
ctx: discord.ApplicationContext,
69+
name: Option(str, "Enter your name"),
70+
age: Option(int, "Enter your age", min_value=1, max_value=99, default=18)
71+
# passing the default value makes an argument optional
72+
# you also can create optional argument using:
73+
# age: Option(int, "Enter your age") = 18
74+
):
75+
await ctx.respond(f"Hello! Your name is {name} and you are {age} years old.")
76+
77+
.. versionadded:: 2.0
78+
79+
Attributes
80+
----------
81+
input_type: :class:`Any`
82+
The type of input that is expected for this option.
83+
description: :class:`str`
84+
The description of this option.
85+
Must be 100 characters or fewer.
86+
name: :class:`str`
87+
The name of this option visible in the UI.
88+
Inherits from the variable name if not provided as a parameter.
89+
choices: Optional[List[Union[:class:`Any`, :class:`OptionChoice`]]]
90+
The list of available choices for this option.
91+
Can be a list of values or :class:`OptionChoice` objects (which represent a name:value pair).
92+
If provided, the input from the user must match one of the choices in the list.
93+
required: Optional[:class:`bool`]
94+
Whether this option is required.
95+
default: Optional[:class:`Any`]
96+
The default value for this option. If provided, ``required`` will be considered ``False``.
97+
min_value: Optional[:class:`int`]
98+
The minimum value that can be entered.
99+
Only applies to Options with an input_type of ``int`` or ``float``.
100+
max_value: Optional[:class:`int`]
101+
The maximum value that can be entered.
102+
Only applies to Options with an input_type of ``int`` or ``float``.
103+
autocomplete: Optional[:class:`Any`]
104+
The autocomplete handler for the option. Accepts an iterable of :class:`str`, a callable (sync or async) that takes a
105+
single argument of :class:`AutocompleteContext`, or a coroutine. Must resolve to an iterable of :class:`str`.
106+
107+
.. note::
108+
109+
Does not validate the input value against the autocomplete results.
110+
"""
111+
60112
def __init__(self, input_type: Any, /, description: str = None, **kwargs) -> None:
61113
self.name: Optional[str] = kwargs.pop("name", None)
62114
self.description = description or "No description provided"
@@ -139,6 +191,19 @@ def __repr__(self):
139191

140192

141193
class OptionChoice:
194+
"""
195+
Represents a name:value pairing for a selected :class:`Option`.
196+
197+
.. versionadded:: 2.0
198+
199+
Attributes
200+
----------
201+
name: :class:`str`
202+
The name of the choice. Shown in the UI when selecting an option.
203+
value: Optional[Union[:class:`str`, :class:`int`, :class:`float`]]
204+
The value of the choice. If not provided, will use the value of ``name``.
205+
"""
206+
142207
def __init__(self, name: str, value: Optional[Union[str, int, float]] = None):
143208
self.name = name
144209
self.value = value if value is not None else name

discord/ui/input_text.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
class InputText:
1717
"""Represents a UI text input field.
1818
19+
.. versionadded:: 2.0
20+
1921
Parameters
2022
----------
2123
style: :class:`discord.InputTextStyle`

discord/ui/modal.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Modal:
2424
2525
This object must be inherited to create a UI within Discord.
2626
27+
.. versionadded:: 2.0
28+
2729
Parameters
2830
----------
2931
title: :class:`str`

docs/api.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ ApplicationContext
160160
.. autoclass:: ApplicationContext
161161
:members:
162162

163+
AutocompleteContext
164+
~~~~~~~~~~~~~~~~~~~
165+
166+
.. attributetable:: AutocompleteContext
167+
168+
.. autoclass:: AutocompleteContext
169+
:members:
170+
163171
Application Info
164172
------------------
165173

@@ -745,6 +753,36 @@ to handle it, which defaults to print a traceback and ignoring the exception.
745753
:param interaction: The interaction data.
746754
:type interaction: :class:`Interaction`
747755

756+
.. function:: on_application_command(context)
757+
758+
Called when an application command is received.
759+
760+
.. versionadded:: 2.0
761+
762+
:param context: The ApplicationContext associated to the command being received.
763+
:type context: :class:`ApplicationContext`
764+
765+
.. function:: on_application_command_completion(context)
766+
767+
Called when an application command is completed, after any checks have finished.
768+
769+
.. versionadded:: 2.0
770+
771+
:param context: The ApplicationContext associated to the command that was completed.
772+
:type context: :class:`ApplicationContext`
773+
774+
.. function:: on_application_command_error(context, exception)
775+
776+
Called when an application command has an error.
777+
778+
.. versionadded:: 2.0
779+
780+
:param context: The ApplicationContext associated to the command that has an error.
781+
:type context: :class:`ApplicationContext`
782+
783+
:param exception: The DiscordException associated to the error.
784+
:type exception: :class:`DiscordException`
785+
748786
.. function:: on_private_channel_update(before, after)
749787

750788
Called whenever a private group DM is updated. e.g. changed name or topic.
@@ -1335,6 +1373,8 @@ Utility Functions
13351373

13361374
.. autofunction:: discord.utils.generate_snowflake
13371375

1376+
.. autofunction:: discord.utils.basic_autocomplete
1377+
13381378
.. _discord-api-enums:
13391379

13401380
Enumerations

0 commit comments

Comments
 (0)