mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[CustomCom] Use humanize_list for iterable arguments (#3277)
* [cc] use humanize_list on lists * [cc] need classmethod * add changelog
This commit is contained in:
parent
d3c97eedfe
commit
45860ca2a6
1
changelog.d/customcom/3277.enhance.rst
Normal file
1
changelog.d/customcom/3277.enhance.rst
Normal file
@ -0,0 +1 @@
|
||||
Use humanize_list utility for iterable parameter results, e.g. :code:`{#:Role.members}`.
|
||||
@ -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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user