utils.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:pcbot 作者: pckv 项目源码 文件源码
def format_objects(*objects, attr=None, dec: str="", sep: str=None):
    """ Return a formatted string of objects (User, Member, Channel or Server) using
    the given decorator and the given separator.

    :param objects: Any object with attributes, preferably User, Member, Channel or Server.
    :param attr: The attribute to get from any object. Defaults to object names.
    :param dec: String to decorate around each object.
    :param sep: Separator between each argument.
    :return: str: the formatted objects.
    """
    if not objects:
        return

    first_object = objects[0]
    if attr is None:
        if isinstance(first_object, discord.User):
            attr = "display_name"
        elif isinstance(first_object, discord.Channel) or isinstance(first_object, discord.Role):
            attr = "mention"
            sep = " "
        elif isinstance(first_object, discord.Server):
            attr = "name"

    sep = sep if sep is not None else ", "

    return sep.join(dec + getattr(m, attr) + dec for m in objects)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号