示例#1
0
async def help(session: CommandSession):
    """help: 帮助

用法:
help [cmd]

示例:
help
help progress
"""
    group_id = session.ctx['group_id']

    argv = session.args['argv']

    commands = get_all_commands()
    lines = [get_random_header()]
    try:
        if not argv:
            # global help
            at_str = "@闹闹 "
            for name, cmd in commands.items():
                if cmd.permission == GROUP:
                    docstr = cmd.func.__doc__
                    brief = docstr.split('\n')[0] if docstr else name

                    line = at_str if cmd.only_to_me else ""
                    line += brief
                    lines.append(line)

            for msg in multiline_msg_generator(lines=lines, lineno=False):
                await session.bot.send_msg_rate_limited(group_id=group_id,
                                                        message=msg)

            return

        cmd = argv[0]
        usage = commands[cmd].func.__doc__

        if cmd == "register":
            # render docstring dynamically
            examples = [
                f"{oj}: {url.format('<id>')}"
                for oj, url in PLATFORM_URLS.items()
            ]
            random.shuffle(examples)

            usage %= "\n".join(examples)

        lines += usage.split('\n')
        for msg in multiline_msg_generator(lines=lines, lineno=False):
            await session.bot.send_msg_rate_limited(group_id=group_id,
                                                    message=msg)

    except:
        await session.finish(f"命令{cmd}不存在或文档未定义!@Nuulll 甩锅")
示例#2
0
async def send_repo_info(session: CommandSession):
    """repo: 查看闹闹机器人开源仓库

用法:
repo
"""
    lines = [get_random_header()]

    lines += [
        'GITHUB: https://github.com/Nuullll/nullbot',
        '欢迎大家follow, star, watch三连'
    ]

    for msg in multiline_msg_generator(lines=lines, lineno=False):
        await session.send(msg)
示例#3
0
async def handle_blog(session: CommandSession):
    """blog: 查询本群公开博客列表/绑定博客/解绑博客

用法:
blog    // 查询博客列表
blog add <your blog url>
blog remove <your blog url> [-a]

示例:
blog add https://blog.nuullll.com
blog remove https://blog.nuullll.com
blog remove -a  // 解绑本人所有博客
"""
    argv = session.args['argv']

    group_id = session.event.group_id
    qq_id = session.event.user_id
    dm = DataManager(group_id)

    try:
        op = argv[0].lower()
        url = argv[1]
        
        if op == "add":
            if not is_valid_url(url):
                await session.send("闹闹无法获取博客内容,请检查url或网站可达性,或稍后再试")
                return
            
            dm.bind_blog(qq_id, url)
            await session.send("绑定成功!")
            
        elif op == "remove":
            remove_all = url == "-a"
            valid = is_valid_url(url)
            if not valid and not remove_all:
                await session.send("闹闹无法获取博客内容,请检查url或网站可达性,或稍后再试")
                return

            if valid:
                if not dm.unbind_blog(qq_id, url):
                    await session.send("该博客未绑定,请检查url")
                    return

            if remove_all:
                url_map = dm.query_blog(qq_id=qq_id)
                for url in url_map[qq_id]:
                    dm.unbind_blog(qq_id, url)
                
            await session.send("解绑成功!")
        else:
            raise ValueError("Usage error.")

        return
    except:
        pass

    # query all
    lines = [get_random_header()]
    url_map = dm.query_blog()
    for qq_id, blog_urls in url_map.items():
        for url in blog_urls:
            line = f"{render_cq_at(qq_id)} {url}"
            lines.append(line)
    
    for msg in multiline_msg_generator(lines=lines, lineno=False):
        await session.bot.send_msg_rate_limited(group_id=group_id, message=msg)
示例#4
0
async def handle_blog(session: CommandSession):
    """blog: 查询本群公开博客列表/绑定博客/解绑博客

用法:
blog    // 查询博客列表
blog add <your blog url>
blog remove <your blog url> [-a]

示例:
blog add https://blog.nuullll.com
blog remove https://blog.nuullll.com
blog remove -a  // 解绑本人所有博客
"""
    argv = session.args['argv']

    group_id = session.event.group_id
    qq_id = session.event.user_id
    dm = DataManager(group_id)

    try:
        op = argv[0].lower()
        url = argv[1]

        if op == "add":
            if not is_valid_url(url):
                await session.send("闹闹无法获取博客内容,请检查url或网站可达性,或稍后再试")
                return

            if dm.bind_blog(qq_id, url):
                await session.send("绑定成功!")
            else:
                await session.send("该博客你已经绑定过啦!")

        elif op == "remove":
            remove_all = url == "-a"

            if remove_all:
                url_map = dm.query_blog(qq_id=qq_id)
                for url in url_map[qq_id]:
                    dm.unbind_blog(qq_id, url)
            else:
                if not dm.unbind_blog(qq_id, url):
                    await session.send("该博客未绑定,请检查url")
                    return

            await session.send("解绑成功!")
        else:
            raise ValueError("Usage error.")

        return
    except:
        pass

    members = await session.bot.get_group_member_list(group_id=group_id)
    # build qq_id -> card dict
    name_dict = {
        member['user_id']: (member['card'], member['nickname'])
        for member in members
    }

    # query all
    lines = []
    url_map = dm.query_blog()
    for qq_id, blog_urls in url_map.items():
        for url in blog_urls:
            if qq_id not in name_dict:
                line = f"{render_cq_at(qq_id)} {url}"
            else:
                card, nickname = name_dict[qq_id]
                card = f"({card})" if card else ""
                line = f"{nickname}{card} {url}"
            lines.append(line)

    shuffle(lines)
    lines = [get_random_header()] + lines
    for msg in multiline_msg_generator(lines=lines, lineno=False):
        await session.bot.send_msg_rate_limited(group_id=group_id, message=msg)