From 2ee5d245406997e171d6694cd0f4de5d49423605 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 15 Dec 2021 20:20:48 -0500 Subject: fix: subcommands inherit their parent's whitelist solves issue with adding decorator to the parent which wouldn't apply to the children --- bot/utils/decorators.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'bot/utils/decorators.py') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 7a3b14ad..3688327a 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -199,13 +199,29 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo kwargs = default_kwargs.copy() allow_dms = False - # Update kwargs based on override - if hasattr(ctx.command.callback, "override"): + # determine which command's overrides we will use + # as we have groups, we want to ensure that group commands inherit from the parent + overridden_command: Union[commands.Command, commands.Group] = None + for command in [ctx.command, *ctx.command.parents]: + print(command) + if hasattr(command.callback, "override"): + overridden_command = command + break + if overridden_command is not None: + log.debug(f'Command {overridden_command} has overrides') + if overridden_command is not ctx.command: + log.debug( + f"Command '{ctx.command.qualified_name}' inherited overrides " + "from parent command '{overridden_command.qualified_name}'" + ) + + # Update kwargs based on override, if one exists + if overridden_command and hasattr(overridden_command.callback, "override"): # Handle DM invocations - allow_dms = ctx.command.callback.override_dm + allow_dms = overridden_command.callback.override_dm # Remove default kwargs if reset is True - if ctx.command.callback.override_reset: + if overridden_command.callback.override_reset: kwargs = {} log.debug( f"{ctx.author} called the '{ctx.command.name}' command and " @@ -213,9 +229,9 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo ) # Merge overwrites and defaults - for arg in ctx.command.callback.override: + for arg in overridden_command.callback.override: default_value = kwargs.get(arg) - new_value = ctx.command.callback.override[arg] + new_value = overridden_command.callback.override[arg] # Skip values that don't need merging, or can't be merged if default_value is None or isinstance(arg, int): -- cgit v1.2.3 From fb5f3f7aababbfbc6991f5b8aac4c6037c8760d7 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 12 Jan 2022 06:35:38 -0500 Subject: minor: remove print debugging statement --- bot/utils/decorators.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot/utils/decorators.py') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 3688327a..ed03c054 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -203,7 +203,6 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo # as we have groups, we want to ensure that group commands inherit from the parent overridden_command: Union[commands.Command, commands.Group] = None for command in [ctx.command, *ctx.command.parents]: - print(command) if hasattr(command.callback, "override"): overridden_command = command break -- cgit v1.2.3 From 065624e13dc4d08f97ba09adb637085f1a1a05a2 Mon Sep 17 00:00:00 2001 From: arl Date: Sat, 9 Jul 2022 16:05:03 -0400 Subject: Apply suggestions from code review --- bot/utils/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/utils/decorators.py') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index ed03c054..f39b52e2 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -201,7 +201,7 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo # determine which command's overrides we will use # as we have groups, we want to ensure that group commands inherit from the parent - overridden_command: Union[commands.Command, commands.Group] = None + overridden_command: Optional[commands.Command] = None for command in [ctx.command, *ctx.command.parents]: if hasattr(command.callback, "override"): overridden_command = command -- cgit v1.2.3 From d8fe28eb20a2f36041021b7756dff32b78b9ae8f Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 17 Sep 2022 12:59:57 +0100 Subject: Split comment over lines evenly --- bot/utils/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/utils/decorators.py') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index f39b52e2..7ad9d22c 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -199,8 +199,8 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo kwargs = default_kwargs.copy() allow_dms = False - # determine which command's overrides we will use - # as we have groups, we want to ensure that group commands inherit from the parent + # Determine which command's overrides we will use. Group commands will + # inherit from their parents if they don't define their own overrides overridden_command: Optional[commands.Command] = None for command in [ctx.command, *ctx.command.parents]: if hasattr(command.callback, "override"): -- cgit v1.2.3 From 6199fd9d83940aaeee9cc243b4f98732f66734ea Mon Sep 17 00:00:00 2001 From: wookie184 Date: Sat, 17 Sep 2022 13:00:10 +0100 Subject: Remove unnecessary hasattr check --- bot/utils/decorators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/utils/decorators.py') diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 7ad9d22c..0061abd9 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -215,7 +215,7 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo ) # Update kwargs based on override, if one exists - if overridden_command and hasattr(overridden_command.callback, "override"): + if overridden_command: # Handle DM invocations allow_dms = overridden_command.callback.override_dm -- cgit v1.2.3