From 5a30d49ec06d97f09d2d1424f6dd33d34c310e45 Mon Sep 17 00:00:00 2001 From: Krittick Date: Sat, 5 Feb 2022 02:55:52 -0800 Subject: [PATCH 1/5] add `selected_options` and `unselected_options` properties to `ApplicationContext` for use in event handling --- discord/commands/context.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index 576fdb48c8..81d8253083 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -24,7 +24,7 @@ """ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, TypeVar, Union +from typing import TYPE_CHECKING, Optional, TypeVar, Union, Dict, List import discord.abc from discord.interactions import InteractionMessage @@ -172,6 +172,26 @@ def voice_client(self) -> Optional[VoiceProtocol]: def response(self) -> InteractionResponse: return self.interaction.response + @property + def selected_options(self) -> Optional[List[Dict]]: + """Returns a dictionary containing the options and values that were selected by the user, if applicable.""" + if "options" in self.interaction.data: + return self.interaction.data["options"] + return None + + @property + def unselected_options(self) -> Optional[List[Dict]]: + """Returns a dictionary containing the options that were not selected by the user, if applicable.""" + if self.command.options is not None: # type: ignore + selected = [opt["name"] for opt in self.selected_options] + return [ + {"name": option.to_dict()["name"]} + for option in self.command.options # type: ignore + if option.to_dict()["name"] not in selected + ] + + return None + @property def respond(self) -> Callable[..., Awaitable[Union[Interaction, WebhookMessage]]]: """Callable[..., Union[:class:`~.Interaction`, :class:`~.Webhook`]]: Sends either a response From d3345fb25bd5133be6b52cb9bd277f6ca8aea38e Mon Sep 17 00:00:00 2001 From: Krittick Date: Sat, 5 Feb 2022 03:00:54 -0800 Subject: [PATCH 2/5] fix option dict --- discord/commands/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index 81d8253083..952f8e13b5 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -185,7 +185,7 @@ def unselected_options(self) -> Optional[List[Dict]]: if self.command.options is not None: # type: ignore selected = [opt["name"] for opt in self.selected_options] return [ - {"name": option.to_dict()["name"]} + option.to_dict() for option in self.command.options # type: ignore if option.to_dict()["name"] not in selected ] From a8fcb342605437b0f0c7b7e4ec76ad779cec115a Mon Sep 17 00:00:00 2001 From: Krittick Date: Sat, 5 Feb 2022 03:02:50 -0800 Subject: [PATCH 3/5] fix option list --- discord/commands/context.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index 952f8e13b5..e5f660f5c8 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -185,7 +185,7 @@ def unselected_options(self) -> Optional[List[Dict]]: if self.command.options is not None: # type: ignore selected = [opt["name"] for opt in self.selected_options] return [ - option.to_dict() + option for option in self.command.options # type: ignore if option.to_dict()["name"] not in selected ] From b1024cd65bba8695e13289a0cf929cc408d52e0b Mon Sep 17 00:00:00 2001 From: Krittick Date: Sat, 5 Feb 2022 03:12:14 -0800 Subject: [PATCH 4/5] better pythonicness --- discord/commands/context.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index e5f660f5c8..2a89542538 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -174,22 +174,20 @@ def response(self) -> InteractionResponse: @property def selected_options(self) -> Optional[List[Dict]]: - """Returns a dictionary containing the options and values that were selected by the user, if applicable.""" + """Returns a dictionary containing the options and values that were selected by the user when the command was processed, if applicable.""" if "options" in self.interaction.data: return self.interaction.data["options"] return None @property - def unselected_options(self) -> Optional[List[Dict]]: - """Returns a dictionary containing the options that were not selected by the user, if applicable.""" + def unselected_options(self) -> Optional[List[Option]]: + """Returns a list of Option objects (if any) that were not selected by the user when the command was processed.""" if self.command.options is not None: # type: ignore - selected = [opt["name"] for opt in self.selected_options] return [ option for option in self.command.options # type: ignore - if option.to_dict()["name"] not in selected + if option.to_dict()["name"] not in [opt["name"] for opt in self.selected_options] ] - return None @property From 63ceb51c0e66a284845db74430f8f7685db80cc2 Mon Sep 17 00:00:00 2001 From: Krittick Date: Sat, 5 Feb 2022 17:04:19 -0800 Subject: [PATCH 5/5] better docstrings --- discord/commands/context.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/discord/commands/context.py b/discord/commands/context.py index 5d35fbce68..9e36880cc4 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -178,14 +178,26 @@ def response(self) -> InteractionResponse: @property def selected_options(self) -> Optional[List[Dict]]: - """Returns a dictionary containing the options and values that were selected by the user when the command was processed, if applicable.""" + """The options and values that were selected by the user when sending the command. + + Returns + ------- + Optional[List[Dict]] + A dictionary containing the options and values that were selected by the user when the command was processed, if applicable. + """ if "options" in self.interaction.data: return self.interaction.data["options"] return None @property def unselected_options(self) -> Optional[List[Option]]: - """Returns a list of Option objects (if any) that were not selected by the user when the command was processed.""" + """The options that were not provided by the user when sending the command. + + Returns + ------- + Optional[List[:class:`.Option`]] + A list of Option objects (if any) that were not selected by the user when the command was processed. + """ if self.command.options is not None: # type: ignore return [ option