Skip to content

Commit e50c239

Browse files
authored
Merge pull request #935 from krittick/actx-option-properties
Add `selected_options` and `unselected_options` properties to `ApplicationContext`
2 parents d1e1670 + 624c831 commit e50c239

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

discord/commands/context.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525
from __future__ import annotations
2626

27-
from typing import TYPE_CHECKING, Optional, TypeVar, Union
27+
from typing import TYPE_CHECKING, Optional, TypeVar, Union, Dict, List
2828

2929
import discord.abc
3030
from discord.interactions import InteractionMessage
@@ -176,6 +176,36 @@ def voice_client(self) -> Optional[VoiceProtocol]:
176176
def response(self) -> InteractionResponse:
177177
return self.interaction.response
178178

179+
@property
180+
def selected_options(self) -> Optional[List[Dict]]:
181+
"""The options and values that were selected by the user when sending the command.
182+
183+
Returns
184+
-------
185+
Optional[List[Dict]]
186+
A dictionary containing the options and values that were selected by the user when the command was processed, if applicable.
187+
"""
188+
if "options" in self.interaction.data:
189+
return self.interaction.data["options"]
190+
return None
191+
192+
@property
193+
def unselected_options(self) -> Optional[List[Option]]:
194+
"""The options that were not provided by the user when sending the command.
195+
196+
Returns
197+
-------
198+
Optional[List[:class:`.Option`]]
199+
A list of Option objects (if any) that were not selected by the user when the command was processed.
200+
"""
201+
if self.command.options is not None: # type: ignore
202+
return [
203+
option
204+
for option in self.command.options # type: ignore
205+
if option.to_dict()["name"] not in [opt["name"] for opt in self.selected_options]
206+
]
207+
return None
208+
179209
@property
180210
def respond(self) -> Callable[..., Awaitable[Union[Interaction, WebhookMessage]]]:
181211
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response

0 commit comments

Comments
 (0)