async def messagechain_to_img( message: MessageChain, max_width: int = 1080, font_size: int = 40, spacing: int = 15, padding_x: int = 20, padding_y: int = 15, img_fixed: bool = False, font_path: str = "./simhei.ttf", save_path: str = "./temp/tempMessageChainToImg.png") -> MessageChain: """ 将 MessageChain 转换为图片,仅支持只含有本地图片/文本的 MessageChain Args: message: 要转换的MessageChain max_width: 最大长度 font_size: 字体尺寸 spacing: 行间距 padding_x: x轴距离边框大小 padding_y: y轴距离边框大小 img_fixed: 图片是否适应大小(仅适用于图片小于最大长度时) font_path: 字体文件路径 save_path: 图片存储路径 Examples: msg = await messagechain_to_img(message=message) Returns: MessageChain (内含图片Image类) """ if not os.path.exists("temp"): os.mkdir("temp") font = ImageFont.truetype(font_path, font_size, encoding="utf-8") message = message.asMerged() elements = message.__root__ plains = message.get(Plain) text_gather = "\n".join([plain.text for plain in plains]) # print(max(font.getsize(text)[0] for text in text_gather.split("\n")) + 2 * padding_x) final_width = min( max(font.getsize(text)[0] for text in text_gather.split("\n")) + 2 * padding_x, max_width) text_width = final_width - 2 * padding_x text_height = (font_size + spacing) * await get_final_text_lines( text_gather, text_width, font) img_height_sum = 0 temp_img_list = [] images = message.get(Image_LocalFile) for image in images: if isinstance(image, Image_LocalFile): temp_img = IMG.open(image.filepath) img_width, img_height = temp_img.size temp_img_list.append( temp_img := temp_img.resize(( int(final_width - 2 * spacing), int( float(img_height * (final_width - 2 * spacing)) / float(img_width)))) if img_width > final_width - 2 * spacing or (img_fixed and img_width < final_width - 2 * spacing) else temp_img) img_height_sum = img_height_sum + temp_img.size[1] # elif isinstance(image, Image_UnsafeBytes): # temp_img = IMG.open(BytesIO(image.image_bytes)) else: raise Exception("messagechain_to_img:仅支持本地图片即Image_LocalFile类的处理!") final_height = 2 * padding_y + text_height + img_height_sum picture = IMG.new('RGB', (final_width, final_height), (255, 255, 255)) draw = ImageDraw.Draw(picture) present_x = padding_x present_y = padding_y image_index = 0 for element in elements: if isinstance(element, Image_LocalFile): # print(f"adding img {image_index}") picture.paste(temp_img_list[image_index], (present_x, present_y)) present_y += (spacing + temp_img_list[image_index].size[1]) image_index += 1 elif isinstance(element, Plain): # print(f"adding text '{element.text}'") for char in element.text: if char == "\n": present_y += (font_size + spacing) present_x = padding_x continue if char == "\r": continue if present_x + font.getsize(char)[0] > text_width: present_y += (font_size + spacing) present_x = padding_x draw.text((present_x, present_y), char, font=font, fill=(0, 0, 0)) present_x += font.getsize(char)[0] present_y += (font_size + spacing) present_x = padding_x picture.save(save_path) print(f"process finished! Image saved at {save_path}") return MessageChain.create([Image.fromLocalFile(save_path)])
async def group_message_process(message: MessageChain, message_info: GroupMessage, app: GraiaMiraiApplication) -> list: """ Process the received message and return the corresponding message Args: message: Received message(MessageChain) message_info: Received message(GroupMessage) app: APP Examples: message_list = await message_process(message, message_info) Return: [ str: Auxiliary treatment to be done(Such as add statement), MessageChain: Message to be send(MessageChain) ] """ message_text = message.asDisplay() message_serialization = message.asSerializationString() sender = message_info.sender.id group_id = message_info.sender.group.id # print("message_serialization:", message_serialization) if message.has(At) and message.get(At)[0].target == await get_config( "BotQQ"): await update_user_called_data(group_id, sender, "at", 1) if message.has(At) and message.get(At)[0].target == await get_config( "BotQQ") and re.search("@.* setting.*", message_text): try: _, config, new_value = message_text.split(".") return await setting_process(group_id, sender, config, new_value) except ValueError: return [ "None", MessageChain.create([Plain(text="Command Error!")]) ] """ 图片功能: setu real bizhi time search yellow predict lsp rank """ if message_text in response_set["setu"]: if await get_setting(group_id, "setu"): if sender == 80000000: return [ "None", MessageChain.create([Plain(text="要涩图就光明正大!匿名算什么好汉!")]) ] await update_dragon_data(group_id, sender, "normal") await update_user_called_data(group_id, sender, "setu", 1) if await get_setting(group_id, "r18"): return await get_pic("setu18", group_id, sender) else: return await get_pic("setu", group_id, sender) else: return [ "None", MessageChain.create([Plain(text="我们是正规群呐,不搞那一套哦,想看去辣种群看哟~")]) ] elif re.search("来点.*[色涩]图", message_text): if await get_setting(group_id, "setu"): if sender == 80000000: return [ "None", MessageChain.create([Plain(text="要涩图就光明正大!匿名算什么好汉!")]) ] keyword = re.findall("来点(.*?)[涩色]图", message_text, re.S)[0] print(keyword) if keyword in ["r18", "R18", "r-18", "R-18"]: return [ "quoteSource", MessageChain.create( [Plain(text="此功能暂时还不支持搜索R18涩图呐~忍忍吧LSP!")]) ] await app.sendGroupMessage( group=group_id, message=MessageChain.create([ Plain( text= f"你要的是{keyword}涩图对叭~等等呐~网很慢的>^<,没有反应不是卡死了就是图发出来被屏蔽了呐~就不要等了呐~" ) ]), quote=message[Source][0]) await update_dragon_data(group_id, sender, "normal") await update_user_called_data(group_id, sender, "setu", 1) return await get_setu_keyword(keyword=keyword) else: return [ "None", MessageChain.create([Plain(text="我们是正规群呐,不搞那一套哦,想看去辣种群看哟~")]) ] elif message_text in response_set["real"]: if await get_setting(group_id, "real"): if sender == 80000000: return [ "None", MessageChain.create([Plain(text="要涩图就光明正大!匿名算什么好汉!")]) ] await update_dragon_data(group_id, sender, "normal") await update_user_called_data(group_id, sender, "real", 1) return await get_pic("real", group_id, sender) else: return [ "None", MessageChain.create([Plain(text="我们是正规群呐,不搞那一套哦,想看去辣种群看哟~")]) ] elif message_text in response_set["realHighq"]: if await get_setting(group_id, "real"): if sender == 80000000: return [ "None", MessageChain.create([Plain(text="要涩图就光明正大!匿名算什么好汉!")]) ] await update_dragon_data(group_id, sender, "normal") await update_user_called_data(group_id, sender, "real", 1) return await get_pic("realHighq", group_id, sender) else: return [ "None", MessageChain.create([Plain(text="我们是正规群呐,不搞那一套哦,想看去辣种群看哟~")]) ] elif message_text in response_set["bizhi"]: if await get_setting(group_id, "bizhi"): if sender == 80000000: return [ "None", MessageChain.create([Plain(text="要涩图就光明正大!匿名算什么好汉!")]) ] await update_user_called_data(group_id, sender, "bizhi", 1) return await get_pic("bizhi", group_id, sender) else: return [ "None", MessageChain.create([Plain(text="壁纸功能关闭了呐~想要打开的话就联系管理员吧~")]) ] elif message_text.startswith("setu*") or message_text.startswith( "real*") or message_text.startswith("bizhi*"): if message_text.startswith("bizhi*"): command = "bizhi" num = message_text[6:] else: command = message_text[:4] num = message_text[5:] if num.isdigit(): num = int(num) if sender not in await get_admin(group_id): if 0 <= num <= 5: return [ "None", MessageChain.create([ Plain(text="只有主人和管理员可以使用%s*num命令哦~你没有权限的呐~" % command) ]) ] elif num < 0: return [ "None", MessageChain.create( [Plain(text="%d?你有问题?不如给爷吐出%d张来" % (num, -num))]) ] else: return [ "None", MessageChain.create( [Plain(text="不是管理员你要你🐎呢?老色批!还要那么多?给你🐎一拳,给爷爬!")]) ] if num < 0: return [ "None", MessageChain.create( [Plain(text="%d?你有问题?不如给爷吐出%d张来" % (num, -num))]) ] elif num > 5: if sender == await get_config("HostQQ"): return ["%s*" % command, num] else: return [ "None", MessageChain.create( [Plain(text="管理最多也只能要5张呐~我可不会被轻易玩儿坏呢!!!!")]) ] else: if sender != await get_config("HostQQ"): await update_user_called_data(group_id, sender, command, num) return ["%s*" % command, int(num)] else: return ["None", MessageChain.create([Plain(text="必须为数字!")])] elif message_text == "几点了": return await get_wallpaper_time(group_id, sender) elif message_text.startswith("选择表盘"): if message_text == "选择表盘": return await show_clock_wallpaper(sender) elif message_text == "搜图": if await get_setting(group_id, "search"): await set_get_img_ready(group_id, sender, True, "searchReady") return [ "None", MessageChain.create( [At(sender), Plain(text="请发送要搜索的图片呐~(仅支持pixiv图片搜索呐!)")]) ] else: return [ "None", MessageChain.create( [At(sender), Plain(text="搜图功能关闭了呐~想要打开就联系管理员吧~")]) ] elif message.has(Image) and await get_setting( group_id, "search") and await get_image_ready( group_id, sender, "searchReady"): # print("status:", await get_image_ready(group_id, sender, "searchReady")) image = message.get(Image)[0] await update_user_called_data(group_id, sender, "search", 1) return await search_image(group_id, sender, image) elif message_text == "这张图涩吗": if await get_setting(group_id, "yellowPredict"): await set_get_img_ready(group_id, sender, True, "yellowPredictReady") return [ "None", MessageChain.create( [At(target=sender), Plain(text="请发送要预测的图片呐~")]) ] else: return [ "None", MessageChain.create([ At(target=sender), Plain(text="图片涩度评价功能关闭了呐~想要打开就联系机器人管理员吧~") ]) ] elif message.has(Image) and await get_setting( group_id, "yellowPredict") and await get_image_ready( group_id, sender, "yellowPredictReady"): image = message.get(Image)[0] await update_user_called_data(group_id, sender, "yellowPredict", 1) return await image_yellow_judge(group_id, sender, image, "yellowPredict") elif message_text == "搜番": if await get_setting(group_id, "searchBangumi"): await set_get_img_ready(group_id, sender, True, "searchBangumiReady") return [ "None", MessageChain.create( [At(sender), Plain(text="请发送要搜索的图片呐~(仅支持番剧图片搜索呐!)")]) ] else: return [ "None", MessageChain.create( [At(sender), Plain(text="搜番功能关闭了呐~想要打开就联系管理员吧~")]) ] elif message.has(Image) and await get_setting( group_id, "searchBangumi") and await get_image_ready( group_id, sender, "searchBangumiReady"): # print("status:", await get_image_ready(group_id, sender, "searchReady")) image = message.get(Image)[0] await update_user_called_data(group_id, sender, "search", 1) return await search_bangumi(group_id, sender, image.url) elif message_text == "rank": return await get_rank(group_id, app) # 爬虫相关功能 """ SAGIRI API相关功能: 历史上的今天 """ if message_text == "历史上的今天": return await get_history_today() """ 微博相关功能: 微博热搜 """ if message_text == "weibo" or message_text == "微博": # return [ # "None", # MessageChain.create([ # Plain(text="本功能已停用,短时间内不再开放!请勿多次申请") # ]) # ] return await get_weibo_hot() """ B站相关功能: B站新番时间表 B站直播间查询 """ if message_text[-4:] == "日内新番": num = message_text[:-4] if not num.isdigit() or int(num) <= 0 or int(num) > 7: return [At(target=sender), Plain(text="参数错误!必须为数字1-7!")] else: return await formatted_output_bangumi(int(num)) """ 力扣相关功能: 用户信息查询 每日一题查询 具体题目查询 """ if message_text.startswith("leetcode "): return await get_leetcode_statics(message_text.replace( "leetcode ", "")) """ steam相关功能: steam游戏查询 """ if message_text.startswith("steam "): return await get_steam_game_search(message_text.replace("steam ", "")) """ bangumi相关功能: 番剧查询 """ if message_text.startswith("番剧 "): keyword = message_text[3:] return await get_bangumi_info(sender, keyword) """ 其他功能: 文本翻译 点歌 机器人帮助 自动回复 笑话 群语录 平安经(群人数过多时慎用) pornhub风格图片生成 摸~ """ if message.has(At) and message.get(At)[0].target == await get_config( "BotQQ") and re.search(".*用.*怎么说", message_text): return await get_translate(message_text, sender) elif message_text.startswith("点歌 ") and len(message_text) >= 4: print("search song:", message_text[3:]) return await get_song_ordered(message_text[3:]) if message_text == "help" or message_text == "!help" or message_text == "/help" or message_text == "!help": return [ "None", MessageChain.create([ Plain( text= "点击链接查看帮助:http://doc.sagiri-web.com/web/#/p/c79d523043f6ec05c1ac1416885477c7\n" ), Plain(text="文档尚未完善,功能说明还在陆续增加中!") ]) ] if message_text == "教务通知": return await get_jlu_csw_notice() if re.search("来点.*笑话", message_text): joke_dict = { "苏联": "soviet", "法国": "french", "法兰西": "french", "美国": "america", "美利坚": "america" } name = re.findall(r'来点(.*?)笑话', message_text, re.S) if name == ['']: return [ "None", MessageChain.create( [At(target=sender), Plain(text="来点儿啥笑话啊,你又不告诉人家!哼!")]) ] elif name[0] in joke_dict.keys(): msg = await get_key_joke(joke_dict[name[0]]) await write_log("joke", "none", sender, group_id, True, "function") return msg else: msg = await get_joke(name[0]) await write_log("joke", "none", sender, group_id, True, "function") return msg if message_text == "群语录": return await get_group_quotes(group_id, app, "None", "random", "None") elif re.search("来点.*语录", message_text): name = re.findall(r'来点(.*?)语录', message_text, re.S)[0] at_obj = message.get(At) if name == [] and at_obj == []: return ["None"] elif at_obj: at_str = at_obj[0].asSerializationString() member_id = re.findall(r'\[mirai:at:(.*?),@.*?\]', at_str, re.S)[0] await write_log("quotes", "None", sender, group_id, True, "function") if message_text[-4:] == ".all": return await get_group_quotes(group_id, app, member_id, "all", "memberId") else: return await get_group_quotes(group_id, app, member_id, "select", "memberId") elif name: await write_log("quotes", "None", sender, group_id, True, "function") if message_text[-4:] == ".all": return await get_group_quotes(group_id, app, name, "all", "nickname") else: return await get_group_quotes(group_id, app, name, "select", "nickname") if message_text == "平安": member_list = await app.memberList(group_id) msg = list() msg.append(Plain(text=f"群{message_info.sender.group.name}平安经\n")) for i in member_list: msg.append(Plain(text=f"{i.name}平安\n")) return ["None", MessageChain.create(msg)] if message_text.startswith("ph ") and len(message_text.split(" ")) == 3: if "\\" in message_text or "/" in message_text: return [ "None", MessageChain.create([Plain(text="不支持 '/' 与 '\\' !")]) ] args = message_text.split(" ") left_text = args[1] right_text = args[2] path = f'./statics/temp/ph_{left_text}_{right_text}.png' if not os.path.exists(path): try: await make_ph_style_logo(left_text, right_text) except OSError as e: if "[Errno 22] Invalid argument:" in str(e): return [ "quoteSource", MessageChain.create([Plain(text="非法字符!")]) ] return ["None", MessageChain.create([Image.fromLocalFile(path)])] if message.has(At) and message_text.startswith( "摸") or message_text.startswith("摸 "): target_id = message.get(At)[0].target await petpet(target_id) return [ "None", MessageChain.create([ Image.fromLocalFile( f'./statics/temp/tempPetPet-{target_id}.gif') ]) ] if message.has(At) and message.get(At)[0].target == await get_config( "BotQQ"): return await reply_process(group_id, sender, message_text) return ["None"]