[CustomCom] Use humanize_list for iterable arguments (#3277)

* [cc] use humanize_list on lists

* [cc] need classmethod

* add changelog
This commit is contained in:
zephyrkul 2020-01-06 14:49:08 -07:00 committed by Michael H
parent d3c97eedfe
commit 45860ca2a6
2 changed files with 15 additions and 5 deletions

View File

@ -0,0 +1 @@
Use humanize_list utility for iterable parameter results, e.g. :code:`{#:Role.members}`.

View File

@ -11,7 +11,7 @@ import discord
from redbot.core import Config, checks, commands from redbot.core import Config, checks, commands
from redbot.core.i18n import Translator, cog_i18n from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils import menus 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 from redbot.core.utils.predicates import MessagePredicate
_ = Translator("CustomCommands", __file__) _ = Translator("CustomCommands", __file__)
@ -604,16 +604,25 @@ class CustomCommands(commands.Cog):
# only update cooldowns if the command isn't on cooldown # only update cooldowns if the command isn't on cooldown
self.cooldowns.update(new_cooldowns) self.cooldowns.update(new_cooldowns)
@staticmethod @classmethod
def transform_arg(result, attr, obj) -> str: def transform_arg(cls, result, attr, obj) -> str:
attr = attr[1:] # strip initial dot attr = attr[1:] # strip initial dot
if not attr: if not attr:
return str(obj) return cls.maybe_humanize_list(obj)
raw_result = "{" + result + "}" raw_result = "{" + result + "}"
# forbid private members and nested attr lookups # forbid private members and nested attr lookups
if attr.startswith("_") or "." in attr: if attr.startswith("_") or "." in attr:
return raw_result 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 @staticmethod
def transform_parameter(result, message) -> str: def transform_parameter(result, message) -> str: