From fe1879f574abfafeccd08cdde9a3d6f8060828f3 Mon Sep 17 00:00:00 2001 From: hibiki <775757368@qq.com> Date: Wed, 30 Jun 2021 19:50:55 +0800 Subject: [PATCH] update --- configs/config.py | 263 +- models/sigin_group_user.py | 24 +- plugins/aconfig/__init__.py | 4 +- plugins/admin_bot_manage/__init__.py | 10 +- plugins/admin_bot_manage/data_source.py | 10 +- plugins/admin_help/__init__.py | 16 +- plugins/ai/__init__.py | 8 +- plugins/ai/data_source.py | 15 +- plugins/alapi/cover.py | 6 +- plugins/alapi/util.py | 2 +- plugins/ban/__init__.py | 2 +- plugins/broadcast/__init__.py | 4 +- plugins/bt/__init__.py | 4 +- plugins/bt/data_source.py | 4 +- plugins/check/data_source.py | 4 +- plugins/coser/__init__.py | 6 +- plugins/delete_img/__init__.py | 4 +- plugins/dialogue/__init__.py | 6 +- plugins/draw_card/__init__.py | 2 +- plugins/draw_card/announcement.py | 41 +- plugins/draw_card/util.py | 2 +- plugins/epic/__init__.py | 2 +- plugins/epic/data_source.py | 6 +- plugins/fake_msg.py | 4 +- plugins/fudu.py | 6 +- plugins/genshin/almanac/__init__.py | 4 +- plugins/genshin/material_remind/__init__.py | 118 +- .../genshin/qiu_qiu_translation/__init__.py | 2 +- .../genshin/query_resource_points/__init__.py | 2 +- .../query_resource_points/query_resource.py | 2 +- plugins/group_handle/__init__.py | 2 +- plugins/group_level/__init__.py | 24 +- plugins/group_welcome_msg.py | 2 +- plugins/help/__init__.py | 4 +- plugins/help/config.py | 13 +- plugins/help/data_source.py | 16 +- plugins/hook.py | 10 +- plugins/jitang.py | 4 +- plugins/last_chat/data_source.py | 2 +- plugins/luxun/__init__.py | 8 +- plugins/member_activity_handle.py | 214 + plugins/move_img/__init__.py | 2 +- plugins/mute.py | 4 +- plugins/nickname.py | 2 +- plugins/nonebot_plugin_manager/__init__.py | 21 +- plugins/nonebot_plugin_manager/data.py | 8 +- .../nonebot_plugin_picsearcher/__init__.py | 2 +- plugins/nonebot_plugin_picsearcher/ascii2d.py | 6 +- .../nonebot_plugin_picsearcher/saucenao.py | 6 +- plugins/one_friend/__init__.py | 10 +- plugins/open_cases/__init__.py | 2 +- plugins/open_cases/open_cases_c.py | 39 +- plugins/open_cases/utils.py | 6 +- plugins/parse_bilibili_json.py | 6 +- plugins/pid_search.py | 100 + plugins/pixiv/__init__.py | 2 +- plugins/pixiv/data_source.py | 6 +- plugins/poke/__init__.py | 4 +- plugins/quotations.py | 2 +- plugins/reimu/__init__.py | 2 +- plugins/reimu/data_source.py | 4 +- plugins/remind/__init__.py | 4 +- plugins/russian/__init__.py | 13 +- plugins/russian/data_source.py | 2 +- plugins/search_anime/__init__.py | 2 +- plugins/search_anime/data_source.py | 2 +- plugins/search_buff_skin_price/__init__.py | 2 +- plugins/search_buff_skin_price/data_source.py | 4 +- plugins/send_dinggong_voice/__init__.py | 4 +- plugins/send_img/__init__.py | 115 +- plugins/send_setu/__init__.py | 3 +- plugins/send_setu/data_source.py | 11 +- plugins/shop/buy.py | 2 +- plugins/shop/gold.py | 2 +- plugins/shop/gold_redbag/__init__.py | 4 +- plugins/shop/gold_redbag/data_source.py | 6 +- plugins/shop/reset_today_gold.py | 2 +- plugins/shop/shop_handle/__init__.py | 4 +- plugins/shop/shop_handle/data_source.py | 4 +- plugins/shop/use/__init__.py | 2 +- plugins/sign_in/__init__.py | 67 +- plugins/sign_in/group_user_checkin.py | 89 +- plugins/songpicker2/music_163.py | 2 +- plugins/statistics_handle.py | 134 + plugins/super_cmd/__init__.py | 7 +- plugins/super_help/__init__.py | 13 +- plugins/test.py | 53 +- plugins/translate/__init__.py | 2 +- plugins/translate/data_source.py | 4 +- plugins/update_gocqhttp/__init__.py | 2 +- plugins/update_gocqhttp/data_source.py | 4 +- plugins/update_info.py | 2 +- plugins/update_pic.py | 8 +- plugins/update_setu/__init__.py | 2 +- plugins/update_setu/data_source.py | 6 +- plugins/upload_img/__init__.py | 4 +- plugins/weather/__init__.py | 2 +- plugins/weather/data_source.py | 2 +- plugins/what_anime/__init__.py | 4 +- plugins/what_anime/data_source.py | 4 +- plugins/white2black_img.py | 8 +- plugins/yiqing/__init__.py | 2 +- plugins/yiqing/data_source.py | 4 +- utils/browser.py | 59 + utils/data_utils.py | 24 + utils/img_utils.py | 326 + utils/init_result.py | 88 + utils/langconv.py | 274 + utils/user_agent.py | 47 + utils/utils.py | 252 + utils/zh_wiki.py | 8275 +++++++++++++++++ 111 files changed, 10620 insertions(+), 454 deletions(-) create mode 100644 plugins/member_activity_handle.py create mode 100644 plugins/pid_search.py create mode 100644 plugins/statistics_handle.py create mode 100644 utils/browser.py create mode 100644 utils/data_utils.py create mode 100644 utils/img_utils.py create mode 100644 utils/init_result.py create mode 100644 utils/langconv.py create mode 100644 utils/user_agent.py create mode 100644 utils/utils.py create mode 100644 utils/zh_wiki.py diff --git a/configs/config.py b/configs/config.py index 5258e915..4a4eddeb 100644 --- a/configs/config.py +++ b/configs/config.py @@ -12,7 +12,7 @@ USE_CONFIG_FILE = False # API KEY(必要) -LOLICON_KEY: str = "" # lolicon +LOLICON_KEY: str = "" # lolicon(不需要了,可不填) RSSHUBAPP: str = "https://rsshub.app/" # rsshub ALAPI_TOKEN: str = "" # ALAPI # 图灵 @@ -22,7 +22,7 @@ TL_KEY: List[str] = [] # 如果填写了bind就不需要再填写后面的字段了#) # 示例:"bind": "postgresql://user:password@127.0.0.1:5432/database" bind: str = '' -sql_name: str = '' +sql_name: str = 'postgresql' user: str = '' password: str = '' address: str = '' @@ -72,7 +72,7 @@ MUTE_DEFAULT_TIME: int = 7 # 刷屏检测默认规定时间 MUTE_DEFAULT_DURATION: int = 10 # 刷屏检测默禁言时长(分钟) -# 注:即在 MALICIOUS_CHECK_TIME 时间内触发相同命令 MALICIOUS_BAN_COUNT 将被ban MALICIOUS_BAN_TIME 分钟 +# 注:即在 MALICIOUS_CHECK_TIME 时间内触发相同命令 MALICIOUS_BAN_COUNT 将被 ban MALICIOUS_BAN_TIME 分钟 MALICIOUS_BAN_TIME: int = 30 # 恶意命令触发检测触发后ban的时长(分钟) MALICIOUS_BAN_COUNT: int = 8 # 恶意命令触发检测最大触发次数 MALICIOUS_CHECK_TIME: int = 5 # 恶意命令触发检测规定时间内(秒) @@ -84,6 +84,7 @@ UPLOAD_LEVEL: int = 6 # 上传图片权限 BAN_LEVEL: int = 5 # BAN权限 OC_LEVEL: int = 2 # 开关群功能权限 MUTE_LEVEL: int = 5 # 更改禁言设置权限 +MEMBER_ACTIVITY_LEVEL = 5 # 群员活跃检测设置权限 # 需要为哪些群更新最新版gocq吗?(上传最新版gocq) # 示例:[434995955, 239483248] @@ -98,94 +99,6 @@ DOWNLOAD_SETU: bool = True # 是否自动同意好友添加 AUTO_ADD_FRIEND: bool = True - -# 模块与对应命令 -# 用于生成帮助图片 和 开关功能 -plugins2name_dict = { - 'sign_in': ['签到'], - 'send_img': ['发送图片', '萝莉', '美图', '壁纸'], - 'send_setu': ['色图', '涩图', '瑟图', '查色图'], - 'white2black': ['黑白图', '黑白草图'], - 'coser': ['coser', 'cos'], - 'quotations': ['语录'], - 'jitang': ['鸡汤'], - 'send_dinggong_voice': ['骂我', '骂老子', '骂劳资'], - 'open_cases': ['开箱', '我的开箱', '群开箱统计', '我的金色'], - 'luxun': ['鲁迅说过', '鲁迅说'], - 'fake_msg': ['假消息'], - 'buy': ['购买', '购买道具'], - 'my_gold': ['我的金币'], - 'my_props': ['我的道具'], - 'shop_handle': ['商店'], - 'update_pic': ['图片', '操作图片', '修改图片'], - 'search_buff_skin_price': ['查询皮肤'], - 'weather': ['天气', '查询天气', '天气查询'], - 'yiqing': ['疫情', '疫情查询', '查询疫情'], - 'what_anime': ['识番'], - 'search_anime': ['搜番'], - 'songpicker2': ['点歌'], - 'epic': ['epic'], - 'pixiv': ['pixiv', 'p站排行', '搜图'], - 'poke': ['戳一戳', '拍一拍'], - 'draw_card': ['抽卡', '游戏抽卡', '原神抽卡', '方舟抽卡', '坎公骑冠剑抽卡', 'pcr抽卡', 'fgo抽卡', '碧蓝抽卡', '碧蓝航线抽卡', '阴阳师抽卡'], - 'ai': ['ai', 'Ai', 'AI', 'aI'], - 'one_friend': ['我有一个朋友', '我有一个朋友想问问'], - 'translate': ['翻译', '英翻', '翻英', '日翻', '翻日', '韩翻', '翻韩'], - 'nonebot_plugin_picsearcher': ['识图'], - 'almanac': ['原神黄历', '黄历'], - 'material_remind': ['今日素材', '天赋材料'], - 'qiu_qiu_translation': ['丘丘翻译', '丘丘一下', '丘丘语翻译'], - 'query_resource_points': ['原神资源查询', '原神资源列表'], - 'russian': ['俄罗斯轮盘', '俄罗斯转盘', '装弹'], - 'gold_redbag': ['塞红包', '红包', '抢红包'], - 'poetry': ['念诗', '来首诗', '念首诗'], - 'comments_163': ['到点了', '12点了', '网易云热评', '网易云评论'], - 'cover': ['b封面'], -} - -# 功能所需的群权限 -plugins2level_dict = { - 'sign_in': 5, - 'send_img': 5, - 'send_setu': 9, - 'white2black': 5, - 'coser': 9, - 'quotations': 5, - 'jitang': 5, - 'send_dinggong_voice': 5, - 'open_cases': 5, - 'luxun': 5, - 'fake_msg': 5, - 'buy': 5, - 'my_gold': 5, - 'my_props': 5, - 'shop_handle': 5, - 'update_pic': 5, - 'search_buff_skin_price': 5, - 'weather': 5, - 'yiqing': 5, - 'what_anime': 5, - 'search_anime': 5, - 'songpicker2': 5, - 'epic': 5, - 'pixiv': 9, - 'poke': 5, - 'draw_card': 5, - 'ai': 5, - 'one_friend': 5, - 'translate': 5, - 'nonebot_plugin_picsearcher': 5, - 'almanac': 5, - 'material_remind': 5, - 'qiu_qiu_translation': 5, - 'query_resource_points': 5, - 'russian': 5, - 'gold_redbag': 5, - 'poetry': 5, - 'comments_163': 5, - 'cover': 5, -} - # 群管理员功能 与 对应权限 admin_plugins_auth = { 'admin_bot_manage': OC_LEVEL, @@ -195,6 +108,173 @@ admin_plugins_auth = { 'upload_img': UPLOAD_LEVEL, 'admin_help': 1, 'mute': MUTE_LEVEL, + 'member_activity_handle': MEMBER_ACTIVITY_LEVEL +} + + +# 模块与对应命令和对应群权限 +# 用于生成帮助图片 和 开关功能 +plugins2info_dict = { + 'sign_in': { + 'level': 5, + 'cmd': ['签到'] + }, + 'send_img': { + 'level': 5, + 'cmd': ['发送图片', '发图', '萝莉', '美图', '壁纸'] + }, + 'send_setu': { + 'level': 9, + 'cmd': ['色图', '涩图', '瑟图', '查色图'] + }, + 'white2black': { + 'level': 5, + 'cmd': ['黑白图', '黑白草图'] + }, + 'coser': { + 'level': 9, + 'cmd': ['coser', 'cos'] + }, + 'quotations': { + 'level': 5, + 'cmd': ['语录'] + }, + 'jitang': { + 'level': 5, + 'cmd': ['鸡汤'] + }, + 'send_dinggong_voice': { + 'level': 5, + 'cmd': ['骂我', '骂老子', '骂劳资'] + }, + 'open_cases': { + 'level': 5, + 'cmd': ['开箱', '我的开箱', '群开箱统计', '我的金色'] + }, + 'luxun': { + 'level': 5, + 'cmd': ['鲁迅说', '鲁迅说过'] + }, + 'fake_msg': { + 'level': 5, + 'cmd': ['假消息'] + }, + 'buy': { + 'level': 5, + 'cmd': ['购买', '购买道具'] + }, + 'my_gold': { + 'level': 5, + 'cmd': ['我的金币'] + }, + 'my_props': { + 'level': 5, + 'cmd': ['我的道具'] + }, + 'shop_handle': { + 'level': 5, + 'cmd': ['商店'] + }, + 'update_pic': { + 'level': 5, + 'cmd': ['图片', '操作图片', '修改图片'] + }, + 'search_buff_skin_price': { + 'level': 5, + 'cmd': ['查询皮肤'] + }, + 'weather': { + 'level': 5, + 'cmd': ['天气', '查询天气', '天气查询'] + }, + 'yiqing': { + 'level': 5, + 'cmd': ['疫情', '疫情查询', '查询疫情'] + }, + 'what_anime': { + 'level': 5, + 'cmd': ['识番'] + }, + 'search_anime': { + 'level': 5, + 'cmd': ['搜番'] + }, + 'songpicker2': { + 'level': 5, + 'cmd': ['点歌'] + }, + 'epic': { + 'level': 5, + 'cmd': ['epic'] + }, + 'pixiv': { + 'level': 9, + 'cmd': ['pixiv', 'p站排行', '搜图'] + }, + 'poke': { + 'level': 5, + 'cmd': ['戳一戳', '拍一拍'] + }, + 'draw_card': { + 'level': 5, + 'cmd': ['抽卡', '游戏抽卡', '原神抽卡', '方舟抽卡', '坎公骑冠剑抽卡', 'pcr抽卡', 'fgo抽卡', '碧蓝抽卡', '碧蓝航线抽卡', '阴阳师抽卡'] + }, + 'ai': { + 'level': 5, + 'cmd': ['ai', 'Ai', 'AI', 'aI'] + }, + 'one_friend': { + 'level': 5, + 'cmd': ['我有一个朋友', '我有一个朋友想问问'] + }, + 'translate': { + 'level': 5, + 'cmd': ['翻译', '英翻', '翻英', '日翻', '翻日', '韩翻', '翻韩'] + }, + 'nonebot_plugin_picsearcher': { + 'level': 5, + 'cmd': ['识图'] + }, + 'almanac': { + 'level': 5, + 'cmd': ['原神黄历', '黄历'] + }, + 'material_remind': { + 'level': 5, + 'cmd': ['今日素材', '天赋材料'] + }, + 'qiu_qiu_translation': { + 'level': 5, + 'cmd': ['丘丘翻译', '丘丘一下', '丘丘语翻译'] + }, + 'query_resource_points': { + 'level': 5, + 'cmd': ['原神资源查询', '原神资源列表'] + }, + 'russian': { + 'level': 5, + 'cmd': ['俄罗斯轮盘', '俄罗斯转盘', '装弹'] + }, + 'gold_redbag': { + 'level': 5, + 'cmd': ['塞红包', '红包', '抢红包'] + }, + 'poetry': { + 'level': 5, + 'cmd': ['念诗', '来首诗', '念首诗'] + }, + 'comments_163': { + 'level': 5, + 'cmd': ['到点了', '12点了', '网易云热评', '网易云评论'] + }, + 'cover': { + 'level': 5, + 'cmd': ['b封面', 'B封面'] + }, + 'pid_search': { + 'level': 9, + 'cmd': ['p搜', 'P搜'] + } } if TL_M_KEY: @@ -204,7 +284,6 @@ if SYSTEM_PROXY: if ALAPI_M_TOKEN: ALAPI_TOKEN = ALAPI_M_TOKEN - # 配置文件应用 if USE_CONFIG_FILE: config = get_config_data() diff --git a/models/sigin_group_user.py b/models/sigin_group_user.py index 4b659182..8b1df594 100644 --- a/models/sigin_group_user.py +++ b/models/sigin_group_user.py @@ -38,22 +38,16 @@ class SignGroupUser(db.Model): async def query_impression_all(cls, belonging_group: int) -> 'list,list': impression_list = [] user_qq_list = [] - query = cls.query.where( - (cls.belonging_group == belonging_group) - ) + user_group = [] + if belonging_group: + query = cls.query.where( + (cls.belonging_group == belonging_group) + ) + else: + query = cls.query for user in await query.gino.all(): impression_list.append(user.impression) user_qq_list.append(user.user_qq) - return user_qq_list, impression_list + user_group.append(user.belonging_group) + return user_qq_list, impression_list, user_group - @classmethod - async def query_glod_all(cls, belonging_group: int) -> 'list,list': - glod_list = [] - user_qq_list = [] - query = cls.query.where( - (cls.belonging_group == belonging_group) - ) - for user in await query.gino.all(): - glod_list.append(user.glod) - user_qq_list.append(user.user_qq) - return user_qq_list, glod_list diff --git a/plugins/aconfig/__init__.py b/plugins/aconfig/__init__.py index 2c7c81fa..676be9e9 100644 --- a/plugins/aconfig/__init__.py +++ b/plugins/aconfig/__init__.py @@ -1,14 +1,14 @@ import random from nonebot import on_keyword import os -from util.init_result import image +from utils.init_result import image from configs.path_config import IMAGE_PATH from nonebot import on_command from nonebot.rule import to_me from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent from nonebot.adapters.cqhttp.permission import GROUP -from util.utils import FreqLimiter +from utils.utils import FreqLimiter __plugin_name__ = '基本设置 [Hidden]' diff --git a/plugins/admin_bot_manage/__init__.py b/plugins/admin_bot_manage/__init__.py index 30db6692..582aab08 100644 --- a/plugins/admin_bot_manage/__init__.py +++ b/plugins/admin_bot_manage/__init__.py @@ -1,12 +1,12 @@ from nonebot import on_command -from util.utils import get_message_text, get_message_imgs, scheduler, get_bot +from utils.utils import get_message_text, get_message_imgs, scheduler, get_bot from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from .data_source import set_group_status, custom_group_welcome, change_group_switch, \ update_member_info, group_current_status from nonebot.adapters.cqhttp.permission import GROUP from services.log import logger -from configs.config import plugins2name_dict +from configs.config import plugins2info_dict from nonebot.plugin import export __plugin_name__ = '自定义进群欢迎消息' @@ -18,8 +18,8 @@ export = export() export.update_member_info = update_member_info cmds = [] -for cmd_list in plugins2name_dict.values(): - for cmd in cmd_list: +for cmd_list in plugins2info_dict.values(): + for cmd in cmd_list['cmd']: cmds.append(f'开启{cmd}') cmds.append(f'关闭{cmd}') cmds = set(cmds) @@ -35,7 +35,7 @@ group_status = on_command('oc_reminds', aliases={'开启早晚安', '关闭早 '群通知状态'}, permission=GROUP, priority=1, block=True) switch_rule = on_command('switch_rule', aliases=cmds, permission=GROUP, priority=4, block=True) -custom_welcome = on_command('自定义进群欢迎消息', aliases={'自定义欢迎消息', '自定义群欢迎消息'}, permission=GROUP, priority=5, block=True) +custom_welcome = on_command('自定义进群欢迎消息', aliases={'自定义欢迎消息', '自定义群欢迎消息', '设置群欢迎消息'}, permission=GROUP, priority=5, block=True) refresh_member_group = on_command("更新群组成员列表", aliases={"更新群组成员信息"}, permission=GROUP, priority=5, block=True) diff --git a/plugins/admin_bot_manage/data_source.py b/plugins/admin_bot_manage/data_source.py index e91a5908..e0e5d389 100644 --- a/plugins/admin_bot_manage/data_source.py +++ b/plugins/admin_bot_manage/data_source.py @@ -4,11 +4,11 @@ from configs.path_config import DATA_PATH import os import aiofiles import aiohttp -from util.init_result import image -from util.utils import get_local_proxy, get_bot +from utils.init_result import image +from utils.utils import get_local_proxy, get_bot from pathlib import Path from nonebot import require -from configs.config import plugins2name_dict +from configs.config import plugins2info_dict from models.group_member_info import GroupInfoUser import time from datetime import datetime @@ -136,8 +136,8 @@ async def change_group_switch(cmd: str, group_id: int): pass except FileNotFoundError: pass - for plugin_cmd in plugins2name_dict.keys(): - if cmd in plugins2name_dict[plugin_cmd]: + for plugin_cmd in plugins2info_dict.keys(): + if cmd in plugins2info_dict[plugin_cmd]['cmd']: # print(plugin_list[plugin_cmd]) if status == '开启': # if group_id in plugin_list[plugin_cmd]: diff --git a/plugins/admin_help/__init__.py b/plugins/admin_help/__init__.py index 8ad6e868..5b0f73dd 100644 --- a/plugins/admin_help/__init__.py +++ b/plugins/admin_help/__init__.py @@ -2,9 +2,9 @@ from nonebot import on_command from nonebot.typing import T_State from nonebot.adapters import Bot from nonebot.adapters.cqhttp import GroupMessageEvent -from util.img_utils import CreateImg +from utils.img_utils import CreateImg from configs.path_config import IMAGE_PATH -from util.init_result import image +from utils.init_result import image __plugin_name__ = '管理帮助 [Hidden]' @@ -16,10 +16,14 @@ __plugin_usage__ = '''管理帮助(权限等级): 5.将用户拉入真寻黑名单 --> .ban/.unban(5) 6.刷屏禁言相关 --> 指令:刷屏检测设置/设置检测时间 \t\t/设置检测次数/设置禁言时长(5) - 7.上传图片(6) - 8.移动图片(7) - 9.删除图片(7) -对我说 “指令名 帮助” 获取对应详细帮助 + 7.群员活跃度相关 --> 指令:群员活跃检测设置 + 设置群员活跃检测时长(天) + 添加群员活跃检测白名单[at]... + 查看群员活跃检测白名单(5) + 8.上传图片(6) + 9.移动图片(7) + 10.删除图片(7) +对我说 “真寻帮助 指令” 获取对应详细帮助 群主与管理员默认 5 级权限 ''' diff --git a/plugins/ai/__init__.py b/plugins/ai/__init__.py index 510643f4..a957c681 100644 --- a/plugins/ai/__init__.py +++ b/plugins/ai/__init__.py @@ -3,8 +3,8 @@ from services.log import logger from nonebot import on_message from nonebot.rule import to_me from nonebot.typing import T_State -from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent -from util.utils import get_message_text, get_message_imgs +from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent, Message +from utils.utils import get_message_text, get_message_imgs from models.friend_user import FriendUser from models.group_member_info import GroupInfoUser @@ -32,7 +32,7 @@ async def _(bot: Bot, event: PrivateMessageEvent, state: T_State): result = await get_qqbot_chat_result(msg, img, event.user_id, nickname) logger.info(f"USER {event.user_id} 问题:{msg}\n回答:{result}") if result: - await ai.finish(result) + await ai.finish(Message(result)) else: await ai.finish(no_result()) @@ -55,7 +55,7 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): result = await get_qqbot_chat_result(msg, img, event.user_id, nickname) logger.info(f"问题:{msg}\n回答:{result}") if result: - await ai.finish(result) + await ai.finish(Message(result)) else: await ai.finish(no_result()) diff --git a/plugins/ai/data_source.py b/plugins/ai/data_source.py index 208959d3..8e6484ee 100644 --- a/plugins/ai/data_source.py +++ b/plugins/ai/data_source.py @@ -4,8 +4,9 @@ import random import os from configs.path_config import IMAGE_PATH, DATA_PATH from services.log import logger -from util.init_result import image -from util.utils import get_bot +from utils.init_result import image, face +from utils.utils import get_bot +import re try: import ujson as json @@ -100,7 +101,13 @@ async def get_qqbot_chat_result(text: str, img_url: str, user_id: int, user_name content = content.replace('{br}', '\n') if content.find('提示') != -1: content = content[:content.find('提示')] - + while True: + r = re.search('{face:(.*)}', content) + if r: + id_ = r.group(1) + content = content.replace('{' + f'face:{id_}' + '}', str(face(id_))) + else: + break return content if resp_payload['results']: for result in resp_payload['results']: @@ -111,7 +118,7 @@ async def get_qqbot_chat_result(text: str, img_url: str, user_id: int, user_name if len(user_name) < 5: if random.random() < 0.5: user_name = "~".join(user_name) + '~' - if random.random() < 0.5: + if random.random() < 0.2: if user_name.find('大人') == -1: user_name += '大~人~' text = text.replace('小主人', user_name) diff --git a/plugins/alapi/cover.py b/plugins/alapi/cover.py index da66692c..c0ee425d 100644 --- a/plugins/alapi/cover.py +++ b/plugins/alapi/cover.py @@ -2,8 +2,8 @@ from nonebot import on_command from nonebot.adapters.cqhttp import Bot, MessageEvent, Message from nonebot.typing import T_State from configs.config import ALAPI_TOKEN -from util.init_result import image -from util.utils import get_message_text +from utils.init_result import image +from utils.utils import get_message_text from .util import get_data from services.log import logger @@ -12,7 +12,7 @@ __plugin_usage__ = '用法: b封面 (链接,av,bv,cv,直播id)\n\t' \ '示例:b封面 av86863038' -cover = on_command('b封面', priority=5, block=True) +cover = on_command('b封面', aliases={'B封面'}, priority=5, block=True) cover_url = 'https://v2.alapi.cn/api/bilibili/cover' diff --git a/plugins/alapi/util.py b/plugins/alapi/util.py index 1eb4d1bb..a4ae61fb 100644 --- a/plugins/alapi/util.py +++ b/plugins/alapi/util.py @@ -1,5 +1,5 @@ import aiohttp -from util.utils import get_local_proxy +from utils.utils import get_local_proxy async def get_data(url: str, params: dict): diff --git a/plugins/ban/__init__.py b/plugins/ban/__init__.py index 982d228a..1a54ac28 100644 --- a/plugins/ban/__init__.py +++ b/plugins/ban/__init__.py @@ -5,7 +5,7 @@ from nonebot.typing import T_State from nonebot.adapters import Bot from nonebot.adapters.cqhttp import GroupMessageEvent from nonebot.adapters.cqhttp.permission import GROUP -from util.utils import get_message_at, get_message_text, is_number +from utils.utils import get_message_at, get_message_text, is_number from services.log import logger from models.group_member_info import GroupInfoUser diff --git a/plugins/broadcast/__init__.py b/plugins/broadcast/__init__.py index bf01a6c4..5f861bab 100644 --- a/plugins/broadcast/__init__.py +++ b/plugins/broadcast/__init__.py @@ -3,10 +3,10 @@ from nonebot.typing import T_State from nonebot.adapters import Bot, Event from nonebot.permission import SUPERUSER import asyncio -from util.utils import get_message_text, get_message_imgs +from utils.utils import get_message_text, get_message_imgs from services.log import logger from models.group_remind import GroupRemind -from util.init_result import image +from utils.init_result import image __plugin_name__ = "广播 [Hidden]" diff --git a/plugins/bt/__init__.py b/plugins/bt/__init__.py index 18cca9ed..df84b7a6 100644 --- a/plugins/bt/__init__.py +++ b/plugins/bt/__init__.py @@ -4,9 +4,9 @@ from services.log import logger from nonebot.typing import T_State from nonebot.adapters import Bot from nonebot.adapters.cqhttp import PrivateMessageEvent -from util.utils import get_message_text +from utils.utils import get_message_text from nonebot.adapters.cqhttp.permission import PRIVATE -from util.utils import UserExistLimiter +from utils.utils import UserExistLimiter from asyncio.exceptions import TimeoutError from aiohttp.client_exceptions import ServerDisconnectedError diff --git a/plugins/bt/data_source.py b/plugins/bt/data_source.py index 1b99c144..bab3c462 100644 --- a/plugins/bt/data_source.py +++ b/plugins/bt/data_source.py @@ -1,8 +1,8 @@ -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent import aiohttp from configs.config import MAXINFO_BT from bs4 import BeautifulSoup -from util.utils import get_local_proxy +from utils.utils import get_local_proxy import platform if platform.system() == 'Windows': import asyncio diff --git a/plugins/check/data_source.py b/plugins/check/data_source.py index 9911e300..49de9365 100644 --- a/plugins/check/data_source.py +++ b/plugins/check/data_source.py @@ -2,10 +2,10 @@ import psutil import aiohttp import time from datetime import datetime -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from asyncio.exceptions import TimeoutError from aiohttp.client_exceptions import ClientConnectorError -from util.utils import get_local_proxy +from utils.utils import get_local_proxy import asyncio from services.log import logger diff --git a/plugins/coser/__init__.py b/plugins/coser/__init__.py index 9eeaa2c6..cd9a7ece 100644 --- a/plugins/coser/__init__.py +++ b/plugins/coser/__init__.py @@ -1,9 +1,9 @@ from nonebot import on_command -from util.utils import get_message_text +from utils.utils import get_message_text from nonebot.typing import T_State from nonebot.adapters import Bot, Event from services.log import logger -from util.init_result import image +from utils.init_result import image import requests __plugin_name__ = 'coser' @@ -11,7 +11,7 @@ __plugin_name__ = 'coser' __plugin_usage__ = '用法:发送‘coser’' -coser = on_command('cos', aliases={'coser', '括丝'}, priority=5, block=True) +coser = on_command('cos', aliases={'coser', '括丝', 'COS', 'Cos', 'cOS', 'coS'}, priority=5, block=True) url_2 = 'http://api.rosysun.cn/cos' diff --git a/plugins/delete_img/__init__.py b/plugins/delete_img/__init__.py index e3686ae2..9567edd3 100644 --- a/plugins/delete_img/__init__.py +++ b/plugins/delete_img/__init__.py @@ -1,13 +1,13 @@ from configs.path_config import IMAGE_PATH, TEMP_PATH import os -from util.init_result import image +from utils.init_result import image from services.log import logger from nonebot import on_command from nonebot.rule import to_me from nonebot.typing import T_State from nonebot.adapters import Bot, Event from configs.config import IMAGE_DIR_LIST -from util.utils import is_number, cn2py +from utils.utils import is_number, cn2py __plugin_name__ = '删除图片' __plugin_usage__ = '删除图片帮助:\n\t' \ diff --git a/plugins/dialogue/__init__.py b/plugins/dialogue/__init__.py index 5490edfc..c03d8873 100644 --- a/plugins/dialogue/__init__.py +++ b/plugins/dialogue/__init__.py @@ -2,10 +2,10 @@ from nonebot import on_command from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent, Message from nonebot.permission import SUPERUSER -from util.utils import get_message_text, is_number, get_message_imgs -from util.init_result import image +from utils.utils import get_message_text, is_number, get_message_imgs +from utils.init_result import image from services.log import logger -from util.init_result import at +from utils.init_result import at __plugin_name__ = '联系管理员' diff --git a/plugins/draw_card/__init__.py b/plugins/draw_card/__init__.py index b87fb502..99a450f9 100644 --- a/plugins/draw_card/__init__.py +++ b/plugins/draw_card/__init__.py @@ -17,7 +17,7 @@ from .config import PRTS_FLAG, PRETTY_FLAG, GUARDIAN_FLAG, GENSHIN_FLAG, PCR_FLA from .async_update_game_info import async_update_game import re import asyncio -from util.utils import scheduler +from utils.utils import scheduler from services.log import logger __plugin_name__ = '游戏抽卡' diff --git a/plugins/draw_card/announcement.py b/plugins/draw_card/announcement.py index dd049e86..822439d1 100644 --- a/plugins/draw_card/announcement.py +++ b/plugins/draw_card/announcement.py @@ -49,7 +49,7 @@ def check_write(data: dict, up_char_file): else: with open(up_char_file, 'r', encoding='utf8') as f: old_data = json.load(f) - if is_expired(data['char']): + if is_expired(old_data['char']): return old_data else: with open(up_char_file, 'w', encoding='utf8') as f: @@ -239,14 +239,32 @@ class PrettyAnnouncement: context = soup.find('div', {'class': 'mw-parser-output'}) data['char']['title'] = title data['card']['title'] = title - time = str(context.find_all('big')[1].text) + for big in context.find_all('big'): + r = re.search(r'\d{1,2}/\d{1,2} \d{1,2}:\d{1,2}', str(big.text)) + if r: + time = str(big.text) + break + else: + logger.error('赛马娘UP无法找到活动日期....取消更新UP池子...') + return + # raise Exception('赛马娘UP无法找到活动日期....') time = time.replace('~', '-').replace('/', '月').split(' ') time = time[0] + '日 ' + time[1] + ' - ' + time[3] + '日 ' + time[4] data['char']['time'] = time data['card']['time'] = time for p in context.find_all('p'): - if str(p).find('当期UP赛马娘') != -1: - data['char']['pool_img'] = p.find('img')['src'] + if str(p).find('当期UP赛马娘') != -1 and str(p).find('■') != -1: + if not data['char']['pool_img']: + try: + data['char']['pool_img'] = p.find('img')['src'] + except TypeError: + for center in context.find_all('center'): + try: + img = center.find('img') + if img and str(img['alt']).find('新马娘') != -1 and str(img['alt']).find('总览') == 1: + data['char']['pool_img'] = img['src'] + except (TypeError, KeyError): + pass r = re.findall(r'.*?当期UP赛马娘([\s\S]*)<奖励内容>.*?', str(p)) if r: for x in r: @@ -261,8 +279,19 @@ class PrettyAnnouncement: data['char']['up_char']['2'][char_name] = '70' elif star == 1: data['char']['up_char']['1'][char_name] = '70' - if str(p).find('(当期UP对象)') != -1 and str(p).find('赛马娘') == -1: - data['card']['pool_img'] = p.find('img')['src'] + if str(p).find('(当期UP对象)') != -1 and str(p).find('赛马娘') == -1 and str(p).find('■') != -1: + # data['card']['pool_img'] = p.find('img')['src'] + if not data['char']['pool_img']: + try: + data['char']['pool_img'] = p.find('img')['src'] + except TypeError: + for center in context.find_all('center'): + try: + img = center.find('img') + if img and str(img['alt']).find('新卡') != -1 and str(img['alt']).find('总览') == 1: + data['card']['pool_img'] = img['src'] + except (TypeError, KeyError): + pass r = re.search(r'■全?新?支援卡(当期UP对象)([\s\S]*)
', str(p)) if r: rmsg = r.group(1).strip() diff --git a/plugins/draw_card/util.py b/plugins/draw_card/util.py index 662f5e01..7156feb1 100644 --- a/plugins/draw_card/util.py +++ b/plugins/draw_card/util.py @@ -10,7 +10,7 @@ from .config import path_dict from configs.path_config import IMAGE_PATH import nonebot import pypinyin -from util.img_utils import CreateImg +from utils.img_utils import CreateImg import platform from services.log import logger import random diff --git a/plugins/epic/__init__.py b/plugins/epic/__init__.py index d99532a6..dbce0aec 100644 --- a/plugins/epic/__init__.py +++ b/plugins/epic/__init__.py @@ -2,7 +2,7 @@ from nonebot import on_command from services.log import logger from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State -from util.utils import scheduler, get_bot +from utils.utils import scheduler, get_bot from .data_source import get_epic_game from models.group_remind import GroupRemind from nonebot.adapters.cqhttp.exception import ActionFailed diff --git a/plugins/epic/data_source.py b/plugins/epic/data_source.py index 1d2e1021..b9f48eeb 100644 --- a/plugins/epic/data_source.py +++ b/plugins/epic/data_source.py @@ -1,11 +1,11 @@ import aiohttp import aiofiles -from util.utils import get_local_proxy +from utils.utils import get_local_proxy import feedparser import platform -from util.init_result import image +from utils.init_result import image from configs.path_config import IMAGE_PATH -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent if platform.system() == 'Windows': import asyncio asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) diff --git a/plugins/fake_msg.py b/plugins/fake_msg.py index 4453ec2e..8f95deed 100644 --- a/plugins/fake_msg.py +++ b/plugins/fake_msg.py @@ -1,8 +1,8 @@ from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot import on_command -from util.utils import get_message_imgs, get_message_text -from util.init_result import share +from utils.utils import get_message_imgs, get_message_text +from utils.init_result import share from services.log import logger diff --git a/plugins/fudu.py b/plugins/fudu.py index 6ec5f076..95f86e5a 100644 --- a/plugins/fudu.py +++ b/plugins/fudu.py @@ -1,10 +1,10 @@ from nonebot.adapters.cqhttp.permission import GROUP from configs.path_config import IMAGE_PATH -from util.img_utils import get_img_hash +from utils.img_utils import get_img_hash import random -from util.init_result import image +from utils.init_result import image from nonebot import on_message -from util.utils import get_message_text, get_message_imgs, get_local_proxy +from utils.utils import get_message_text, get_message_imgs, get_local_proxy from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent import aiohttp diff --git a/plugins/genshin/almanac/__init__.py b/plugins/genshin/almanac/__init__.py index 8abff492..0d0cfc97 100644 --- a/plugins/genshin/almanac/__init__.py +++ b/plugins/genshin/almanac/__init__.py @@ -1,11 +1,11 @@ from .alc import get_almanac_base64_str, load_data import os -from util.utils import get_bot, scheduler +from utils.utils import get_bot, scheduler from nonebot import on_command from models.level_user import LevelUser from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent -from util.init_result import image +from utils.init_result import image from services.log import logger from models.group_remind import GroupRemind diff --git a/plugins/genshin/material_remind/__init__.py b/plugins/genshin/material_remind/__init__.py index 0abd8bd4..f36ecc4c 100644 --- a/plugins/genshin/material_remind/__init__.py +++ b/plugins/genshin/material_remind/__init__.py @@ -1,72 +1,80 @@ -from nonebot import on_command +from nonebot import on_command, Driver from nonebot.typing import T_State -from nonebot.adapters.cqhttp import Bot, MessageEvent -from util.init_result import image +from nonebot.adapters.cqhttp import Bot, MessageEvent, Message +from utils.init_result import image +from utils.browser import get_browser +from configs.path_config import IMAGE_PATH +import nonebot +from services.log import logger +from utils.utils import scheduler +from nonebot.permission import SUPERUSER +import os import time +driver: Driver = nonebot.get_driver() + material = on_command('今日素材', aliases={'今日材料', '今天素材', '今天材料'}, priority=5, block=True) -role_material = on_command('天赋材料', priority=5, block=True) +super_cmd = on_command('更新原神今日素材', permission=SUPERUSER, priority=1, block=True) -def get_today_material(name: str): - # 返回今天的材料图片CQ码 - if name == '天赋材料': - return image('天赋材料.png', "genshin/material/") - week = time.strftime("%w") - png_name = '' - if week == "0": - return "今天是周日,所有材料副本都开放了。" - elif week in ["1", "4"]: - png_name = f"{name}_周一周四.png" - elif week in ["2", "5"]: - png_name = f"{name}_周二周五.png" - elif week in ["3", "6"]: - png_name = f"{name}_周三周六.png" - - return image(png_name, "genshin/material/") - - -# @sv.on_fullmatch('开启原神每日素材提醒') -# async def open_remind(bot , ev): -# gid = str(ev.group_id) -# if not (gid in group_list): -# group_list.append(gid) -# save_group_list() -# await bot.send(ev, "每日提醒已开启,每天8点会发送今日素材") -# -# -# @sv.on_fullmatch('关闭原神每日素材提醒') -# async def off_remind(bot , ev): -# gid = str(ev.group_id) -# if gid in group_list: -# group_list.remove(gid) -# save_group_list() -# await bot.send(ev, "每日提醒已关闭") @material.handle() async def _(bot: Bot, event: MessageEvent, state: T_State): if time.strftime("%w") == "0": await material.send("今天是周日,所有材料副本都开放了。") return - arms_material_CQ = get_today_material("武器突破材料") - roles_material_CQ = get_today_material("角色天赋材料") - await material.send(arms_material_CQ + roles_material_CQ) + await material.send(Message(image('daily_material.png', 'genshin') + '\n※ 黄历数据来源于 genshin.pub')) + logger.info( + f"(USER {event.user_id}, GROUP {event.group_id if event.message_type != 'private' else 'private'})" + f" 发送查看今日素材") -@role_material.handle() +@super_cmd.handle() async def _(bot: Bot, event: MessageEvent, state: T_State): - await material.send(get_today_material("天赋材料")) + try: + await update_image() + await super_cmd.send('更新成功...') + logger.info(f'更新每日天赋素材成功...') + except Exception as e: + await super_cmd.send(f'更新出错e:{e}') + logger.error(f'更新每日天赋素材出错 e:{e}') + + +@driver.on_startup +async def update_image(): + try: + if os.path.exists(f'{IMAGE_PATH}/genshin/daily_material.png'): + os.remove(f'{IMAGE_PATH}/genshin/daily_material.png') + browser = await get_browser() + url = 'https://genshin.pub/daily' + page = await browser.new_page() + await page.goto(url, wait_until='networkidle', timeout=10000) + await page.set_viewport_size({"width": 2560, "height": 1080}) + await page.click("button") + card = await page.query_selector(".GSContainer_inner_border_box__21_vs") + card = await card.bounding_box() + await page.screenshot(path=f'{IMAGE_PATH}/genshin/daily_material.png', clip=card, timeout=100000) + await page.close() + except Exception: + logger.warning('win环境下 使用playwright失败....请替换环境至linux') + pass + + +@scheduler.scheduled_job( + 'cron', + hour=0, + minute=1, +) +async def _(): + for _ in range(5): + try: + await update_image() + logger.info(f'更新每日天赋素材成功...') + break + except Exception as e: + logger.error(f'更新每日天赋素材出错 e:{e}') + + + -# @sv.scheduled_job('cron', hour='8') -# async def material_remind(): -# # 每日提醒 -# if time.strftime("%w") == "0": -# # 如果今天是周日就不发了 -# return -# bot = get_bot() -# arms_material_CQ = get_today_material("武器突破材料") -# roles_material_CQ = get_today_material("角色天赋材料") -# for gid in group_list: -# await bot.send_group_msg(group_id=int(gid), message=arms_material_CQ) -# await bot.send_group_msg(group_id=int(gid), message=roles_material_CQ) diff --git a/plugins/genshin/qiu_qiu_translation/__init__.py b/plugins/genshin/qiu_qiu_translation/__init__.py index d3bc61d6..fd6585ad 100644 --- a/plugins/genshin/qiu_qiu_translation/__init__.py +++ b/plugins/genshin/qiu_qiu_translation/__init__.py @@ -2,7 +2,7 @@ from .qiu_translation import qiu_qiu_word_translation, qiu_qiu_phrase_translatio from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State from nonebot import on_command -from util.utils import get_message_text +from utils.utils import get_message_text from services.log import logger __plugin_name__ = '丘丘语翻译' diff --git a/plugins/genshin/query_resource_points/__init__.py b/plugins/genshin/query_resource_points/__init__.py index e9893b7a..63e2be49 100644 --- a/plugins/genshin/query_resource_points/__init__.py +++ b/plugins/genshin/query_resource_points/__init__.py @@ -1,7 +1,7 @@ from nonebot import on_command, on_regex from nonebot.rule import to_me from .query_resource import get_resource_map_mes, get_resource_list_mes, up_label_and_point_list -from util.utils import get_message_text, scheduler +from utils.utils import get_message_text, scheduler from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State import os diff --git a/plugins/genshin/query_resource_points/query_resource.py b/plugins/genshin/query_resource_points/query_resource.py index 2c4451ed..51df9484 100644 --- a/plugins/genshin/query_resource_points/query_resource.py +++ b/plugins/genshin/query_resource_points/query_resource.py @@ -6,7 +6,7 @@ import os import time import base64 from configs.path_config import IMAGE_PATH -from util.init_result import image +from utils.init_result import image from services.log import logger import asyncio import nonebot diff --git a/plugins/group_handle/__init__.py b/plugins/group_handle/__init__.py index 4bd25779..c17e068a 100644 --- a/plugins/group_handle/__init__.py +++ b/plugins/group_handle/__init__.py @@ -1,6 +1,6 @@ from nonebot import on_notice, on_request from configs.path_config import IMAGE_PATH, DATA_PATH -from util.init_result import image +from utils.init_result import image import os import random from models.group_member_info import GroupInfoUser diff --git a/plugins/group_level/__init__.py b/plugins/group_level/__init__.py index aead7cca..3e4a9d8a 100644 --- a/plugins/group_level/__init__.py +++ b/plugins/group_level/__init__.py @@ -1,5 +1,5 @@ from nonebot import on_command, on_regex -from util.utils import get_message_text, is_number +from utils.utils import get_message_text, is_number, FreqLimiter from nonebot.rule import to_me from services.log import logger from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent, GROUP @@ -7,7 +7,7 @@ from nonebot.typing import T_State from nonebot.matcher import Matcher from nonebot.permission import SUPERUSER from pathlib import Path -from configs.config import plugins2level_dict, plugins2name_dict +from configs.config import plugins2info_dict from nonebot.message import run_preprocessor, IgnoredException try: import ujson as json @@ -17,6 +17,8 @@ except ModuleNotFoundError: __plugin_name__ = '群权限' __plugin_usage__ = '区分权限功能' +flmt = FreqLimiter(60) + group_level_data = Path() / 'data'/ 'manager' / 'group_level.json' group_level_data.parent.mkdir(exist_ok=True, parents=True) group_data = {} @@ -26,15 +28,19 @@ if group_level_data.exists(): @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State): + if not isinstance(event, MessageEvent): + return if matcher.type == 'message' and matcher.priority not in [1, 9]: if isinstance(event, GroupMessageEvent): if not group_data.get(str(event.group_id)): group_data[str(event.group_id)] = 5 - if plugins2level_dict.get(matcher.module): - if plugins2level_dict[matcher.module] > group_data[str(event.group_id)]: + if plugins2info_dict.get(matcher.module): + if plugins2info_dict[matcher.module]['level'] > group_data[str(event.group_id)]: try: - await bot.send_group_msg(group_id=event.group_id, message='群权限不足...') - except: + if flmt.check(event.group_id): + flmt.start_cd(event.group_id) + await bot.send_group_msg(group_id=event.group_id, message='群权限不足...') + except Exception: pass raise IgnoredException('群权限不足') @@ -72,9 +78,9 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): if group_data.get(str(event.group_id)): level = group_data[str(event.group_id)] tmp = '' - for plugin in plugins2level_dict: - if plugins2level_dict[plugin] > level: - plugin_name = plugins2name_dict[plugin][0] + for plugin in plugins2info_dict: + if plugins2info_dict[plugin]['level'] > level: + plugin_name = plugins2info_dict[plugin]['cmd'][0] if plugin_name == 'pixiv': plugin_name = '搜图 p站排行' tmp += f'{plugin_name}\n' diff --git a/plugins/group_welcome_msg.py b/plugins/group_welcome_msg.py index 23f55937..984eaee7 100644 --- a/plugins/group_welcome_msg.py +++ b/plugins/group_welcome_msg.py @@ -3,7 +3,7 @@ from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.adapters.cqhttp.permission import GROUP from configs.path_config import DATA_PATH -from util.init_result import image +from utils.init_result import image import os from pathlib import Path try: diff --git a/plugins/help/__init__.py b/plugins/help/__init__.py index b5e551de..e69f4455 100644 --- a/plugins/help/__init__.py +++ b/plugins/help/__init__.py @@ -3,11 +3,11 @@ from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent from nonebot.typing import T_State from nonebot.rule import to_me from configs.path_config import DATA_PATH -from util.init_result import image +from utils.init_result import image import os from .data_source import create_help_img, create_group_help_img, get_plugin_help from nonebot import require -from util.utils import get_message_text +from utils.utils import get_message_text export = require("nonebot_plugin_manager") diff --git a/plugins/help/config.py b/plugins/help/config.py index 41ec4918..2c7cdcff 100644 --- a/plugins/help/config.py +++ b/plugins/help/config.py @@ -14,14 +14,15 @@ utility_help = { 'epic': 'epic速速白嫖 --> 指令:epic', 'pixiv_r': 'P站排行榜直接冲 --> 指令:p站排行(可含参数)', 'pixiv_s': 'P站的图随便搜搜 --> 指令:搜图(可含参数)', + 'pid_search': '通过PID搜索图片 --> 指令:p搜 [pid]', 'translate': '出国旅游助手(狗头) --> 指令:英翻/翻英/日翻/翻日/韩翻/翻韩', 'cover': '快捷的b站封面获取方式 --> 指令:b封面 [连接/av/bv/cv/直播id]' } # 娱乐 entertainment_help = { - 'sign_in': '签到(影响色图几率和开箱次数) --> 指令:签到/我的签到/好感度排行', - 'send_img': '发送图片 --> 指令:美图/萝莉/壁纸', - 'send_setu': '不要小看涩图啊混蛋! --> 指令:色图/n张色图/n张xx色图/查色图/...(请查看 色图 帮助)', + 'sign_in': '签到(影响色图和开箱) --> 指令:签到/我的签到/好感度排行/好感度总榜/好感度总榜[屏蔽我/显示我]', + 'send_img': '发送图片 --> 指令:美图/萝莉/壁纸/(美图/萝莉/壁纸[id])/N张图xx(N<=9)', + 'send_setu': '不要小看涩图啊混蛋! --> 指令:色图/色图[id]/n张色图/n张xx色图/查色图/...(请查看 色图 帮助)', 'white2black_img': '黑白草图 --> 指令:黑白图/黑白草图', 'coser': '三次元也不戳 --> 指令:coser', 'jitang': '不喝点什么不舒服 --> 指令:鸡汤/语录', @@ -42,7 +43,7 @@ entertainment_help = { 'one_friend': '我有一个朋友想问问... --> 指令:我有一个朋友想问问xxx(内容)', 'nickname': '区区昵称! --> 指令:以后叫我xx(昵称)/我是谁/取消昵称', 'almanac': '这是一张正经的黄历 --> 指令:原神黄历', - 'material_remind': '看看原神今天要刷什么 --> 指令:今日素材/天赋材料', + 'material_remind': '看看原神今天要刷什么 --> 指令:今日素材/今日材料/今天素材/今天材料', 'qiu_qiu_translation': '这家伙到底在说什么? --> 指令:丘丘翻译/丘丘一下/丘丘语翻译', 'query_resource_points': '地图资源速速查看 --> 指令:原神资源查询xx/原神资源列表/哪里有xx/xx在哪(xx=资源名称)', 'russian': '紧张刺激的俄罗斯轮盘 --> 指令:俄罗斯轮盘帮助', @@ -58,5 +59,7 @@ other_help = [ '有人记得你是什么时候加入我们的 --> 指令:我的信息', '让我看看更新了什么 --> 指令:更新信息', '真寻给我把话收回去! --> 指令:撤回 [id](默认0)', - '群拥有的权限 --> 指令:查看群权限 ' + '群拥有的权限 --> 指令:查看群权限', + '数据统计可视化_1 --> 指令:功能调用统计/日功能调用统计/周功能调用统计/月功能调用统计', + '数据统计可视化_2 --> 指令:周功能调用统计 [功能名称]/月功能调用统计 [功能名称]' ] diff --git a/plugins/help/data_source.py b/plugins/help/data_source.py index a3fc7997..9b14d860 100644 --- a/plugins/help/data_source.py +++ b/plugins/help/data_source.py @@ -1,18 +1,18 @@ -from util.img_utils import CreateImg +from utils.img_utils import CreateImg from configs.path_config import IMAGE_PATH, DATA_PATH import ujson as json import os from .config import * from nonebot import require from configs.config import INITIAL_OPEN_CASE_COUNT, INITIAL_SETU_PROBABILITY, ADMIN_DEFAULT_AUTH -from configs.config import plugins2name_dict +from configs.config import plugins2info_dict import nonebot export = require("nonebot_plugin_manager") width = 1500 e_height = 0 -u_height = 850 +u_height = 950 o_height = 1500 # f_height = @@ -49,7 +49,7 @@ def create_help_img(): A.paste(e, (0, 0)) A.paste(u, (0, u_height)) A.paste(o, (0, o_height)) - A.text((10, h * 0.76), '大部分交互功能可以通过输入‘取消’,‘算了’来取消当前交互\n对真寻说 “真寻帮助 指令名” 获取对应详细帮助\n' + A.text((10, h * 0.68), '大部分交互功能可以通过输入‘取消’,‘算了’来取消当前交互\n对真寻说 “真寻帮助 指令名” 获取对应详细帮助\n' '可以通过 “滴滴滴- [消息]” 联系管理员(有趣的想法尽管来吧!<还有Bug和建议>)' '\n[群管理员请看 管理员帮助(群主与管理员自带 5 级权限)]\n\n' '\t「如果真寻回复了一些不符合人设的话,那是因为每日白嫖的图灵次数已用完,使用的是备用接口【QAQ】」') @@ -99,10 +99,10 @@ def create_group_help_img(group_id: int): A.paste(u, (0, u_height)) A.paste(o, (0, o_height)) # A.text((width, 10), f'总开关【{"√" if data["总开关"] else "×"}】') - A.text((10, h * 0.76), '大部分交互功能可以通过输入‘取消’,‘算了’来取消当前交互\n对真寻说 “真寻帮助 指令名” 获取对应详细帮助\n' + A.text((10, h * 0.68), '大部分交互功能可以通过输入‘取消’,‘算了’来取消当前交互\n对真寻说 “真寻帮助 指令名” 获取对应详细帮助\n' '可以通过 “滴滴滴- [消息]” 联系管理员(有趣的想法尽管来吧!<还有Bug和建议>)' f'\n[群管理员请看 管理员帮助(群主与管理员自带 {ADMIN_DEFAULT_AUTH} 级权限)]') - A.text((10, h * 0.81), f"【注】「色图概率:好感度 + {int(INITIAL_SETU_PROBABILITY*100)}%\n" + A.text((10, h * 0.77), f"【注】「色图概率:好感度 + {int(INITIAL_SETU_PROBABILITY*100)}%\n" f"\t\t每 3 点好感度 + 1次开箱,初始 {INITIAL_OPEN_CASE_COUNT} 次\n" f"\t\t开启/关闭功能只需输入‘开启/关闭 指令名’(每个功能的第一个指令)」\n" f"\t\t示例:开启签到\n" @@ -144,8 +144,8 @@ def rcmd(dfg): def get_plugin_help(msg: str) -> str: plugin = None - for p in plugins2name_dict.keys(): - if msg in plugins2name_dict[p]: + for p in plugins2info_dict.keys(): + if msg in plugins2info_dict[p]['cmd']: plugin = nonebot.plugin.get_plugin(p) break if plugin: diff --git a/plugins/hook.py b/plugins/hook.py index f7bbc5a7..38fd58da 100644 --- a/plugins/hook.py +++ b/plugins/hook.py @@ -4,8 +4,8 @@ from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent, PrivateMessageEvent, GroupMessageEvent from configs.config import BAN_RESULT, admin_plugins_auth, MALICIOUS_BAN_TIME, MALICIOUS_CHECK_TIME, MALICIOUS_BAN_COUNT from models.ban_user import BanUser -from util.utils import is_number, static_flmt, BanCheckLimiter -from util.init_result import at +from utils.utils import is_number, static_flmt, BanCheckLimiter +from utils.init_result import at from services.log import logger from models.level_user import LevelUser try: @@ -17,6 +17,8 @@ except ModuleNotFoundError: # 检查是否被ban @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: MessageEvent, state: T_State): + if not isinstance(event, MessageEvent): + return if matcher.type == 'message' and matcher.priority not in [1, 9]: if await BanUser.isban(event.user_id) and str(event.user_id) not in bot.config.superusers: time = await BanUser.check_ban_time(event.user_id) @@ -51,6 +53,8 @@ _blmt = BanCheckLimiter(MALICIOUS_CHECK_TIME, MALICIOUS_BAN_COUNT) # 恶意触发命令检测 @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State): + if not isinstance(event, MessageEvent): + return if matcher.type == 'message' and matcher.priority not in [1, 9]: if state["_prefix"]["raw_command"]: # print(state["_prefix"]["raw_command"]) @@ -77,6 +81,8 @@ async def _(matcher: Matcher, bot: Bot, event: GroupMessageEvent, state: T_State # 权限检测 @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: MessageEvent, state: T_State): + if not isinstance(event, MessageEvent): + return if await BanUser.isban(event.user_id): return if matcher.module in admin_plugins_auth.keys() and matcher.priority not in [1, 9]: diff --git a/plugins/jitang.py b/plugins/jitang.py index 58fa3649..44b73146 100644 --- a/plugins/jitang.py +++ b/plugins/jitang.py @@ -1,9 +1,7 @@ from nonebot import on_command -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from services.log import logger from nonebot.adapters.cqhttp import Bot, Event -from nonebot.matcher import Matcher -from nonebot.message import run_preprocessor, IgnoredException from nonebot.typing import T_State import aiohttp from asyncio.exceptions import TimeoutError diff --git a/plugins/last_chat/data_source.py b/plugins/last_chat/data_source.py index 87e93fbd..7f89b49f 100644 --- a/plugins/last_chat/data_source.py +++ b/plugins/last_chat/data_source.py @@ -1,5 +1,5 @@ from configs.path_config import DATA_PATH -from util.utils import get_bot +from utils.utils import get_bot from models.group_remind import GroupRemind from datetime import datetime import time diff --git a/plugins/luxun/__init__.py b/plugins/luxun/__init__.py index add6ab97..7a9dafd1 100644 --- a/plugins/luxun/__init__.py +++ b/plugins/luxun/__init__.py @@ -4,10 +4,10 @@ from configs.path_config import IMAGE_PATH, TTF_PATH from nonebot import on_command from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent -from util.init_result import image +from utils.init_result import image from services.log import logger -from util.utils import UserExistLimiter, get_message_text -from util.img_utils import pic2b64 +from utils.utils import UserExistLimiter, get_message_text +from utils.img_utils import pic2b64 __plugin_name__ = '鲁迅说' @@ -16,7 +16,7 @@ __plugin_usage__ = '用法:鲁迅说 [消息]' _ulmt = UserExistLimiter() -luxun = on_command("鲁迅说过", aliases={"鲁迅说"}) +luxun = on_command("鲁迅说过", aliases={"鲁迅说"}, priority=5, block=True) @luxun.handle() diff --git a/plugins/member_activity_handle.py b/plugins/member_activity_handle.py new file mode 100644 index 00000000..a61ce714 --- /dev/null +++ b/plugins/member_activity_handle.py @@ -0,0 +1,214 @@ +from nonebot import on_message, on_command +import time +from utils.utils import scheduler, get_bot, get_message_text, is_number, get_message_at +from models.group_member_info import GroupInfoUser +from services.log import logger +from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, GROUP +from nonebot.typing import T_State +from pathlib import Path +from configs.path_config import DATA_PATH +try: + import ujson as json +except ModuleNotFoundError: + import json + +__plugin_name__ = '群员发言检测 [Hidden]' + + +check_activity = on_message(priority=1, permission=GROUP, block=False) + +show_setting = on_command('群员活跃检测设置', permission=GROUP, priority=1, block=True) + +set_check_time = on_command('设置群员活跃检测时长', permission=GROUP, priority=1, block=True) + +set_white_list = on_command('添加群员活跃检测白名单', aliases={'移除群员活跃检测白名单'}, permission=GROUP, priority=1, block=True) + +show_white_list = on_command('查看群员活跃检测白名单', permission=GROUP, priority=1, block=True) + +_file = Path(f'{DATA_PATH}/member_activity_check.json') + +try: + data = json.load(open(_file, 'r', encoding='utf8')) +except (FileNotFoundError, ValueError, TypeError): + data = { + 'check_time': time.time() + } + + +@check_activity.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + group = str(event.group_id) + user_id = str(event.user_id) + now = time.time() + if not data.get(group): + await _init_group_member_info(bot, group) + data[group]['data'][user_id] = now + if now - data['check_time'] > 10 * 60: + data['check_time'] = now + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, indent=4, ensure_ascii=False) + logger.info('群员发言时间检测存储数据成功....') + + +@set_check_time.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + msg = get_message_text(event.json()) + if not is_number(msg): + await set_check_time.finish('请检查输入是否是数字....', at_sender=True) + group = str(event.group_id) + if not data.get(group): + await _init_group_member_info(bot, group) + data[group]['check_time_day'] = int(msg) + await set_check_time.send(f'设置群员活跃检测时长成功:{msg} 天\n【设置为 0 即为关闭】') + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, indent=4, ensure_ascii=False) + logger.info(f'USER {event.user_id} GROUP {event.group_id} 设置群员活跃检测时长:{msg}') + + +@set_white_list.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + group = str(event.group_id) + at = get_message_at(event.json()) + if not data.get(group): + await _init_group_member_info(bot, group) + if at: + rst = '' + if state["_prefix"]["raw_command"] == '添加群员活跃检测白名单': + for user in at: + try: + user_name = (await GroupInfoUser.select_member_info(int(user), event.group_id)).user_name + except AttributeError: + user_name = str(user) + if str(user) not in data[group]['white_list']: + rst += f'{user_name} ' + data[group]['white_list'].append(str(user)) + else: + await set_white_list.send(f'{user_name} 已在群员活跃检测白名单中...') + rst = f'已将\n{rst}\n等添加入群员活跃检测白名单' + else: + for user in at: + try: + user_name = (await GroupInfoUser.select_member_info(int(user), event.group_id)).user_name + except AttributeError: + user_name = str(user) + if str(user) in data[group]['white_list']: + rst += f'{user_name} ' + data[group]['white_list'].remove(str(user)) + else: + await set_white_list.send(f'{user_name} 未在群员活跃检测白名单中...') + rst = f'已将 \n{rst}\n等添移除群员活跃检测白名单' + await set_white_list.send(rst, sender=True) + logger.info(f'群员活跃检测白名单变动 USER {event.user_id}:{rst}') + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, ensure_ascii=False, indent=4) + else: + await set_white_list.finish('添加群员活跃检测白名单时请艾特对象!', at_sender=True) + + +@show_white_list.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + group = str(event.group_id) + if not data.get(group): + await _init_group_member_info(bot, group) + if data[group]['white_list']: + user_name_list = [] + for user in data[group]['white_list']: + try: + user_name = (await GroupInfoUser.select_member_info(int(user), event.group_id)).user_name + except AttributeError: + user_name = str(user) + user_name_list.append(user_name) + rst = '' + for i in range(len(user_name_list)): + rst += f'{user_name_list[i]}({data[group]["white_list"][i]})\n' + await show_white_list.send(rst) + else: + await show_white_list.finish('群员活跃检测白名单中没有任何用户...') + + +@show_setting.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + group = str(event.group_id) + user_name_list = [] + if not data.get(group): + await _init_group_member_info(bot, group) + if data[group]['white_list']: + for user in data[group]['white_list']: + try: + user_name = (await GroupInfoUser.select_member_info(int(user), event.group_id)).user_name + except AttributeError: + user_name = str(user) + user_name_list.append(user_name) + await show_setting.send(f'群员活跃检测:\n' + f'检测天数:{data[group]["check_time_day"]}\n' + f'白名单:{user_name_list}\n' + f'【检测天数=0时为关闭】') + + +@scheduler.scheduled_job( + 'cron', + hour=7, + minute=1, +) +async def _(): + bot = get_bot() + now = time.time() + rst = '群员发言时间检测:\n' + for group in data.keys(): + if group != 'check_time': + member_list = await bot.get_group_member_list(group_id=int(group), self_id=int(bot.self_id)) + member_list = [x['user_id'] for x in member_list] + for user in member_list: + if user not in data[group]['data']: + data[group][str(user)] = now + logger.info('群员活跃检测:自动更新群员完成...') + if group != 'check_time' and data[group]['check_time_day'] > 0: + for user in list(data[group]['data'].keys()): + user = str(user) + if user not in data[group]['white_list'] and \ + now - data[group]['data'][user] > data[group]['check_time_day'] * 24 * 60 * 60: + try: + # await bot.set_group_kick(group_id=int(group), user_id=int(user)) + t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(data[group]['data'][user])) + try: + user_name = (await GroupInfoUser.select_member_info(int(user), int(group))).user_name + except AttributeError: + user_name = user + rst += f'检测 {user_name}({user}) 上次发言时间过久:\n时间:{t}\n' + # await bot.send_group_msg(group_id=int(group), + # message=f'群员发言时间检测:检测 {user_name}({user}) 上次' + # f'发言时间过久:..\n时间:{t}..') + del data[group]['data'][user] + except Exception as e: + logger.warning(f'群员活跃检测:{user} 踢出失败,疑真寻不是管理或对方是管理或不在群内...e:{e}') + if rst: + await bot.send_group_msg(group_id=int(group), + message=rst[:-1]) + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, ensure_ascii=False, indent=4) + + +async def _init_group_member_info(bot: Bot, group: str): + global data + now = time.time() + data[group] = {} + data[group]['data'] = {} + data[group]['white_list'] = [] + data[group]['check_time_day'] = 0 + member_list = await bot.get_group_member_list(group_id=int(group), self_id=int(bot.self_id)) + member_list = [x['user_id'] for x in member_list] + for user in member_list: + data[group]['data'][str(user)] = now + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, ensure_ascii=False, indent=4) + + + + + + + + + + + diff --git a/plugins/move_img/__init__.py b/plugins/move_img/__init__.py index 2f3f3e79..ef2eca0c 100644 --- a/plugins/move_img/__init__.py +++ b/plugins/move_img/__init__.py @@ -5,7 +5,7 @@ from nonebot.rule import to_me from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent from configs.config import IMAGE_DIR_LIST -from util.utils import is_number, cn2py +from utils.utils import is_number, cn2py from configs.path_config import IMAGE_PATH diff --git a/plugins/mute.py b/plugins/mute.py index 4ed0497e..22d22852 100644 --- a/plugins/mute.py +++ b/plugins/mute.py @@ -1,12 +1,12 @@ from nonebot import on_message, on_command from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.adapters.cqhttp.permission import GROUP -from util.utils import get_message_text, is_number, get_message_imgs, get_local_proxy +from utils.utils import get_message_text, is_number, get_message_imgs, get_local_proxy from nonebot.typing import T_State import time from nonebot.adapters.cqhttp.exception import ActionFailed from configs.path_config import DATA_PATH, IMAGE_PATH -from util.img_utils import get_img_hash +from utils.img_utils import get_img_hash from services.log import logger import aiohttp import aiofiles diff --git a/plugins/nickname.py b/plugins/nickname.py index f1280a39..d41251bd 100644 --- a/plugins/nickname.py +++ b/plugins/nickname.py @@ -2,7 +2,7 @@ from nonebot import on_command from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent from nonebot.rule import to_me -from util.utils import get_message_text +from utils.utils import get_message_text from models.group_member_info import GroupInfoUser from models.friend_user import FriendUser import random diff --git a/plugins/nonebot_plugin_manager/__init__.py b/plugins/nonebot_plugin_manager/__init__.py index 30a2c3f5..28be6f30 100644 --- a/plugins/nonebot_plugin_manager/__init__.py +++ b/plugins/nonebot_plugin_manager/__init__.py @@ -3,8 +3,9 @@ from nonebot.matcher import Matcher from nonebot.typing import T_State from nonebot.exception import IgnoredException from nonebot.message import run_preprocessor -from nonebot.adapters.cqhttp import Event, Bot, GroupMessageEvent, PrivateMessageEvent -from configs.config import plugins2name_dict +from nonebot.adapters.cqhttp import Event, Bot, GroupMessageEvent, PrivateMessageEvent, MessageEvent +from configs.config import plugins2info_dict +from utils.utils import FreqLimiter from models.ban_user import BanUser from .data import ( block_plugin, @@ -24,17 +25,21 @@ export.get_group_plugin_list = get_group_plugin_list # 注册 shell_like 事件响应器 plugin_manager = on_shell_command("npm", parser=npm_parser, priority=1) +flmt = FreqLimiter(60) + # 在 Matcher 运行前检测其是否启用 @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State): + if not isinstance(event, MessageEvent): + return plugin = matcher.module group_id = _get_group_id(event) loaded_plugin_list = _get_loaded_plugin_list() plugin_list = auto_update_plugin_list(loaded_plugin_list) # 无视本插件的 Matcher - if plugin not in plugins2name_dict or matcher.priority in [1, 9] or await BanUser.isban(event.user_id): + if plugin not in plugins2info_dict or matcher.priority in [1, 9] or await BanUser.isban(event.user_id): return try: if isinstance(event, PrivateMessageEvent) and plugin_list[plugin]["default"]: @@ -47,8 +52,12 @@ async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State): # print(f'{matcher.module} -> this is hook') if not plugin_list[plugin]["default"]: if event.message_type == 'group': - await bot.send_group_msg(group_id=event.group_id, message='此功能正在维护...') + if flmt.check(event.group): + flmt.start_cd(event.group) + await bot.send_group_msg(group_id=event.group_id, message='此功能正在维护...') else: + if flmt.check(event.user_id): + flmt.start_cd(event.user_id) await bot.send_private_msg(user_id=event.user_id, message='此功能正在维护...') raise IgnoredException(f"Nonebot Plugin Manager has blocked {plugin} !") # print(plugin_list[plugin]) @@ -58,7 +67,9 @@ async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State): if group_id in plugin_list[plugin]: if not plugin_list[plugin][group_id]: if plugin != 'ai' and matcher.type == 'message': - await bot.send_group_msg(group_id=event.group_id, message='该群未开启此功能..') + if flmt.check(event.group): + flmt.start_cd(event.group) + await bot.send_group_msg(group_id=event.group_id, message='该群未开启此功能..') raise IgnoredException(f"Nonebot Plugin Manager has blocked {plugin} !") diff --git a/plugins/nonebot_plugin_manager/data.py b/plugins/nonebot_plugin_manager/data.py index 56c0c486..ed5363c5 100644 --- a/plugins/nonebot_plugin_manager/data.py +++ b/plugins/nonebot_plugin_manager/data.py @@ -1,7 +1,7 @@ import json import httpx from pathlib import Path -from configs.config import plugins2name_dict +from configs.config import plugins2info_dict _DATA_PATH = Path() / "data" / "manager" / "plugin_list.json" @@ -80,9 +80,9 @@ def _update_plugin_list(group_id: str, block: bool, *plugins: str) -> str: message = "结果如下:" operate = "屏蔽" if block else "启用" for plugin in plugins: - for values in plugins2name_dict.values(): - if plugin in values: - plugin = list(plugins2name_dict.keys())[list(plugins2name_dict.values()).index(values)] + for values in plugins2info_dict.values(): + if plugin in values['cmd']: + plugin = list(plugins2info_dict.keys())[list(plugins2info_dict.values()).index(values)] # print(plugin) break message += "\n" diff --git a/plugins/nonebot_plugin_picsearcher/__init__.py b/plugins/nonebot_plugin_picsearcher/__init__.py index 9f97fd15..26d9df03 100644 --- a/plugins/nonebot_plugin_picsearcher/__init__.py +++ b/plugins/nonebot_plugin_picsearcher/__init__.py @@ -7,7 +7,7 @@ from nonebot.plugin import on_command, on_message from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent from nonebot.typing import T_State from services.log import logger -from util.utils import get_message_text, get_message_imgs +from utils.utils import get_message_text, get_message_imgs from configs.config import MAX_FIND_IMG_COUNT from nonebot.rule import to_me diff --git a/plugins/nonebot_plugin_picsearcher/ascii2d.py b/plugins/nonebot_plugin_picsearcher/ascii2d.py index 941e9d0e..c45635f4 100644 --- a/plugins/nonebot_plugin_picsearcher/ascii2d.py +++ b/plugins/nonebot_plugin_picsearcher/ascii2d.py @@ -2,11 +2,11 @@ from typing import List, Tuple from urllib.parse import urljoin import aiofiles -from util.utils import get_local_proxy -from util.user_agent import get_user_agent +from utils.utils import get_local_proxy +from utils.user_agent import get_user_agent from configs.path_config import IMAGE_PATH from asyncio.exceptions import TimeoutError -from util.init_result import image +from utils.init_result import image from lxml.html import fromstring import aiohttp diff --git a/plugins/nonebot_plugin_picsearcher/saucenao.py b/plugins/nonebot_plugin_picsearcher/saucenao.py index 14987b83..c3533c39 100644 --- a/plugins/nonebot_plugin_picsearcher/saucenao.py +++ b/plugins/nonebot_plugin_picsearcher/saucenao.py @@ -2,11 +2,11 @@ import io from typing import List, Tuple, Union import aiofiles -from util.utils import get_local_proxy -from util.user_agent import get_user_agent +from utils.utils import get_local_proxy +from utils.user_agent import get_user_agent from configs.path_config import IMAGE_PATH from asyncio.exceptions import TimeoutError -from util.init_result import image +from utils.init_result import image import aiohttp from lxml.html import fromstring diff --git a/plugins/one_friend/__init__.py b/plugins/one_friend/__init__.py index 1d25b1ac..b30ade13 100644 --- a/plugins/one_friend/__init__.py +++ b/plugins/one_friend/__init__.py @@ -1,15 +1,15 @@ import aiohttp -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from io import BytesIO from random import choice from nonebot import on_regex from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent -from util.utils import get_message_text, get_local_proxy, get_message_at -from util.init_result import image +from utils.utils import get_message_text, get_local_proxy, get_message_at +from utils.init_result import image import re -from util.img_utils import CreateImg +from utils.img_utils import CreateImg __plugin_name__ = '我有一个朋友' @@ -53,3 +53,5 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): A.text((150, 85), msg, (125, 125, 125)) await one_friend.send(image(b64=A.pic2bs4())) + + diff --git a/plugins/open_cases/__init__.py b/plugins/open_cases/__init__.py index ac0fa8e8..92499c0c 100644 --- a/plugins/open_cases/__init__.py +++ b/plugins/open_cases/__init__.py @@ -1,5 +1,5 @@ from nonebot import on_command -from util.utils import FreqLimiter, scheduler, get_message_text, is_number +from utils.utils import FreqLimiter, scheduler, get_message_text, is_number from nonebot.adapters.cqhttp.permission import GROUP from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent diff --git a/plugins/open_cases/open_cases_c.py b/plugins/open_cases/open_cases_c.py index 068278e0..05cc5809 100644 --- a/plugins/open_cases/open_cases_c.py +++ b/plugins/open_cases/open_cases_c.py @@ -4,16 +4,16 @@ from services.log import logger from services.db_context import db from models.open_cases_user import OpenCasesUser from models.sigin_group_user import SignGroupUser -from util.init_result import image +from utils.init_result import image import pypinyin import random from .utils import get_price from models.buff_price import BuffPrice from PIL import Image -from util.img_utils import alphabg2white_PIL, CreateImg +from utils.img_utils import alphabg2white_PIL, CreateImg from configs.path_config import IMAGE_PATH import asyncio -from util.utils import cn2py +from utils.utils import cn2py from configs.config import INITIAL_OPEN_CASE_COUNT @@ -146,6 +146,11 @@ async def open_shilian_case(user_qq: int, group: int, case_name: str, num: int = if int(MAX_COUNT + int(impression) / 3) - user.today_open_total < num: return f"今天开箱次数不足{num}次噢,请单抽试试看(也许单抽运气更好?)" \ f"\n剩余开箱次数:{int(MAX_COUNT + int(impression) / 3) - user.today_open_total}" + await user.update( + total_count=user.total_count + num, + spend_money=user.spend_money + 17 * num, + today_open_total=user.today_open_total + num, + ).apply() if num < 5: h = 270 elif num % 5 == 0: @@ -158,7 +163,7 @@ async def open_shilian_case(user_qq: int, group: int, case_name: str, num: int = # lan zi fen hong jin price uplist = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0] img_list = [] - name_list = ['蓝', '蓝(暗金)', '紫', '紫(暗金)', '粉', '粉(暗金)', '红', '红(暗金)', '金', '金(暗金)'] + name_list = ['蓝', '蓝(暗金)', '紫', '紫(暗金)', '粉', '粉(暗金)', '红', '红(暗金)', '金', '金(暗金)'] async with db.transaction(): for _ in range(num): knifes_flag = False @@ -222,7 +227,7 @@ async def open_shilian_case(user_qq: int, group: int, case_name: str, num: int = wImg.text((5, 240), f'价格:{price_result}') img_list.append(wImg) logger.info(f"USER {user_qq} GROUP {group} 开启{case_name}武器箱 获得 {skin} 磨损:{mosun}, 价格:{uplist[10]}") - if await update_user_total(user, uplist, num): + if await update_user_total(user, uplist, 0): logger.info(f"USER {user_qq} GROUP {group} 开启{case_name}武器箱 {num} 次, 数据更新成功") else: logger.warning(f"USER {user_qq} GROUP {group} 开启{case_name}武器箱 {num} 次, 价格:{uplist[10]}, 数据更新失败") @@ -251,22 +256,22 @@ def _handle_is_MAX_COUNT() -> str: return f"今天已达开箱上限了喔,明天再来吧\n(提升好感度可以增加每日开箱数 #疯狂暗示)" -async def update_user_total(user: OpenCasesUser, uplist: list, num: int = 1) -> bool: +async def update_user_total(user: OpenCasesUser, up_list: list, num: int = 1) -> bool: try: await user.update( total_count=user.total_count + num, - blue_count=user.blue_count + uplist[0], - blue_st_count=user.blue_st_count + uplist[1], - purple_count=user.purple_count + uplist[2], - purple_st_count=user.purple_st_count + uplist[3], - pink_count=user.pink_count + uplist[4], - pink_st_count=user.pink_st_count + uplist[5], - red_count=user.red_count + uplist[6], - red_st_count=user.red_st_count + uplist[7], - knife_count=user.knife_count + uplist[8], - knife_st_count=user.knife_st_count + uplist[9], + blue_count=user.blue_count + up_list[0], + blue_st_count=user.blue_st_count + up_list[1], + purple_count=user.purple_count + up_list[2], + purple_st_count=user.purple_st_count + up_list[3], + pink_count=user.pink_count + up_list[4], + pink_st_count=user.pink_st_count + up_list[5], + red_count=user.red_count + up_list[6], + red_st_count=user.red_st_count + up_list[7], + knife_count=user.knife_count + up_list[8], + knife_st_count=user.knife_st_count + up_list[9], spend_money=user.spend_money + 17 * num, - make_money=user.make_money + uplist[10], + make_money=user.make_money + up_list[10], today_open_total=user.today_open_total + num, open_cases_time_last=datetime.now() ).apply() diff --git a/plugins/open_cases/utils.py b/plugins/open_cases/utils.py index e7bc6645..4e43af28 100644 --- a/plugins/open_cases/utils.py +++ b/plugins/open_cases/utils.py @@ -1,16 +1,16 @@ from models.buff_price import BuffPrice from services.db_context import db from datetime import datetime, timedelta -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from configs.path_config import IMAGE_PATH import aiohttp import aiofiles from models.open_cases_user import OpenCasesUser import os from services.log import logger -from util.utils import get_bot +from utils.utils import get_bot from models.group_remind import GroupRemind -from util.utils import get_cookie_text +from utils.utils import get_cookie_text from asyncio.exceptions import TimeoutError import pypinyin from nonebot.adapters.cqhttp.exception import ActionFailed diff --git a/plugins/parse_bilibili_json.py b/plugins/parse_bilibili_json.py index 7538f377..75d31cca 100644 --- a/plugins/parse_bilibili_json.py +++ b/plugins/parse_bilibili_json.py @@ -2,12 +2,12 @@ from nonebot import on_message from services.log import logger from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.typing import T_State -from util.utils import get_message_json, get_local_proxy +from utils.utils import get_message_json, get_local_proxy import json -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from nonebot.adapters.cqhttp.permission import GROUP from bilibili_api import video -from util.init_result import image +from utils.init_result import image from models.group_remind import GroupRemind from nonebot.adapters.cqhttp.exception import ActionFailed import time diff --git a/plugins/pid_search.py b/plugins/pid_search.py new file mode 100644 index 00000000..0b94602c --- /dev/null +++ b/plugins/pid_search.py @@ -0,0 +1,100 @@ +from nonebot import on_command +from nonebot.adapters.cqhttp import Bot, MessageEvent, Message, GroupMessageEvent +from nonebot.typing import T_State +from utils.utils import get_message_text, is_number +from utils.init_result import image +import aiohttp +from services.log import logger +from asyncio.exceptions import TimeoutError +import asyncio +import aiofiles +from io import BytesIO +from configs.path_config import IMAGE_PATH + +try: + import ujson as json +except ModuleNotFoundError: + import json + +__plugin_name__ = 'p搜' +__plugin_usage__ = '用法: 通过pid在Pixiv上搜索图片\n格式:p搜 [pid]\n\t示例:p搜 79520120' + +pid_search = on_command("p搜", aliases={'pixiv搜', 'P搜'}, priority=5, block=True) + +url = 'https://api.fantasyzone.cc/tu/search.php' + + +@pid_search.args_parser +async def _(bot: Bot, event: MessageEvent, state: T_State): + pid = get_message_text(event.json()) + if pid: + if pid in ['取消', '算了']: + await pid_search.finish('已取消操作...') + if not is_number(pid): + await pid_search.reject('笨蛋,重新输入数!字!', at_sender=True) + state['pid'] = pid + + +@pid_search.handle() +async def _(bot: Bot, event: MessageEvent, state: T_State): + pid = get_message_text(event.json()) + if pid: + state['pid'] = pid + + +@pid_search.got('pid', prompt='需要查询的图片PID是?') +async def _(bot: Bot, event: MessageEvent, state: T_State): + pid = state['pid'] + params = { + 'id': pid, + 'p': 1, + } + async with aiohttp.ClientSession() as session: + for _ in range(10): + try: + async with session.get(url, timeout=2, params=params) as response: + data = json.loads(await response.text()) + except TimeoutError: + pass + else: + if not data['width'] and not data['height']: + await pid_search.finish(f'没有搜索到 PID:{pid} 的图片', at_sender=True) + pid = data['id'] + title = data['title'] + author = data['userName'] + author_id = data['userId'] + img_url = data['url'] + for _ in range(5): + try: + await download_pic(img_url, event.user_id) + except TimeoutError: + pass + else: + break + else: + await pid_search.finish('图片下载失败了....', at_sender=True) + tmp = '' + if isinstance(event, GroupMessageEvent): + tmp = '\n【注】将在30后撤回......' + msg_id = await pid_search.send(Message(f'title:{title}\n' + f'pid:{pid}\n' + f'author:{author}\n' + f'author_id:{author_id}\n' + f'{image(f"pid_search_{event.user_id}.png", "temp")}' + f'{tmp}')) + logger.info( + f"(USER {event.user_id}, GROUP {event.group_id if event.message_type != 'private' else 'private'})" + f" 查询图片 PID:{pid}") + if isinstance(event, GroupMessageEvent): + await asyncio.sleep(30) + await bot.delete_msg(message_id=msg_id['message_id'], self_id=int(bot.self_id)) + break + else: + await pid_search.finish('图片下载失败了....', at_sender=True) + + +async def download_pic(img_url: str, user_id: int): + async with aiohttp.ClientSession() as session: + async with session.get(img_url, timeout=2) as res: + async with aiofiles.open(f'{IMAGE_PATH}/temp/pid_search_{user_id}.png', 'wb') as f: + await f.write(await res.read()) diff --git a/plugins/pixiv/__init__.py b/plugins/pixiv/__init__.py index e6b0f513..1f928ab6 100644 --- a/plugins/pixiv/__init__.py +++ b/plugins/pixiv/__init__.py @@ -1,7 +1,7 @@ from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot import on_command -from util.utils import get_message_text, UserExistLimiter, is_number +from utils.utils import get_message_text, UserExistLimiter, is_number from .data_source import get_pixiv_urls, download_pixiv_imgs, search_pixiv_urls import time from services.log import logger diff --git a/plugins/pixiv/data_source.py b/plugins/pixiv/data_source.py index 7ed8f086..3cbe9b44 100644 --- a/plugins/pixiv/data_source.py +++ b/plugins/pixiv/data_source.py @@ -1,11 +1,11 @@ import aiohttp import aiofiles from configs.path_config import IMAGE_PATH -from util.utils import get_local_proxy -from util.user_agent import get_user_agent +from utils.utils import get_local_proxy +from utils.user_agent import get_user_agent from bs4 import BeautifulSoup import feedparser -from util.init_result import image +from utils.init_result import image from asyncio.exceptions import TimeoutError from configs.config import RSSHUBAPP from aiohttp.client_exceptions import ClientConnectorError diff --git a/plugins/poke/__init__.py b/plugins/poke/__init__.py index c6a2720e..19a13b98 100644 --- a/plugins/poke/__init__.py +++ b/plugins/poke/__init__.py @@ -3,10 +3,10 @@ from nonebot.adapters.cqhttp import Bot, PokeNotifyEvent from nonebot.typing import T_State from configs.path_config import VOICE_PATH, IMAGE_PATH import os -from util.init_result import record, image, poke +from utils.init_result import record, image, poke from services.log import logger import random -from util.utils import CountLimiter +from utils.utils import CountLimiter from models.ban_user import BanUser __plugin_name__ = '戳一戳 [Hidden]' diff --git a/plugins/quotations.py b/plugins/quotations.py index 86cd3c18..d85bbec5 100644 --- a/plugins/quotations.py +++ b/plugins/quotations.py @@ -3,7 +3,7 @@ from services.log import logger from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State import aiohttp -from util.utils import get_local_proxy +from utils.utils import get_local_proxy __plugin_name__ = '语录' diff --git a/plugins/reimu/__init__.py b/plugins/reimu/__init__.py index eee367d9..082059c0 100644 --- a/plugins/reimu/__init__.py +++ b/plugins/reimu/__init__.py @@ -4,7 +4,7 @@ from .data_source import from_reimu_get_info from services.log import logger from nonebot.adapters.cqhttp import Bot, Event from nonebot.typing import T_State -from util.utils import is_number, get_message_text, UserExistLimiter, scheduler +from utils.utils import is_number, get_message_text, UserExistLimiter, scheduler from models.count_user import UserCount from configs.config import COUNT_PER_DAY_REIMU diff --git a/plugins/reimu/data_source.py b/plugins/reimu/data_source.py index 98efde62..3feaecf0 100644 --- a/plugins/reimu/data_source.py +++ b/plugins/reimu/data_source.py @@ -3,8 +3,8 @@ import time import aiohttp from services.log import logger from configs.config import MAXINFO_REIMU -from util.user_agent import get_user_agent -from util.utils import get_local_proxy +from utils.user_agent import get_user_agent +from utils.utils import get_local_proxy from asyncio.exceptions import TimeoutError diff --git a/plugins/remind/__init__.py b/plugins/remind/__init__.py index a8bce100..e1af0fcb 100644 --- a/plugins/remind/__init__.py +++ b/plugins/remind/__init__.py @@ -1,5 +1,5 @@ -from util.init_result import image -from util.utils import scheduler, get_bot +from utils.init_result import image +from utils.utils import scheduler, get_bot from services.log import logger from models.group_remind import GroupRemind from models.group_info import GroupInfo diff --git a/plugins/russian/__init__.py b/plugins/russian/__init__.py index 16b34562..37b7e350 100644 --- a/plugins/russian/__init__.py +++ b/plugins/russian/__init__.py @@ -3,9 +3,9 @@ import random import asyncio from nonebot.adapters.cqhttp import GROUP, Bot, GroupMessageEvent, Message from nonebot.typing import T_State -from util.utils import get_message_text, is_number, get_message_at +from utils.utils import get_message_text, is_number, get_message_at from models.group_member_info import GroupInfoUser -from util.init_result import at +from utils.init_result import at from models.russian_user import RussianUser from models.bag_user import BagUser from services.log import logger @@ -72,7 +72,7 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): else: await accept.finish('你的金币不足以接受这场对决!', at_sender=True) - player2_name = (await GroupInfoUser.select_member_info(event.user_id, event.group_id)).user_name + player2_name = event.sender.card if event.sender.card else event.sender.nickname rs_player[event.group_id][2] = event.user_id rs_player[event.group_id]['player2'] = player2_name @@ -151,7 +151,6 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): time.time() - rs_player[event.group_id]['time'] > 30: await shot.send('决斗已过时,强行结算...') await end_game(bot, event) - return if not rs_player[event.group_id][2] and time.time() - rs_player[event.group_id]['time'] > 30: rs_player[event.group_id][1] = 0 rs_player[event.group_id][2] = 0 @@ -178,7 +177,7 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): async def _(bot: Bot, event: GroupMessageEvent, state: T_State): global rs_player bullet_num = state['bullet_num'] - at_ = state['at'] + at_ = state['at'] if state.get('at') else [] money = state['money'] if state.get('money') else 200 user_money = await BagUser.get_gold(event.user_id, event.group_id) if bullet_num < 0 or bullet_num > 6: @@ -188,11 +187,11 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): if money > user_money: await rssian.finish('你没有足够的钱支撑起这场挑战', at_sender=True) - player1_name = (await GroupInfoUser.select_member_info(event.user_id, event.group_id)).user_name + player1_name = event.sender.card if event.sender.card else event.sender.nickname if at_: at_ = at_[0] - at_player_name = (await GroupInfoUser.select_member_info(at_, event.group_id)).user_name + at_player_name = event.sender.card if event.sender.card else event.sender.nickname msg = f'{player1_name} 向 {at(at_)} 发起了决斗!请 {at_player_name} 在30秒内回复‘接受对决’ or ‘拒绝对决’,超时此次决斗作废!' else: at_ = 0 diff --git a/plugins/russian/data_source.py b/plugins/russian/data_source.py index b79a0fae..8a2abef9 100644 --- a/plugins/russian/data_source.py +++ b/plugins/russian/data_source.py @@ -1,5 +1,5 @@ from models.russian_user import RussianUser -from util.data_utils import init_rank +from utils.data_utils import init_rank async def rank(group_id: int, itype) -> str: diff --git a/plugins/search_anime/__init__.py b/plugins/search_anime/__init__.py index c383dbeb..3d92ea68 100644 --- a/plugins/search_anime/__init__.py +++ b/plugins/search_anime/__init__.py @@ -4,7 +4,7 @@ from services.log import logger from nonebot.adapters.cqhttp import Bot, Event from nonebot.typing import T_State from configs.config import MAXINFO_GROUP_ANIME, MAXINFO_PRIVATE_ANIME -from util.utils import get_message_text, get_message_type, UserExistLimiter +from utils.utils import get_message_text, get_message_type, UserExistLimiter __plugin_name__ = '搜番' diff --git a/plugins/search_anime/data_source.py b/plugins/search_anime/data_source.py index 7acb8e86..450c93e9 100644 --- a/plugins/search_anime/data_source.py +++ b/plugins/search_anime/data_source.py @@ -4,7 +4,7 @@ from urllib import parse from services.log import logger import aiohttp import time -from util.utils import get_local_proxy +from utils.utils import get_local_proxy async def from_anime_get_info(key_word: str, max: int) -> str: diff --git a/plugins/search_buff_skin_price/__init__.py b/plugins/search_buff_skin_price/__init__.py index 60c5bca7..3cd8c953 100644 --- a/plugins/search_buff_skin_price/__init__.py +++ b/plugins/search_buff_skin_price/__init__.py @@ -5,7 +5,7 @@ from nonebot.typing import T_State from nonebot.adapters import Bot, Event from nonebot.rule import to_me from nonebot.permission import SUPERUSER -from util.utils import UserExistLimiter, get_message_text +from utils.utils import UserExistLimiter, get_message_text __plugin_name__ = '查询皮肤' diff --git a/plugins/search_buff_skin_price/data_source.py b/plugins/search_buff_skin_price/data_source.py index e51793fb..8eade884 100644 --- a/plugins/search_buff_skin_price/data_source.py +++ b/plugins/search_buff_skin_price/data_source.py @@ -1,6 +1,6 @@ -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent import aiohttp -from util.utils import get_cookie_text +from utils.utils import get_cookie_text from configs.path_config import TXT_PATH from asyncio.exceptions import TimeoutError from configs.config import buff_proxy diff --git a/plugins/send_dinggong_voice/__init__.py b/plugins/send_dinggong_voice/__init__.py index 1248019f..80968385 100644 --- a/plugins/send_dinggong_voice/__init__.py +++ b/plugins/send_dinggong_voice/__init__.py @@ -1,9 +1,9 @@ from nonebot import on_keyword -from util.init_result import record +from utils.init_result import record from configs.path_config import VOICE_PATH import random from services.log import logger -from util.utils import FreqLimiter +from utils.utils import FreqLimiter from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.rule import to_me diff --git a/plugins/send_img/__init__.py b/plugins/send_img/__init__.py index 1cbd28c0..3c09f55a 100644 --- a/plugins/send_img/__init__.py +++ b/plugins/send_img/__init__.py @@ -1,19 +1,29 @@ -from nonebot import on_command, on_keyword +from nonebot import on_command, on_keyword, on_regex from configs.path_config import IMAGE_PATH -from util.init_result import image +from utils.init_result import image +from utils.utils import get_message_text, is_number import os import random -from util.utils import is_number from services.log import logger from nonebot.typing import T_State -from nonebot.adapters import Bot, Event -from util.utils import FreqLimiter, cn2py +from nonebot.adapters.cqhttp import Bot, MessageEvent, Message +from utils.utils import FreqLimiter, cn2py from models.group_remind import GroupRemind +from asyncio.exceptions import TimeoutError from configs.config import IMAGE_DIR_LIST +import aiofiles +import aiohttp +import re +try: + import ujson as json +except ModuleNotFoundError: + import json -__plugin_name__ = '壁纸/萝莉/美图' -__plugin_usage__ = '用法: 发送"壁纸/萝莉/美图", 回复图片,后添加id获得指定图片' +__plugin_name__ = '壁纸/萝莉/美图/在线搜图' +__plugin_usage__ = '用法: \n' \ + '\t1.发送"壁纸/萝莉/美图", 回复图片,后添加id获得指定图片 示例:萝莉 123\n' \ + '\t2.在线搜图 示例:1张米浴的图' _flmt = FreqLimiter(1) @@ -23,11 +33,15 @@ cmd = set(IMAGE_DIR_LIST) # print(cmd) send_img = on_command("img", aliases=cmd, priority=5, block=True) +pa = on_keyword({"爬", "爪巴"}, priority=1, block=True) +search_img = on_regex('.*[份|发|张|个|次|点]图.*?', priority=6, block=True) + +search_url = 'https://api.fantasyzone.cc/tu/search.php' @send_img.handle() -async def _(bot: Bot, event: Event, state: T_State): - img_id = str(event.get_message()) +async def _(bot: Bot, event: MessageEvent, state: T_State): + img_id = get_message_text(event.json()) path = cn2py(state["_prefix"]["raw_command"]) + '/' if path in IMAGE_DIR_LIST: if not os.path.exists(f'{IMAGE_PATH}/{path}/'): @@ -53,11 +67,8 @@ async def _(bot: Bot, event: Event, state: T_State): await send_img.finish(f"不想给你看Ov|") -pa = on_keyword({"爬", "爪巴"}, priority=1, block=True) - - @pa.handle() -async def _(bot: Bot, event: Event, state: T_State): +async def _(bot: Bot, event: MessageEvent, state: T_State): if await GroupRemind.get_status(event.group_id, 'pa'): try: if str(event.get_message()[:2]) in ['开启', '关闭']: @@ -68,3 +79,81 @@ async def _(bot: Bot, event: Event, state: T_State): return _flmt.start_cd(event.user_id) await pa.finish(image(random.choice(os.listdir(IMAGE_PATH + "pa")), 'pa')) + + +num_key = { + '一': 1, + '二': 2, + '两': 2, + '双': 2, + '三': 3, + '四': 4, + '五': 5, + '六': 6, + '七': 7, + '八': 8, + '九': 9 +} + + +@search_img.handle() +async def _(bot: Bot, event: MessageEvent, state: T_State): + msg = get_message_text(event.json()) + r = re.search('[来要]?(.*)[份发张个次点]图(.*)', msg) + num = r.group(1) + if num in num_key.keys(): + num = num_key[num] + elif is_number(num): + num = int(num) + else: + return + keyword = r.group(2) + params = { + 'search': keyword, + 'r18': 0 + } + async with aiohttp.ClientSession() as session: + exists_id = [] + for _ in range(num): + for _ in range(10): + try: + async with session.get(search_url, timeout=5, params=params) as response: + data = json.loads(await response.text()) + except TimeoutError: + pass + else: + if data['id'] == 'null': + await send_img.finish(f'没有搜索到 {keyword} 的图片...', at_sender=True) + if data['id'] in exists_id: + continue + title = data['title'] + author = data['userName'] + pid = data['id'] + img_url = data['url'] + exists_id.append(pid) + for _ in range(5): + try: + await download_pic(img_url, event.user_id, session) + except TimeoutError: + pass + else: + break + else: + await search_img.finish('图片下载失败...', at_sender=True) + await search_img.send(Message(f'title:{title}\n' + f'pid:{pid}\n' + f'author:{author}\n' + f'{image(f"send_img_{event.user_id}.png", "temp")}')) + break + else: + await search_img.send('图片下载惜败了....', at_sender=True) + logger.info( + f"(USER {event.user_id}, GROUP {event.group_id if event.message_type != 'private' else 'private'})" + f" 发送搜索了 {num} 张 {keyword} 的图片") + + +async def download_pic(img_url: str, user_id: int, session): + async with session.get(img_url, timeout=2) as res: + async with aiofiles.open(f'{IMAGE_PATH}/temp/send_img_{user_id}.png', 'wb') as f: + await f.write(await res.read()) + diff --git a/plugins/send_setu/__init__.py b/plugins/send_setu/__init__.py index 4646eee4..6829c91f 100644 --- a/plugins/send_setu/__init__.py +++ b/plugins/send_setu/__init__.py @@ -3,7 +3,7 @@ from nonebot import on_command, on_regex from nonebot.permission import SUPERUSER from services.log import logger from models.sigin_group_user import SignGroupUser -from util.utils import FreqLimiter, UserExistLimiter, is_number, get_message_text, get_message_imgs +from utils.utils import FreqLimiter, UserExistLimiter, is_number, get_message_text, get_message_imgs from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent, PrivateMessageEvent from .data_source import get_setu, get_luoxiang, search_online_setu, get_setu_urls, \ @@ -154,6 +154,7 @@ async def _(bot: Bot, event: PrivateMessageEvent, state: T_State): urls, text_list, code = await get_setu_urls(keyword, num, r18=r18) except ClientConnectorError: await UserCount.add_count(event.user_id, 'setu_r18', count=-1) + _ulmt.set_False(event.user_id) await setu.finish('网络失败了..别担心!这次搜索不算数喔', at_sender=True) else: count = 0 diff --git a/plugins/send_setu/data_source.py b/plugins/send_setu/data_source.py index 8a507674..ab00f00a 100644 --- a/plugins/send_setu/data_source.py +++ b/plugins/send_setu/data_source.py @@ -1,13 +1,13 @@ from configs.path_config import IMAGE_PATH, TXT_PATH import os import random -from util.init_result import image +from utils.init_result import image from configs.config import LOLICON_KEY import aiohttp import aiofiles from services.log import logger -from util.img_utils import get_img_hash -from util.utils import get_local_proxy, is_number +from utils.img_utils import get_img_hash +from utils.utils import get_local_proxy, is_number from asyncio.exceptions import TimeoutError from models.count_user import UserCount from configs.config import DOWNLOAD_SETU @@ -54,7 +54,8 @@ async def get_setu_urls(keyword: str, num: int = 1, r18: int = 0): if response.status == 200: data = await response.json() if data['code'] == 0: - for i in range(len(data['data'])): + lens = len(data['data']) + for i in range(lens): img_url = data['data'][i]['url'] title = data['data'][i]['title'] author = data['data'][i]['author'] @@ -77,6 +78,7 @@ async def get_setu_urls(keyword: str, num: int = 1, r18: int = 0): if DOWNLOAD_SETU: with open(TXT_PATH + file_name, 'w', encoding='utf8') as f: json.dump(txt_data, f, ensure_ascii=False, indent=4) + num = lens if num > lens else num random_idx = random.sample(range(len(data['data'])), num) x_urls = [] x_text_lst = [] @@ -123,7 +125,6 @@ def get_setu(index: str, setu_data: dict, tag: str = None): index = random.randint(0, length - 1) if tag: all_tag_setu = [x for x in setu_data.keys() if tag in setu_data[x]['tags']] - print(all_tag_setu) if all_tag_setu: index = random.choice(all_tag_setu) if is_number(index): diff --git a/plugins/shop/buy.py b/plugins/shop/buy.py index 4a9b961e..fd5ef8ac 100644 --- a/plugins/shop/buy.py +++ b/plugins/shop/buy.py @@ -2,7 +2,7 @@ from nonebot import on_command from services.log import logger from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.typing import T_State -from util.utils import get_message_text, is_number +from utils.utils import get_message_text, is_number from models.bag_user import BagUser from services.db_context import db from nonebot.adapters.cqhttp.permission import GROUP diff --git a/plugins/shop/gold.py b/plugins/shop/gold.py index ece5df74..5a2ced97 100644 --- a/plugins/shop/gold.py +++ b/plugins/shop/gold.py @@ -2,7 +2,7 @@ from nonebot import on_command from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.typing import T_State from nonebot.adapters.cqhttp.permission import GROUP -from util.data_utils import init_rank +from utils.data_utils import init_rank from models.bag_user import BagUser diff --git a/plugins/shop/gold_redbag/__init__.py b/plugins/shop/gold_redbag/__init__.py index 379c1114..9d07e6e4 100644 --- a/plugins/shop/gold_redbag/__init__.py +++ b/plugins/shop/gold_redbag/__init__.py @@ -5,8 +5,8 @@ from .data_source import check_gold, generate_send_redbag_pic, open_redbag, gene from nonebot.adapters.cqhttp.permission import GROUP from nonebot.message import run_preprocessor, IgnoredException from nonebot.matcher import Matcher -from util.utils import get_message_text, is_number, scheduler -from util.init_result import image +from utils.utils import get_message_text, is_number, scheduler +from utils.init_result import image from services.log import logger from nonebot.permission import SUPERUSER from nonebot.rule import to_me diff --git a/plugins/shop/gold_redbag/data_source.py b/plugins/shop/gold_redbag/data_source.py index 6bb5105f..45d26afe 100644 --- a/plugins/shop/gold_redbag/data_source.py +++ b/plugins/shop/gold_redbag/data_source.py @@ -1,7 +1,7 @@ from models.bag_user import BagUser -from util.utils import is_number, get_local_proxy -from util.img_utils import CreateImg -from util.user_agent import get_user_agent +from utils.utils import is_number, get_local_proxy +from utils.img_utils import CreateImg +from utils.user_agent import get_user_agent from configs.path_config import IMAGE_PATH from models.redbag_user import RedbagUser import random diff --git a/plugins/shop/reset_today_gold.py b/plugins/shop/reset_today_gold.py index 487c4515..589d6672 100644 --- a/plugins/shop/reset_today_gold.py +++ b/plugins/shop/reset_today_gold.py @@ -1,4 +1,4 @@ -from util.utils import scheduler +from utils.utils import scheduler from models.bag_user import BagUser from services.log import logger diff --git a/plugins/shop/shop_handle/__init__.py b/plugins/shop/shop_handle/__init__.py index 8412a0d2..781e3722 100644 --- a/plugins/shop/shop_handle/__init__.py +++ b/plugins/shop/shop_handle/__init__.py @@ -2,10 +2,10 @@ from nonebot import on_command from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent from nonebot.typing import T_State from configs.path_config import IMAGE_PATH -from util.init_result import image +from utils.init_result import image from .data_source import create_shop_help, add_goods, del_goods, update_goods from nonebot.permission import SUPERUSER -from util.utils import get_message_text, is_number +from utils.utils import get_message_text, is_number from services.log import logger import os import time diff --git a/plugins/shop/shop_handle/data_source.py b/plugins/shop/shop_handle/data_source.py index 203ed4c3..2a0ff195 100644 --- a/plugins/shop/shop_handle/data_source.py +++ b/plugins/shop/shop_handle/data_source.py @@ -1,6 +1,6 @@ from models.goods_info import GoodsInfo -from util.img_utils import CreateImg -from util.utils import is_number +from utils.img_utils import CreateImg +from utils.utils import is_number from configs.path_config import IMAGE_PATH import time diff --git a/plugins/shop/use/__init__.py b/plugins/shop/use/__init__.py index 76b683db..69b08398 100644 --- a/plugins/shop/use/__init__.py +++ b/plugins/shop/use/__init__.py @@ -2,7 +2,7 @@ from nonebot import on_command from services.log import logger from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.typing import T_State -from util.utils import is_number, get_message_text +from utils.utils import is_number, get_message_text from models.bag_user import BagUser from nonebot.adapters.cqhttp.permission import GROUP from services.db_context import db diff --git a/plugins/sign_in/__init__.py b/plugins/sign_in/__init__.py index 70dcb346..07d31557 100644 --- a/plugins/sign_in/__init__.py +++ b/plugins/sign_in/__init__.py @@ -1,10 +1,16 @@ -from .group_user_checkin import group_user_check_in, group_user_check, group_impression_rank +from .group_user_checkin import group_user_check_in, group_user_check, group_impression_rank, impression_rank from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, GroupMessageEvent from nonebot.adapters.cqhttp.permission import GROUP -from util.utils import get_message_text -from nonebot.plugin import MatcherGroup - +from utils.init_result import image +from nonebot import on_command +from utils.utils import get_message_text +from pathlib import Path +from configs.path_config import DATA_PATH +try: + import ujson as json +except ModuleNotFoundError: + import json __plugin_name__ = '签到' __plugin_usage__ = ( @@ -13,13 +19,23 @@ __plugin_usage__ = ( '“我的签到” 来获取历史签到信息\n' '“好感度排行” 来查看当前好感度前十的伙伴\n' '/ 签到时有 3% 概率 * 2 /' - ) +_file = Path(f'{DATA_PATH}/not_show_sign_rank_user.json') +try: + data = json.load(open(_file, 'r', encoding='utf8')) +except (FileNotFoundError, ValueError, TypeError): + data = { + '0': [] + } -sign_match_group = MatcherGroup(priority=5, permission=GROUP, block=True) -sign = sign_match_group.on_command("签到") +sign = on_command("签到", priority=5, permission=GROUP, block=True) +my_sign = on_command(cmd="我的签到", aliases={'好感度'}, priority=5, permission=GROUP, block=True) +sign_rank = on_command(cmd="积分排行", aliases={'好感度排行', '签到排行', '积分排行', '好感排行', + '好感度排名,签到排名,积分排名'}, + priority=5, permission=GROUP, block=True) +total_sign_rank = on_command('签到总排行', aliases={'好感度总排行', '好感度总榜', '签到总榜'}, priority=5, block=True) @sign.handle() @@ -30,9 +46,6 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): ) -my_sign = sign_match_group.on_command(cmd="我的签到", aliases={'好感度'}) - - @my_sign.handle() async def _(bot: Bot, event: GroupMessageEvent, state: T_State): await my_sign.send( @@ -40,12 +53,34 @@ async def _(bot: Bot, event: GroupMessageEvent, state: T_State): at_sender=True, ) -sign_ranking = sign_match_group.on_command(cmd="积分排行", aliases={'好感度排行', '签到排行', '积分排行', '好感排行', - '好感度排名,签到排名,积分排名'}) - -@sign_ranking.handle() +@sign_rank.handle() async def _(bot: Bot, event: GroupMessageEvent, state: T_State): - await sign_ranking.send( + await sign_rank.send( await group_impression_rank(event.group_id) - ) \ No newline at end of file + ) + + +@total_sign_rank.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + msg = get_message_text(event.json()) + if not msg: + await total_sign_rank.send('请稍等..正在整理数据...') + await total_sign_rank.send(image(b64=await impression_rank(0, data))) + elif msg in ['屏蔽我']: + if event.user_id in data['0']: + await total_sign_rank.finish('您已经在屏蔽名单中了,请勿重复添加!', at_sender=True) + data['0'].append(event.user_id) + await total_sign_rank.send('设置成功,您不会出现在签到总榜中!', at_sender=True) + elif msg in ['显示我']: + if event.user_id not in data['0']: + await total_sign_rank.finish('您不在屏蔽名单中!', at_sender=True) + data['0'].remove(event.user_id) + await total_sign_rank.send('设置成功,签到总榜将会显示您的头像名称以及好感度!', at_sender=True) + with open(_file, 'w', encoding='utf8') as f: + json.dump(data, f, ensure_ascii=False, indent=4) + + + + + diff --git a/plugins/sign_in/group_user_checkin.py b/plugins/sign_in/group_user_checkin.py index 4d6290f6..2ebed46f 100644 --- a/plugins/sign_in/group_user_checkin.py +++ b/plugins/sign_in/group_user_checkin.py @@ -1,12 +1,17 @@ import random from datetime import datetime, timedelta - +from io import BytesIO from services.log import logger from services.db_context import db from models.sigin_group_user import SignGroupUser from models.group_member_info import GroupInfoUser from models.bag_user import BagUser from configs.config import MAX_SIGN_GOLD +from utils.img_utils import CreateImg +import aiohttp +from asyncio.exceptions import TimeoutError +import math +import asyncio async def group_user_check_in(user_qq: int, group: int) -> str: @@ -82,7 +87,7 @@ async def group_user_check(user_qq: int, group: int) -> str: async def group_impression_rank(group: int) -> str: result = "\t好感度排行榜\t\n" - user_qq_list, impression_list = await SignGroupUser.query_impression_all(group) + user_qq_list, impression_list, _ = await SignGroupUser.query_impression_all(group) _count = 11 if user_qq_list and impression_list: for i in range(1, len(user_qq_list)): @@ -115,4 +120,84 @@ async def random_gold(user_id, group_id, impression): return 0 +async def impression_rank(group_id: int, data: dict): + user_qq_list, impression_list, group_list = await SignGroupUser.query_impression_all(group_id) + users, impressions, groups = [], [], [] + num = 0 + for i in range(105 if len(user_qq_list) > 105 else len(user_qq_list)): + impression = max(impression_list) + index = impression_list.index(impression) + user = user_qq_list[index] + group = group_list[index] + user_qq_list.pop(index) + impression_list.pop(index) + group_list.pop(index) + if user not in users and impression < 100000: + if user not in data['0']: + users.append(user) + impressions.append(impression) + groups.append(group) + else: + num += 1 + for i in range(num): + impression = max(impression_list) + index = impression_list.index(impression) + user = user_qq_list[index] + group = group_list[index] + user_qq_list.pop(index) + impression_list.pop(index) + group_list.pop(index) + if user not in users and impression < 100000: + users.append(user) + impressions.append(impression) + groups.append(group) + return (await asyncio.gather(*[_pst(users, impressions, groups)]))[0] + +async def _pst(users: list, impressions: list, groups: list): + lens = len(users) + count = math.ceil(lens / 33) + width = 10 + idx = 0 + A = CreateImg(1740, 3300, color='#FFE4C4') + async with aiohttp.ClientSession() as session: + for _ in range(count): + col_img = CreateImg(550, 3300, 550, 100, color='#FFE4C4') + for _ in range(33 if int(lens / 33) >= 1 else lens % 33 - 1): + idx += 1 + if idx > 100: + break + impression = max(impressions) + index = impressions.index(impression) + user = users[index] + group = groups[index] + impressions.pop(index) + users.pop(index) + groups.pop(index) + try: + user_name = (await GroupInfoUser.select_member_info(user, group)).user_name + except AttributeError: + user_name = f'我名字呢?' + user_name = user_name if len(user_name) < 11 else user_name[:10] + '...' + impression = str(impression)[:4] if len(str(impression)) > 4 else impression + try: + async with session.get(f'http://q1.qlogo.cn/g?b=qq&nk={user}&s=160', timeout=5) as response: + ava = CreateImg(50, 50, background=BytesIO(await response.read())) + except TimeoutError: + ava = CreateImg(50, 50, color='white') + ava.circle() + bk = CreateImg(550, 100, color='#FFE4C4', font_size=30) + font_w, font_h = bk.getsize(f'{idx}') + bk.text((5, int((100-font_h)/2)), f'{idx}.') + bk.paste(ava, (55, int((100-50)/2)), True) + bk.text((120, int((100-font_h)/2)), f'{user_name}') + bk.text((460, int((100-font_h)/2)), f'[{impression}]') + col_img.paste(bk) + A.paste(col_img, (width, 0)) + lens -= 33 + width += 580 + W = CreateImg(1740, 3700, color='#FFE4C4', font_size=130) + W.paste(A, (0, 260)) + font_w, font_h = W.getsize('真寻的好感度总榜') + W.text((int((1740-font_w)/2), int((260-font_h)/2)), '真寻的好感度总榜') + return W.pic2bs4() diff --git a/plugins/songpicker2/music_163.py b/plugins/songpicker2/music_163.py index f20cd8b9..ce904e04 100644 --- a/plugins/songpicker2/music_163.py +++ b/plugins/songpicker2/music_163.py @@ -1,6 +1,6 @@ import aiohttp import json -from util.utils import get_local_proxy +from utils.utils import get_local_proxy from configs.config import ALAPI_TOKEN from asyncio.exceptions import TimeoutError diff --git a/plugins/statistics_handle.py b/plugins/statistics_handle.py new file mode 100644 index 00000000..75859c7a --- /dev/null +++ b/plugins/statistics_handle.py @@ -0,0 +1,134 @@ +from nonebot import on_command +from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, GROUP +from models.group_info import GroupInfo +from nonebot.typing import T_State +from pathlib import Path +from configs.path_config import DATA_PATH, TTF_PATH +import matplotlib.pyplot as plt +from utils.utils import get_message_text +from utils.img_utils import fig2b64 +from utils.init_result import image +from configs.config import plugins2info_dict +from matplotlib.font_manager import FontProperties + +plt.rcParams['font.family'] = ['SimHei', 'FangSong', 'KaiTi'] +plt.rcParams['font.sans-serif'] = ['SimHei', 'FangSong', 'KaiTi'] +plt.rcParams['axes.unicode_minus'] = False + +font = FontProperties(fname=f'{TTF_PATH}/yz.ttf', size=10) + +try: + import ujson as json +except ModuleNotFoundError: + import json + +__plugin_name__ = '功能调用统计' +__plugin_usage__ = '用法: 无' + +statistics = on_command("功能调用统计", aliases={'日功能调用统计', '周功能调用统计', '月功能调用统计'}, permission=GROUP, priority=5, block=True) + +statistics_file = Path(f'{DATA_PATH}/_prefix_count.json') + + +@statistics.handle() +async def _(bot: Bot, event: GroupMessageEvent, state: T_State): + msg = get_message_text(event.json()) + plugin = '' + if not statistics_file.exists(): + await statistics.finish('统计文件不存在...', at_sender=True) + if state["_prefix"]["raw_command"][0] == '日': + arg = 'day_statistics' + elif state["_prefix"]["raw_command"][0] == '周': + arg = 'week_statistics' + elif state["_prefix"]["raw_command"][0] == '月': + arg = 'month_statistics' + else: + arg = 'total_statistics' + if msg: + for key in plugins2info_dict.keys(): + if msg in plugins2info_dict[key]['cmd']: + plugin = plugins2info_dict[key]['cmd'][0] + break + else: + if arg not in ['day_statistics', 'total_statistics']: + await statistics.finish('未找到此功能的调用...', at_sender=True) + data: dict = json.load(open(statistics_file, 'r', encoding='utf8')) + if not data[arg].get(str(event.group_id)): + await statistics.finish('该群统计数据不存在...', at_sender=True) + day_index = data['day_index'] + data = data[arg][str(event.group_id)] + group_name = await GroupInfo.get_group_info(event.group_id) + group_name = group_name.group_name if group_name else str(event.group_id) + img = generate_statistics_img(data, arg, group_name, plugin, day_index) + await statistics.send(image(b64=img)) + plt.cla() + + +def generate_statistics_img(data: dict, arg: str, group_name: str, plugin: str, day_index: int): + if arg == 'day_statistics': + init_bar_graph(data, f'{group_name} 日功能调用统计') + elif arg == 'week_statistics': + if plugin: + current_week = day_index % 7 + week_lst = [] + if current_week == 0: + week_lst = [1, 2, 3, 4, 5, 6, 7] + else: + for i in range(current_week + 1, 7): + week_lst.append(str(i)) + for i in range(current_week + 1): + week_lst.append(str(i)) + count = [] + for i in range(7): + if int(week_lst[i]) == 7: + count.append(data[str(0)][plugin]) + else: + count.append(data[str(week_lst[i])][plugin]) + week_lst = ['7' if i == '0' else i for i in week_lst] + plt.plot(week_lst, count) + plt.title(f'{group_name} 周 {plugin} 功能调用统计【为7天统计】', FontProperties=font) + else: + init_bar_graph(update_data(data), f'{group_name} 周功能调用统计【为7天统计】') + elif arg == 'month_statistics': + if plugin: + day_index = day_index % 30 + day_lst = [] + for i in range(day_index + 1, 30): + day_lst.append(i) + for i in range(day_index + 1): + day_lst.append(i) + count = [data[str(day_lst[i])][plugin] for i in range(30)] + day_lst = [str(x + 1) for x in day_lst] + plt.title(f'{group_name} 月 {plugin} 功能调用统计【为30天统计】', FontProperties=font) + plt.plot(day_lst, count) + else: + init_bar_graph(update_data(data), f'{group_name} 月功能调用统计【为30天统计】') + elif arg == 'total_statistics': + init_bar_graph(data, f'{group_name} 功能调用统计') + + return fig2b64(plt) + + +def init_bar_graph(data: dict, title: str, ha: str = 'left', va: str = 'center'): + plt.tick_params(axis='y', labelsize=7) + tmp_x = list(data.keys()) + tmp_y = list(data.values()) + x = [tmp_x[i] for i in range(len(tmp_y)) if tmp_y[i]] + y = [tmp_y[i] for i in range(len(tmp_y)) if tmp_y[i]] + plt.barh(x, y) + plt.title(f'{title}', FontProperties=font) + for y, x in zip(y, x): + plt.text(y, x, s=str(y), ha=ha, va=va, fontsize=8) + + +def update_data(data: dict): + tmp_dict = {} + for day in data.keys(): + for plugin_name in data[day].keys(): + # print(f'{day}:{plugin_name} = {data[day][plugin_name]}') + if data[day][plugin_name] is not None: + if tmp_dict.get(plugin_name) is None: + tmp_dict[plugin_name] = 1 + else: + tmp_dict[plugin_name] += data[day][plugin_name] + return tmp_dict diff --git a/plugins/super_cmd/__init__.py b/plugins/super_cmd/__init__.py index 6effccd9..f6a4098a 100644 --- a/plugins/super_cmd/__init__.py +++ b/plugins/super_cmd/__init__.py @@ -4,12 +4,12 @@ from models.level_user import LevelUser from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, Event, MessageEvent, Message from nonebot.rule import to_me -from util.utils import get_message_at, get_message_text, is_number, get_bot +from utils.utils import get_message_at, get_message_text, is_number, get_bot from services.log import logger from .data_source import open_remind, close_remind from models.group_info import GroupInfo from models.friend_user import FriendUser -from util.init_result import at +from utils.init_result import at __plugin_name__ = '超级用户指令 [Hidden]' @@ -65,8 +65,9 @@ async def _(bot: Bot, event: MessageEvent, state: T_State): if flag == 2: await super_cmd.send(result) elif flag == 1: - await bot.send_group_msg(group_id=group_id, message=Message(f'{at(qq)}管理员已为你修改' + await bot.send_group_msg(group_id=group_id, message=Message(f'{at(qq)}管理员修改了你的权限' f'权限\n--------\n你当前的权限等级:{level}')) + await super_cmd.send('修改成功') except Exception as e: await super_cmd.send("执行指令失败!") logger.error(f'执行指令失败 e{e}') diff --git a/plugins/super_help/__init__.py b/plugins/super_help/__init__.py index bfcd5318..f2a8c613 100644 --- a/plugins/super_help/__init__.py +++ b/plugins/super_help/__init__.py @@ -11,22 +11,23 @@ super_help = on_command("超级用户帮助", rule=to_me(), priority=1, permissi @super_help.handle() async def _(bot: Bot, event: Event, state: T_State): result = '''超级用户帮助: - 1.添加/删除管理 + 1.添加/删除管理 [at] [level] 2.查看群组/查看好友 - 3.广播 --> 指令:广播- + 3.广播 --> 指令:广播- [msg] 4.更新色图 - 5.回复 --> 指令:/t 用户 群号 + 5.回复 --> 指令:/t命令帮助 6.更新cookie --> 指令:更新cookie [cookie] 7.开启广播通知 --> 指令:开启广播通知 [群号] - 8.退群 --> 指令:退群 群号 + 8.退群 --> 指令:退群 [群号] 9.自检 10.更新好友信息 11.更新群群信息 - 12.重载原神/方舟卡池 + 12.重载原神/方舟/赛马娘/坎公骑冠剑卡池 13.添加商品 [名称] [价格] [描述] [折扣] [限时时间] 14.删除商品 [名称(序号)] 15.修改商品 -name [名称(序号)] -price [价格] -des [描述] -discount [折扣] -time [限时] - 16.节日红包 [金额] [数量] [祝福语](可省) [指定群](可省) [指定群]...''' + 16.节日红包 [金额] [数量] [祝福语](可省) [指定群](可省) [指定群]... + 17.更新原神今日素材''' await super_help.finish(result, at_sender=True) diff --git a/plugins/test.py b/plugins/test.py index b4553452..fdaa3abc 100644 --- a/plugins/test.py +++ b/plugins/test.py @@ -1,5 +1,6 @@ from nonebot.rule import to_me from nonebot.typing import T_State +import base64 from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, MessageEvent from nonebot import on_command, on_keyword, on_metaevent from nonebot.plugin import MatcherGroup @@ -8,8 +9,8 @@ import nonebot from models.open_cases_user import OpenCasesUser from tzlocal import get_localzone from datetime import datetime, timezone, timedelta -from util.utils import get_bot, get_message_imgs, get_local_proxy -from util.init_result import * +from utils.utils import get_bot, get_message_imgs, get_local_proxy +from utils.init_result import * from nonebot.adapters.cqhttp.message import MessageSegment import requests import aiohttp @@ -17,28 +18,37 @@ from models.bag_user import BagUser from nonebot.adapters.cqhttp.message import Message import asyncio from models.group_member_info import GroupInfoUser - +from matplotlib import font_manager +from matplotlib import pyplot as plt +import matplotlib as mpl +from utils.utils import scheduler +from utils.browser import get_browser +from playwright.async_api import async_playwright, Browser +from typing import Optional +import playwright # erm = on_command('异世相遇,尽享美味', aliases={'异世相遇 尽享美味', '异世相遇,尽享美味'}, priority=5, block=True) -# matcher = on_keyword({"test"}) -# -# -# @matcher.handle() -# async def _(bot: Bot, event: MessageEvent, state: T_State): -# songContent = [ -# { -# "type": "music", -# "data": { -# "type": "custom", -# "subtype": "kugou", -# "url": "https://webfs.yun.kugou.com/", -# "audio": "https://webfs.yun.kugou.com/202106231924/77ad13a9c9c091f74aa10098281433ab/G128/M02/02/00/wA0DAFplVJ6AIcIiADK1ZUYugV4043.mp3", -# "title": "音乐标题" -# } -# } -# ] -# await matcher.send(songContent) +matcher = on_keyword({"test"}) + + +@matcher.handle() +async def _(bot: Bot, event: MessageEvent, state: T_State): + browser = await get_browser() + url = 'https://genshin.pub/daily' + # try: + page = await browser.new_page() + await page.goto(url) + await page.set_viewport_size({"width": 2560, "height": 1080}) + await page.click("button") + card = await page.query_selector(".GSContainer_inner_border_box__21_vs") + clip = await card.bounding_box() + await page.screenshot(path=f'xxxx.png', clip=clip) + await page.close() + # except Exception: + if page: + await page.close() + # raise # @erm.handle() # async def first_receive(bot: Bot, event: Event, state: T_State): @@ -47,4 +57,3 @@ from models.group_member_info import GroupInfoUser - diff --git a/plugins/translate/__init__.py b/plugins/translate/__init__.py index b51748b8..ed7afb5d 100644 --- a/plugins/translate/__init__.py +++ b/plugins/translate/__init__.py @@ -1,5 +1,5 @@ from nonebot import on_command -from util.utils import get_message_text +from utils.utils import get_message_text from services.log import logger from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State diff --git a/plugins/translate/data_source.py b/plugins/translate/data_source.py index 1af00053..6bdd3d11 100644 --- a/plugins/translate/data_source.py +++ b/plugins/translate/data_source.py @@ -1,6 +1,6 @@ import aiohttp -from util.utils import get_local_proxy -from util.user_agent import get_user_agent +from utils.utils import get_local_proxy +from utils.user_agent import get_user_agent url = f'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null' diff --git a/plugins/update_gocqhttp/__init__.py b/plugins/update_gocqhttp/__init__.py index bfc6826b..6c5a01ae 100644 --- a/plugins/update_gocqhttp/__init__.py +++ b/plugins/update_gocqhttp/__init__.py @@ -5,7 +5,7 @@ from .data_source import download_gocq_lasted, upload_gocq_lasted import os from nonebot.adapters.cqhttp.permission import GROUP from services.log import logger -from util.utils import scheduler, get_bot, UserExistLimiter +from utils.utils import scheduler, get_bot, UserExistLimiter from configs.config import UPDATE_GOCQ_GROUP from pathlib import Path diff --git a/plugins/update_gocqhttp/data_source.py b/plugins/update_gocqhttp/data_source.py index f49f82bb..b17fe46f 100644 --- a/plugins/update_gocqhttp/data_source.py +++ b/plugins/update_gocqhttp/data_source.py @@ -1,6 +1,6 @@ import aiohttp -from util.utils import get_local_proxy, get_bot -from util.user_agent import get_user_agent +from utils.utils import get_local_proxy, get_bot +from utils.user_agent import get_user_agent import asyncio import platform import aiofiles diff --git a/plugins/update_info.py b/plugins/update_info.py index 13f2f006..5d2e744b 100644 --- a/plugins/update_info.py +++ b/plugins/update_info.py @@ -1,7 +1,7 @@ from nonebot import on_command from nonebot.adapters.cqhttp import Bot, Event from nonebot.typing import T_State -from util.init_result import image +from utils.init_result import image __plugin_name__ = '更新信息' diff --git a/plugins/update_pic.py b/plugins/update_pic.py index 04fa5580..83764149 100644 --- a/plugins/update_pic.py +++ b/plugins/update_pic.py @@ -1,16 +1,16 @@ from nonebot import on_command from PIL import Image, ImageFilter -from util.init_result import image +from utils.init_result import image from configs.path_config import IMAGE_PATH from services.log import logger from nonebot.rule import to_me from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot.typing import T_State -from util.utils import get_message_imgs +from utils.utils import get_message_imgs import aiofiles import aiohttp -from util.utils import is_number, get_message_text -from util.img_utils import CreateImg, pic2b64 +from utils.utils import is_number, get_message_text +from utils.img_utils import CreateImg, pic2b64 import cv2 import numpy as np diff --git a/plugins/update_setu/__init__.py b/plugins/update_setu/__init__.py index 662085dc..99a26bf1 100644 --- a/plugins/update_setu/__init__.py +++ b/plugins/update_setu/__init__.py @@ -1,4 +1,4 @@ -from util.utils import scheduler +from utils.utils import scheduler from nonebot import on_command from nonebot.permission import SUPERUSER from nonebot.typing import T_State diff --git a/plugins/update_setu/data_source.py b/plugins/update_setu/data_source.py index 621d4e39..53b77ca1 100644 --- a/plugins/update_setu/data_source.py +++ b/plugins/update_setu/data_source.py @@ -1,10 +1,10 @@ from configs.path_config import IMAGE_PATH, TXT_PATH import os -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent from services.log import logger from datetime import datetime -from util.img_utils import rar_imgs, get_img_hash -from util.utils import get_bot, get_local_proxy +from utils.img_utils import rar_imgs, get_img_hash +from utils.utils import get_bot, get_local_proxy from asyncio.exceptions import TimeoutError import aiofiles import aiohttp diff --git a/plugins/upload_img/__init__.py b/plugins/upload_img/__init__.py index 647805ca..f5b36c13 100644 --- a/plugins/upload_img/__init__.py +++ b/plugins/upload_img/__init__.py @@ -5,10 +5,10 @@ import os from nonebot.rule import to_me from nonebot.typing import T_State from nonebot.adapters import Bot, Event -from util.utils import get_message_imgs, get_message_text +from utils.utils import get_message_imgs, get_message_text import aiohttp import aiofiles -from util.utils import cn2py +from utils.utils import cn2py from configs.config import IMAGE_DIR_LIST __plugin_name__ = '上传图片' diff --git a/plugins/weather/__init__.py b/plugins/weather/__init__.py index ee3f22ae..0b1ed3ce 100644 --- a/plugins/weather/__init__.py +++ b/plugins/weather/__init__.py @@ -6,7 +6,7 @@ from services.log import logger from nonebot.typing import T_State from .config import city_list import re -from util.utils import get_message_text +from utils.utils import get_message_text __plugin_name__ = '天气查询' __plugin_usage__ = "普普通通的查天气吧\n示例:北京天气" diff --git a/plugins/weather/data_source.py b/plugins/weather/data_source.py index e806839d..57f19871 100644 --- a/plugins/weather/data_source.py +++ b/plugins/weather/data_source.py @@ -1,5 +1,5 @@ import requests -from util.init_result import image +from utils.init_result import image async def get_weather_of_city(city) -> str: diff --git a/plugins/what_anime/__init__.py b/plugins/what_anime/__init__.py index 2fe601eb..95fda9d7 100644 --- a/plugins/what_anime/__init__.py +++ b/plugins/what_anime/__init__.py @@ -3,9 +3,9 @@ from .data_source import get_anime from nonebot import on_command from nonebot.typing import T_State from nonebot.adapters import Bot, Event -from util.utils import get_message_imgs +from utils.utils import get_message_imgs from services.log import logger -from util.utils import UserExistLimiter +from utils.utils import UserExistLimiter __plugin_name__ = '识番' diff --git a/plugins/what_anime/data_source.py b/plugins/what_anime/data_source.py index b08f2c0c..6d968781 100644 --- a/plugins/what_anime/data_source.py +++ b/plugins/what_anime/data_source.py @@ -1,9 +1,9 @@ import time from services.log import logger -from util.langconv import * +from utils.langconv import * import aiohttp -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent async def get_anime(anime: str) -> str: diff --git a/plugins/white2black_img.py b/plugins/white2black_img.py index 9e51ecf0..b33e5972 100644 --- a/plugins/white2black_img.py +++ b/plugins/white2black_img.py @@ -1,13 +1,13 @@ from nonebot.typing import T_State from nonebot.adapters.cqhttp import Bot, MessageEvent from nonebot import on_command -from util.utils import get_message_imgs, get_local_proxy, get_message_text, is_Chinese -from util.init_result import image +from utils.utils import get_message_imgs, get_local_proxy, get_message_text, is_Chinese +from utils.init_result import image import aiohttp import aiofiles from configs.path_config import IMAGE_PATH -from util.img_utils import CreateImg -from util.user_agent import get_user_agent +from utils.img_utils import CreateImg +from utils.user_agent import get_user_agent from services.log import logger # ZH_CN2EN 中文 » 英语 diff --git a/plugins/yiqing/__init__.py b/plugins/yiqing/__init__.py index 556ce221..5b8a3db4 100644 --- a/plugins/yiqing/__init__.py +++ b/plugins/yiqing/__init__.py @@ -4,7 +4,7 @@ from services.log import logger from nonebot.adapters.cqhttp import Bot, Event from nonebot.typing import T_State from .config import city_list -from util.utils import scheduler +from utils.utils import scheduler __plugin_name__ = '疫情查询' __plugin_usage__ = '查询疫情帮助:\n\t对我说 查询疫情 省份/城市,我会回复疫情的实时数据\n\t示例: 查询疫情 温州' diff --git a/plugins/yiqing/data_source.py b/plugins/yiqing/data_source.py index 90df0733..7c68e28a 100644 --- a/plugins/yiqing/data_source.py +++ b/plugins/yiqing/data_source.py @@ -1,10 +1,10 @@ from datetime import datetime import aiohttp -from util.user_agent import get_user_agent +from utils.user_agent import get_user_agent import json import os from configs.path_config import TXT_PATH -from util.utils import get_local_proxy +from utils.utils import get_local_proxy url = "https://api.yimian.xyz/coro/" diff --git a/utils/browser.py b/utils/browser.py new file mode 100644 index 00000000..f2d6de8b --- /dev/null +++ b/utils/browser.py @@ -0,0 +1,59 @@ +from typing import Optional +import shutil +from nonebot.log import logger +from playwright.async_api import Browser, async_playwright +import nonebot +from nonebot import Driver +from appdirs import AppDirs +from pathlib import Path +from services.log import logger + + +driver: Driver = nonebot.get_driver() + + +_browser: Optional[Browser] = None + + +async def init(**kwargs) -> Optional[Browser]: + try: + global _browser + browser = await async_playwright().start() + _browser = await browser.chromium.launch(**kwargs) + return _browser + except NotImplemented: + logger.warning('win环境下 初始化playwright失败....请替换环境至linux') + return None + + +async def get_browser(**kwargs) -> Browser: + return _browser or await init(**kwargs) + + +@driver.on_startup +def install(): + """自动安装、更新 Chromium""" + logger.info("正在检查 Chromium 更新") + import sys + from playwright.__main__ import main + sys.argv = ['', 'install', 'chromium'] + main() + + +@driver.on_startup +def delete_pyppeteer(): + """删除 Pyppeteer 遗留的 Chromium""" + + dir = Path(AppDirs('pyppeteer').user_data_dir) + if not dir.exists(): + return + + # if not config.haruka_delete_pyppeteer: + # logger.info("检测到 Pyppeteer 依赖(约 300 M)," + # "新版 HarukaBot 已经不需要这些文件了。" + # "如果没有其他程序依赖 Pyppeteer,请在 '.env.*' 中设置" + # " 'HARUKA_DELETE_PYPPETEER=True' 并重启 Bot 后,将自动清除残留") + # else: + shutil.rmtree(dir) + logger.info("已清理 Pyppeteer 依赖残留") + diff --git a/utils/data_utils.py b/utils/data_utils.py new file mode 100644 index 00000000..903c2d6e --- /dev/null +++ b/utils/data_utils.py @@ -0,0 +1,24 @@ +from models.group_member_info import GroupInfoUser + + +# 生成通用排行榜 +async def init_rank(users: list, all_user_data: list, group_id: int): + all_user_id = [user.user_qq for user in users] + rst = '' + for i in range(len(all_user_id) if len(all_user_id) < 10 else 10): + _max = max(all_user_data) + max_user_id = all_user_id[all_user_data.index(_max)] + all_user_id.remove(max_user_id) + all_user_data.remove(_max) + try: + user_name = (await GroupInfoUser.select_member_info(max_user_id, group_id)).user_name + except AttributeError: + user_name = f'{max_user_id}' + rst += f'{user_name}: {_max}\n' + return rst[:-1] + + + + + + diff --git a/utils/img_utils.py b/utils/img_utils.py new file mode 100644 index 00000000..d1491408 --- /dev/null +++ b/utils/img_utils.py @@ -0,0 +1,326 @@ +import os +from configs.path_config import IMAGE_PATH, TXT_PATH, TTF_PATH +from PIL import Image, ImageFile, ImageDraw, ImageFont +import cv2 +import imagehash +import base64 +from io import BytesIO +from matplotlib import pyplot as plt + + +# 扫描图库id是否连贯 +def scan_img(path): + path = IMAGE_PATH + path + nolist = [] + length = len(os.listdir(path)) + print(length) + for i in range(length): + if i in nolist: + continue + img_path = path + "{}.jpg".format(i) + if not os.path.exists(img_path): + print("不存在=== " + str(length) + ".jpg -------> " + str(i) + ".jpg") + os.rename(path + "{}.jpg".format(length - 1), img_path) + nolist.append(length) + length -= 1 + + +# 比较hash值 +def compare_image_with_hash(image_file1, image_file2, max_dif=1.5): + """ + max_dif: 允许最大hash差值, 越小越精确,最小为0 + 推荐使用 + """ + ImageFile.LOAD_TRUNCATED_IMAGES = True + hash_1 = None + hash_2 = None + hash_1 = get_img_hash(image_file1) + hash_2 = get_img_hash(image_file2) + dif = hash_1 - hash_2 + if dif < 0: + dif = -dif + if dif <= max_dif: + return True + else: + return False + + +# 比较图片与hash值 +def compare_one_img_hash(image_file, hash_2, max_dif=1.5): + hash_1 = get_img_hash(image_file) + dif = hash_1 - hash_2 + if dif < 0: + dif = -dif + if dif <= max_dif: + return True + else: + return False + + +def get_img_hash(image_file): + with open(image_file, 'rb') as fp: + hash_value = imagehash.average_hash(Image.open(fp)) + return hash_value + + +# 压缩图片 +def rar_imgs(inpath, outpath, ratio=0.9, start=0, end=0, lens=0, maxsize=0.0, in_file_name='', out_file_name='', + itype='jpg'): + in_path = IMAGE_PATH + inpath + '/' + out_path = IMAGE_PATH + outpath + '/' + # scan_img(inpath) + l = [] + if in_file_name != '' and out_file_name != '': + filein = in_path + in_file_name + "." + itype + fileout = out_path + out_file_name + "." + itype + h, w, d = cv2.imread(filein).shape + width = int(w * ratio) + height = int(h * ratio) + ResizeImage(filein, fileout, width, height) + else: + if lens == 0: + lens = len(os.listdir(in_path)) + if end == 0: + end = lens + for i in range(start, end): + if i in l: + continue + if maxsize != 0: + if os.path.getsize(in_path + str(i) + ".jpg") > maxsize: + print("压缩----->", i, ".jpg") + filein = in_path + str(i) + ".jpg" + fileout = out_path + str(i) + ".jpg" + h, w, d = cv2.imread(filein).shape + width = int(w * ratio) + height = int(h * ratio) + ResizeImage(filein, fileout, width, height) + else: + continue + else: + print("压缩----->", i, ".jpg") + filein = in_path + str(i) + ".jpg" + fileout = out_path + str(i) + ".jpg" + h, w, d = cv2.imread(filein).shape + width = int(w * ratio) + height = int(h * ratio) + ResizeImage(filein, fileout, width, height) + + +# 压缩 +def ResizeImage(filein, fileout, width, height): + img = cv2.resize(cv2.imread(filein), (int(width), int(height))) + cv2.imwrite(fileout, img) + + +# 保存图片压缩后的hash值 +def save_img_hash(path, name): + for file in os.listdir(IMAGE_PATH + path): + if os.path.getsize(IMAGE_PATH + path + file) > 1024 * 1024 * 1.5: + compare_img_hash_in_txt(IMAGE_PATH + 'rar/' + file, name) + else: + compare_img_hash_in_txt(IMAGE_PATH + path + file, name) + + +# 比较色图hash值 +def compare_img_hash_in_txt(file, name, mode=1): + with open(TXT_PATH + name + ".txt", 'a+') as txtfile: + txtfile.seek(0) + hash_list = txtfile.read()[:-1].strip(",") + txtfile.seek(2) + with open(file, 'rb') as fp: + img_hash = str(imagehash.average_hash(Image.open(fp))) + if img_hash not in hash_list: + if mode == 1: + txtfile.write(img_hash + ",") + return False + return True + + +# 透明背景 -> 白色 +def alphabg2white_PIL(img): + img = img.convert('RGBA') + sp = img.size + width = sp[0] + height = sp[1] + for yh in range(height): + for xw in range(width): + dot = (xw, yh) + color_d = img.getpixel(dot) + if color_d[3] == 0: + color_d = (255, 255, 255, 255) + img.putpixel(dot, color_d) + return img + + +def pic2b64(pic: Image) -> str: + buf = BytesIO() + pic.save(buf, format='PNG') + base64_str = base64.b64encode(buf.getvalue()).decode() + return 'base64://' + base64_str + + +def fig2b64(plt: plt) -> str: + buf = BytesIO() + plt.savefig(buf, format='PNG', dpi=100) + base64_str = base64.b64encode(buf.getvalue()).decode() + return 'base64://' + base64_str + + +class CreateImg: + def __init__(self, + w, + h, + img_w=0, + img_h=0, + color='white', + image_type='RGBA', + font_size=10, + background='', + ttf='yz.ttf', + divisor=1): + self.w = int(w) + self.h = int(h) + self.img_w = int(img_w) + self.img_h = int(img_h) + self.current_w = 0 + self.current_h = 0 + self.ttfont = ImageFont.truetype(TTF_PATH + ttf, int(font_size)) + if not background: + self.markImg = Image.new(image_type, (self.w, self.h), color) + else: + if w == 0 and h == 0: + self.markImg = Image.open(background) + w, h = self.markImg.size + if divisor: + self.w = int(divisor * w) + self.h = int(divisor * h) + self.markImg = self.markImg.resize((self.w, self.h), Image.ANTIALIAS) + else: + self.w = w + self.h = h + else: + self.markImg = Image.open(background).resize((self.w, self.h), Image.ANTIALIAS) + self.draw = ImageDraw.Draw(self.markImg) + self.size = self.w, self.h + + # 贴图 + def paste(self, img, pos=None, alpha=False): + if isinstance(img, CreateImg): + img = img.markImg + if self.current_w == self.w: + self.current_w = 0 + self.current_h += self.img_h + if not pos: + pos = (self.current_w, self.current_h) + if alpha: + try: + self.markImg.paste(img, pos, img) + except ValueError: + img = img.convert("RGBA") + self.markImg.paste(img, pos, img) + else: + self.markImg.paste(img, pos) + self.current_w += self.img_w + return self.markImg + + # 获取文字大小 + def getsize(self, msg): + return self.ttfont.getsize(msg) + + # 写字 + def text(self, pos, text, fill=(0, 0, 0)): + self.draw.text(pos, text, fill=fill, font=self.ttfont) + return self.markImg + + # 饼图 + def pieslice(self): + self.draw.pieslice((350, 50, 500, 200), -150, -30, 'pink', 'crimson') + return self.markImg + + # 保存 + def save(self, path): + self.markImg.save(path) + + # 显示 + def show(self): + self.markImg.show(self.markImg) + + # 压缩 + def resize(self, ratio=0, w=0, h=0): + if not w and not h and not ratio: + raise Exception('缺少参数...') + if not w and not h and ratio: + w = int(self.w * ratio) + h = int(self.h * ratio) + self.markImg = self.markImg.resize((w, h), Image.ANTIALIAS) + self.w, self.h = self.markImg.size + self.size = self.w, self.h + self.draw = ImageDraw.Draw(self.markImg) + + # 检查字体大小 + def check_font_size(self, word): + return self.ttfont.getsize(word)[0] > self.w + + # 透明化 + def transparent(self, n=0): + self.markImg = self.markImg.convert('RGBA') # 修改颜色通道为RGBA + x, y = self.markImg.size # 获得长和宽 + # 设置每个像素点颜色的透明度 + for i in range(n, x - n): + for k in range(n, y - n): + color = self.markImg.getpixel((i, k)) + color = color[:-1] + (100, ) + self.markImg.putpixel((i, k), color) + return self.markImg + + # 转bs4: + def pic2bs4(self): + buf = BytesIO() + self.markImg.save(buf, format='PNG') + base64_str = base64.b64encode(buf.getvalue()).decode() + return base64_str + + # + def convert(self, itype): + self.markImg = self.markImg.convert(itype) + + # 变圆 + def circle(self): + self.convert('RGBA') + r2 = min(self.w, self.h) + if self.w != self.h: + self.resize(w=r2, h=r2) + r3 = int(r2 / 2) + imb = Image.new('RGBA', (r3 * 2, r3 * 2), (255, 255, 255, 0)) + pima = self.markImg.load() # 像素的访问对象 + pimb = imb.load() + r = float(r2 / 2) + for i in range(r2): + for j in range(r2): + lx = abs(i - r) # 到圆心距离的横坐标 + ly = abs(j - r) # 到圆心距离的纵坐标 + l = (pow(lx, 2) + pow(ly, 2)) ** 0.5 # 三角函数 半径 + if l < r3: + pimb[i - (r - r3), j - (r - r3)] = pima[i, j] + self.markImg = imb + + # + def getchannel(self, itype): + self.markImg = self.markImg.getchannel(itype) + + + +if __name__ == '__main__': + pass + + + + + + + + + + + + diff --git a/utils/init_result.py b/utils/init_result.py new file mode 100644 index 00000000..92d4b0d0 --- /dev/null +++ b/utils/init_result.py @@ -0,0 +1,88 @@ +from configs.path_config import IMAGE_PATH, VOICE_PATH +from nonebot.adapters.cqhttp.message import MessageSegment +import os +from services.log import logger +import ujson + + +def image(img_name: str = None, path: str = '', abspath: str = None, b64: str = None): + if abspath: + if os.path.exists(abspath): + return MessageSegment.image("file:///" + abspath) + else: + return '' + elif b64: + if b64.find('base64://') != -1: + return MessageSegment.image(b64) + else: + return MessageSegment.image('base64://' + b64) + else: + img_name = str(img_name) + if img_name.find('http') == -1: + if len(img_name.split('.')) == 1: + img_name += '.jpg' + if os.path.exists(IMAGE_PATH + path + '/' + img_name): + return MessageSegment.image("file:///" + IMAGE_PATH + path + '/' + img_name) + else: + logger.warning(f"图片 {path}/{img_name}缺失.") + return '' + else: + return MessageSegment.image(img_name) + + +def at(qq): + return MessageSegment.at(qq) + + +def record(voice_name='', path=''): + if len(voice_name.split('.')) == 1: + voice_name += '.mp3' + if path == "": + name = VOICE_PATH + "{}.".format(voice_name) + else: + name = VOICE_PATH + "{}/{}".format(path, voice_name) + if voice_name.find('http') == -1: + if os.path.exists(name): + result = MessageSegment.record("file:///" + name) + return result + else: + logger.warning(f"语音{path}/{voice_name}缺失...") + return "" + else: + return MessageSegment.record(voice_name) + + +def text(msg): + return MessageSegment.text(msg) + + +def contact_user(qq): + return MessageSegment.contact_user(qq) + + +def share(url, title, content='', image_url=''): + return MessageSegment.share(url, title, content, image_url) + + +def xml(data): + return MessageSegment.xml(data) + + +def json(data): + data = ujson.dumps(data) + return MessageSegment.json(data) + + +def face(id_): + return MessageSegment.face(id_) + + +def poke(qq): + return MessageSegment('poke', {"qq": qq}) + + +def forward(): + return MessageSegment.forward() + +# if __name__ == '__main__': +# print(get_record_result("dadada", "", type="amr")) diff --git a/utils/langconv.py b/utils/langconv.py new file mode 100644 index 00000000..4977161b --- /dev/null +++ b/utils/langconv.py @@ -0,0 +1,274 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from copy import deepcopy +import re + +try: + import psyco + psyco.full() +except: + pass + +from .zh_wiki import zh2Hant, zh2Hans + +import sys +py3k = sys.version_info >= (3, 0, 0) + +if py3k: + UEMPTY = '' +else: + _zh2Hant, _zh2Hans = {}, {} + for old, new in ((zh2Hant, _zh2Hant), (zh2Hans, _zh2Hans)): + for k, v in old.items(): + new[k.decode('utf8')] = v.decode('utf8') + zh2Hant = _zh2Hant + zh2Hans = _zh2Hans + UEMPTY = ''.decode('utf8') + +# states +(START, END, FAIL, WAIT_TAIL) = list(range(4)) +# conditions +(TAIL, ERROR, MATCHED_SWITCH, UNMATCHED_SWITCH, CONNECTOR) = list(range(5)) + +MAPS = {} + +class Node(object): + def __init__(self, from_word, to_word=None, is_tail=True, + have_child=False): + self.from_word = from_word + if to_word is None: + self.to_word = from_word + self.data = (is_tail, have_child, from_word) + self.is_original = True + else: + self.to_word = to_word or from_word + self.data = (is_tail, have_child, to_word) + self.is_original = False + self.is_tail = is_tail + self.have_child = have_child + + def is_original_long_word(self): + return self.is_original and len(self.from_word)>1 + + def is_follow(self, chars): + return chars != self.from_word[:-1] + + def __str__(self): + return '