示例#1
0
 async def save_image(cls, event: Event) -> None:
     for i, m in enumerate(event.message):
         match = re.match('\[CQ:image.+?\]', str(m))
         if match:
             try:
                 url = re.findall(r'http.*?term=\d', str(m))[0]
                 save_name = re.findall(r'(?<=-)[^-]*?(?=/)', url)[0]
                 image_path = await download_async(url, cls.image_dir,
                                                   save_name)
                 event.message[i] = MessageSegment.image(image_path)
             except Exception as ex:
                 logger.warning(f'保存图片时发生错误{ex}')
     event.raw_message = str(event.message)
示例#2
0
def makeMessageTitle(ctx: Event):
    items = ctx.items()

    _time = time.localtime(ctx.get('time'))
    _time = time.strftime("%Y-%m-%d %H:%M:%S", _time)

    # 获取发送者信息
    sender = ctx.get('sender')
    user_id = sender['user_id']
    nickname = sender['nickname']
    # 获取信息
    result_message = f"{_time}\nQQ名称:{nickname}\nQQ号:{user_id}\n"

    return result_message
示例#3
0
 async def create(bot: NoneBot, event: CQEvent) -> 'SenderRoles':
     """constructor to create a SenderRoles object from an event"""
     # same approach as vanilla permission checker
     sender_info = await _get_member_info(bot, event.self_id,
         event.group_id, event.user_id) \
         if event.get('message_type') == 'group' else None
     return SenderRoles(bot, event, sender_info)
示例#4
0
    async def create(bot: NoneBot, event: CQEvent) -> 'SenderRoles':
        """
        构造 `SenderRoles`。

        参数:
            bot: 接收事件的 NoneBot 对象
            event: 上报事件

        用法:
            ```python
            sender = await SenderRoles.create(session.bot, session.event)
            if sender.is_groupchat:
                if sender.is_owner:
                    await process_owner(session)
                elif sender.is_admin:
                    await process_admin(session)
                else:
                    await process_member(session)
            ```

            根据发送者的身份决定相应命令处理方式。
        """
        # same approach as vanilla permission checker
        sender_info = await _get_member_info(bot, event.self_id,
            event.group_id, event.user_id) \
            if event.get('message_type') == 'group' else None
        return SenderRoles(bot, event, sender_info)
 async def save_image(cls, event: Event, folder=None) -> None:
     if not folder:
         image_path = cls.image_dir
     else:
         image_path = path.join(cls.image_dir, folder)
     if not path.isdir(image_path):
         os.mkdir(image_path)
     for i, m in enumerate(event.message):
         match = re.match('\[CQ:image.+?\]', str(m))
         if match:
             try:
                 url = re.findall(r'http.*?term=\d', str(m))[0]
                 save_name = re.findall(r'(?<=-)[^-]*?(?=/)', url)[0]
                 image = await download_async(url, image_path, save_name)
                 event.message[i] = MessageSegment.image(f'file:///{image}')
             except Exception as ex:
                 print_exc()
     event.raw_message = str(event.message)
async def show_group_reply(bot, event: Event):
    gid = event.get('group_id')
    try:
        page = int(event.raw_message.strip('查看问答'))
    except:
        page = 1
    records = {(m, n) : v for (m, n), v in RECORDS.items() if gid == n}
    reply, pages = show(records,page)
    await bot.send(event,reply.strip()+f'\n\n当前第{page}页,共{pages}页',at_sender=False)
async def show_group_reply(bot, event: Event):
    gid = event.get('group_id')
    uid = event.user_id
    try:
        page = int(event.raw_message.strip('查看我问'))
    except:
        page = 1
    records = {(m, n): v
               for (m, n), v in RECORDS.items()
               if gid == n and uid == RECORDS[(m, n)]['rec_maker']}
    reply, pages = show(records, page)
    await bot.send(event,
                   gfw.filter(reply.strip()) + f'\n\n当前第{page}页,共{pages}页',
                   at_sender=False)
async def add_reply_for_self(bot, event: Event):
    uid = event['user_id']
    gid = event.get('group_id')
    user_priv = get_user_priv(event)
    user_quata = get_user_quota(user_priv)

    # mirai要下载图片
    await R.save_image(event)

    qu = event.raw_message.split('你答')[0].strip('我问').strip()
    ans = event.raw_message.split('你答')[1].strip()
    rec = Record(qu, ans, uid, gid, 0)

    if rec.count_user_records(uid) >= user_quata:
        await bot.send(event, '您的额度不足,请删除记录后再来', at_sender=True)
        return

    if rec.insert_database():
        await bot.send(event, '问答添加成功', at_sender=True)
        if rec.get_records:
            global RECORDS
            RECORDS = rec.get_records()
    else:
        await bot.send(event, '问答添加失败,问答不规范或者冲突', at_sender=True)