From 45860ca2a6ea0fc11a3b33bdc4b4b696e159166c Mon Sep 17 00:00:00 2001 From: zephyrkul Date: Mon, 6 Jan 2020 14:49:08 -0700 Subject: [PATCH] [CustomCom] Use humanize_list for iterable arguments (#3277) * [cc] use humanize_list on lists * [cc] need classmethod * add changelog --- changelog.d/customcom/3277.enhance.rst | 1 + redbot/cogs/customcom/customcom.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 changelog.d/customcom/3277.enhance.rst diff --git a/changelog.d/customcom/3277.enhance.rst b/changelog.d/customcom/3277.enhance.rst new file mode 100644 index 000000000..13da0713f --- /dev/null +++ b/changelog.d/customcom/3277.enhance.rst @@ -0,0 +1 @@ +Use humanize_list utility for iterable parameter results, e.g. :code:`{#:Role.members}`. diff --git a/redbot/cogs/customcom/customcom.py b/redbot/cogs/customcom/customcom.py index fb384db12..5b163f046 100644 --- a/redbot/cogs/customcom/customcom.py +++ b/redbot/cogs/customcom/customcom.py @@ -11,7 +11,7 @@ import discord from redbot.core import Config, checks, commands from redbot.core.i18n import Translator, cog_i18n from redbot.core.utils import menus -from redbot.core.utils.chat_formatting import box, pagify, escape +from redbot.core.utils.chat_formatting import box, pagify, escape, humanize_list from redbot.core.utils.predicates import MessagePredicate _ = Translator("CustomCommands", __file__) @@ -604,16 +604,25 @@ class CustomCommands(commands.Cog): # only update cooldowns if the command isn't on cooldown self.cooldowns.update(new_cooldowns) - @staticmethod - def transform_arg(result, attr, obj) -> str: + @classmethod + def transform_arg(cls, result, attr, obj) -> str: attr = attr[1:] # strip initial dot if not attr: - return str(obj) + return cls.maybe_humanize_list(obj) raw_result = "{" + result + "}" # forbid private members and nested attr lookups if attr.startswith("_") or "." in attr: return raw_result - return str(getattr(obj, attr, raw_result)) + return cls.maybe_humanize_list(getattr(obj, attr, raw_result)) + + @staticmethod + def maybe_humanize_list(thing) -> str: + if isinstance(thing, str): + return thing + try: + return humanize_list(list(map(str, thing))) + except TypeError: + return str(thing) @staticmethod def transform_parameter(result, message) -> str: