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)
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)