Skip to content

Commit 5a30d49

Browse files
committed
add selected_options and unselected_options properties to ApplicationContext for use in event handling
1 parent 7fbd75f commit 5a30d49

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

discord/commands/context.py

Lines changed: 21 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
@@ -172,6 +172,26 @@ def voice_client(self) -> Optional[VoiceProtocol]:
172172
def response(self) -> InteractionResponse:
173173
return self.interaction.response
174174

175+
@property
176+
def selected_options(self) -> Optional[List[Dict]]:
177+
"""Returns a dictionary containing the options and values that were selected by the user, if applicable."""
178+
if "options" in self.interaction.data:
179+
return self.interaction.data["options"]
180+
return None
181+
182+
@property
183+
def unselected_options(self) -> Optional[List[Dict]]:
184+
"""Returns a dictionary containing the options that were not selected by the user, if applicable."""
185+
if self.command.options is not None: # type: ignore
186+
selected = [opt["name"] for opt in self.selected_options]
187+
return [
188+
{"name": option.to_dict()["name"]}
189+
for option in self.command.options # type: ignore
190+
if option.to_dict()["name"] not in selected
191+
]
192+
193+
return None
194+
175195
@property
176196
def respond(self) -> Callable[..., Awaitable[Union[Interaction, WebhookMessage]]]:
177197
"""Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response

0 commit comments

Comments
 (0)