From 2196037bd355aa23c4a63d47a4bae16783672060 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 24 Feb 2025 16:32:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=994K=E7=BE=A4=E5=8A=A0=E5=85=A5=20#?= =?UTF-8?q?=E5=9B=BE=E6=9D=A5=20=E6=8C=87=E4=BB=A4=E3=80=82=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E8=A6=81=E8=89=B2=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robot.py | 9 +++++++++ xiuren/random_pic.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 xiuren/random_pic.py diff --git a/robot.py b/robot.py index ddeb027..ef08493 100644 --- a/robot.py +++ b/robot.py @@ -40,6 +40,7 @@ from message_report.write_db import write_to_db, generate_and_send_ranking from message_storage.message_to_db import archive_message, get_messages from message_summary.message_summary_4o import message_summary from sehuatang.shehuatang import pdf_file_path +from xiuren.random_pic import get_xiuren_pic from xiuren.xiuren_dl import xiuren_dowload_pic @@ -170,6 +171,14 @@ class Robot(Job): elif q == '#总结': self.message_summary_robot((msg.roomid if msg.from_group() else msg.sender)) return True + # 暂时只支持4K群要图 + elif q == "#图来" and msg.roomid == "45317011307@chatroom": + try: + file_path = get_xiuren_pic() + self.wcf.send_file(file_path, msg.roomid) + except Exception as e: + self.LOG.error(f"图来发图出错:{e}") + return True # 如果正则匹配到时加群指令,则从库中提取第一个群ID elif match: try: diff --git a/xiuren/random_pic.py b/xiuren/random_pic.py new file mode 100644 index 0000000..cf1bd83 --- /dev/null +++ b/xiuren/random_pic.py @@ -0,0 +1,25 @@ +import os +import random + + +def get_random_file_from_dir(directory): + selected_file = None + file_count = 0 + + # 遍历目录及其所有子目录 + for root, dirs, files in os.walk(directory): + for file in files: + # 计算当前文件被选中的概率 + file_count += 1 + if random.randint(1, file_count) == 1: + # 每个文件有1/file_count的概率被选中 + selected_file = os.path.join(root, file) + + return selected_file + + +def get_xiuren_pic(): + # 使用示例 + directory = '.' # 替换为你的目录路径 + random_file_path = get_random_file_from_dir(directory) + return os.path.abspath(random_file_path)