diff options
author | Kyomotoi <[email protected]> | 2021-01-27 21:47:58 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2021-01-27 21:47:58 +0800 |
commit | eb52fab79ada7efe6191e3a5f90179766feaded0 (patch) | |
tree | ad6e513148203a6279ce8c72e9fb26fd7b415f95 | |
parent | b0b3a8c468b612502aea700fe59d9c885adf83dc (diff) | |
download | ATRI-eb52fab79ada7efe6191e3a5f90179766feaded0.tar.gz ATRI-eb52fab79ada7efe6191e3a5f90179766feaded0.tar.bz2 ATRI-eb52fab79ada7efe6191e3a5f90179766feaded0.zip |
[Update]
25 files changed, 195 insertions, 253 deletions
diff --git a/ATRI/__init__.py b/ATRI/__init__.py index 23d4d2b..bfd6133 100644 --- a/ATRI/__init__.py +++ b/ATRI/__init__.py @@ -7,16 +7,17 @@ @Author : Kyomotoi @Contact : [email protected] @Github : https://github.com/Kyomotoi -@License : Copyright © 2018-2020 Kyomotoi, All Rights Reserved. +@License : Copyright © 2021 Kyomotoi, All Rights Reserved. ''' __author__ = 'kyomotoi' +import time import shutil from pathlib import Path from .log import logger -from .config import check_config -from .service.send import Send +from .config import check_config, RUNTIME_CONFIG +from .service.httppost import HttpPost from main import driver @@ -29,15 +30,31 @@ async def startup_event() -> None: @driver.on_shutdown async def shutdown_event() -> None: - logger.info('Thanks for using!') + logger.info('Thanks for using.') + logger.debug('bot已关闭,正在清理插件信息...') try: shutil.rmtree(PLUGIN_INFO_PATH) except: + repo = ('清理插件信息失败', + '请前往 ATRI/data/service 下', + '将 plugins 整个文件夹删除') + logger.error(repo) + time.sleep(10) pass @driver.on_bot_connect -async def az(bot): - await Send.send_to_superuser('test') +async def connect(bot): + for super in RUNTIME_CONFIG['superusers']: + await HttpPost.send_private_msg( + int(super), 'WebSocket 成功连接,数据开始传输~!') + [email protected]_bot_disconnect +async def disconnect(bot): + for super in RUNTIME_CONFIG['superusers']: + try: + await HttpPost.send_private_msg(int(super), 'WebSocket 貌似断开了呢...') + except: + logger.error('WebSocket 已断开,等待重连') from .exceptions import Error as Error diff --git a/ATRI/config/setu.plguin.yml b/ATRI/config/setu.plguin.yml index 1ba4aac..51ac882 100644 --- a/ATRI/config/setu.plguin.yml +++ b/ATRI/config/setu.plguin.yml @@ -9,4 +9,7 @@ setu: near_r18: 'near_r18.db' r18: 'r18' repo: - waiting: '别急,在找了!'
\ No newline at end of file + waiting: '别急,在找了!' + +admin: + command: ['/setu', '.setu']
\ No newline at end of file diff --git a/ATRI/data/service/plugins.service.json b/ATRI/data/service/plugins.service.json deleted file mode 100644 index e69de29..0000000 --- a/ATRI/data/service/plugins.service.json +++ /dev/null diff --git a/ATRI/data/service/plugins/call_me.plugins.json b/ATRI/data/service/plugins/call_me.plugins.json deleted file mode 100644 index 70a9437..0000000 --- a/ATRI/data/service/plugins/call_me.plugins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "call_me", - "docs": "\u6b63\u786e\u5730\u547c\u53eb\u54b1", - "command": [ - "Atri", - "ATRI", - "atri", - "\u4e9a\u6258\u8389", - "\u30a2\u30c8\u30ea" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/call_robot.plugins.json b/ATRI/data/service/plugins/call_robot.plugins.json deleted file mode 100644 index 54ffd07..0000000 --- a/ATRI/data/service/plugins/call_robot.plugins.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "call_robot", - "docs": "\u4f7f\u7528\u6b63\u5219\u89e6\u53d1", - "command": [ - "\u841d\u535c\u5b50" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/cant_do_anything.plugins.json b/ATRI/data/service/plugins/cant_do_anything.plugins.json deleted file mode 100644 index f660e3f..0000000 --- a/ATRI/data/service/plugins/cant_do_anything.plugins.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "cant_do_anything", - "docs": "\u5403\u996d\u7b2c\u4e00\u540d\uff01\u597d\u5403\u5c31\u662f\u9ad8\u5174\uff01\uff01\n\u4f7f\u7528\u6b63\u5219\u5339\u914d", - "command": [ - "\u5e72\u5565\u5565\u4e0d\u884c\uff0c\u5403\u996d\u7b2c\u4e00\u540d" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/curse.plugins.json b/ATRI/data/service/plugins/curse.plugins.json deleted file mode 100644 index 00164ec..0000000 --- a/ATRI/data/service/plugins/curse.plugins.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "curse", - "docs": "\u53e3\u81ed\u4e00\u4e0b", - "command": [ - "\u9a82\u6211", - "\u53e3\u81ed\u4e00\u4e0b", - "\u53e3\u81ed" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/good_morning.plugins.json b/ATRI/data/service/plugins/good_morning.plugins.json deleted file mode 100644 index 8dd3e11..0000000 --- a/ATRI/data/service/plugins/good_morning.plugins.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "good_morning", - "docs": "\u7565\u5e26\u6da9\u6c14\u7684\u65e9\u5b89\n\u9700at", - "command": [ - "\u65e9\u5b89", - "\u65e9\u4e0a\u597d", - "morning", - "\u54e6\u54c8\u54df", - "ohayo", - "\u304a\u65e9\u3046" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/good_night.plugins.json b/ATRI/data/service/plugins/good_night.plugins.json deleted file mode 100644 index f223e8b..0000000 --- a/ATRI/data/service/plugins/good_night.plugins.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "good_night", - "docs": "\u665a\u5b89~\uff01\n\u9700at", - "command": [ - "\u665a\u5b89", - "\u54e6\u5440\u601d\u5bc6", - "\u54e6\u5440\u65af\u5bc6", - "oyasumi", - "\u304a\u3084\u3059\u307f" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/good_noon.plugins.json b/ATRI/data/service/plugins/good_noon.plugins.json deleted file mode 100644 index 8e6da45..0000000 --- a/ATRI/data/service/plugins/good_noon.plugins.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "good_noon", - "docs": "\u505a\u767d\u65e5\u68a6\n\u9700at", - "command": [ - "\u5348\u5b89", - "\u4e2d\u5348\u597d" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/hitokoto.plugins.json b/ATRI/data/service/plugins/hitokoto.plugins.json deleted file mode 100644 index 9a61ecf..0000000 --- a/ATRI/data/service/plugins/hitokoto.plugins.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "hitokoto", - "docs": "\u4e00\u8a00", - "command": [ - "\u4e00\u8a00" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/kani.plugins.json b/ATRI/data/service/plugins/kani.plugins.json deleted file mode 100644 index 1643d36..0000000 --- a/ATRI/data/service/plugins/kani.plugins.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "kani", - "docs": "\u30ab\u30cb\uff01\u30ab\u30cb\uff01\uff01\uff01\n\u4f7f\u7528\u6b63\u5219\u5339\u914d", - "command": [ - "kani", - "\u30ab\u30cb", - "\u8783\u87f9" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/setu.plugins.json b/ATRI/data/service/plugins/setu.plugins.json deleted file mode 100644 index ed08939..0000000 --- a/ATRI/data/service/plugins/setu.plugins.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "name": "setu", - "docs": "\n\u6da9\u56fe\uff0c\u5f00\u51b2\uff01\n\u4f7f\u7528\u6b63\u5219\u5339\u914d\n", - "command": [ - "\u6765\u5f20\u6da9\u56fe", - "\u6da9\u56fe\u6765", - "\u5f00\u51b2" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/tee_tee.plugins.json b/ATRI/data/service/plugins/tee_tee.plugins.json deleted file mode 100644 index 6e3a676..0000000 --- a/ATRI/data/service/plugins/tee_tee.plugins.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "tee_tee", - "docs": "\u4e00\u822c\u4eba\u5458\u4e0d\u8bb8\u8d34\uff01\n\u9700at", - "command": [ - "\u8d34\u8d34", - "\u8d34", - "teetee", - "\u3066\u3048\u3066\u3048", - "\u8d1d\u5360", - "\u8d1d\u5360\u8d1d\u5360" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/plugins/waste.plugins.json b/ATRI/data/service/plugins/waste.plugins.json deleted file mode 100644 index ce61818..0000000 --- a/ATRI/data/service/plugins/waste.plugins.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "waste", - "docs": "\u4e0d\u51c6\u9a82\u54b1\u5e9f\u7269\n\u4f7f\u7528\u6b63\u5219\u5339\u914d\uff0c\u9700at", - "command": [ - "fw", - "FW", - "\u5e9f\u7269", - "waste" - ] -}
\ No newline at end of file diff --git a/ATRI/data/service/switch.service.json b/ATRI/data/service/switch.service.json deleted file mode 100644 index 972610d..0000000 --- a/ATRI/data/service/switch.service.json +++ /dev/null @@ -1,93 +0,0 @@ -{ - "global": { - "curse": true, - "hitokoto": true, - "roll": true, - "rcnb": true, - "setu": true - }, - "330616602": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "516729280": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "1126435961": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "879471303": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "663588449": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "672076603": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "1128059355": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "567297659": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "921925032": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "null": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "652116841": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - }, - "1126435961": { - "curse": true, - "hitokoto": true, - "setu": true, - "roll": true, - "rcnb": true - } -}
\ No newline at end of file diff --git a/ATRI/log.py b/ATRI/log.py index 982e786..929784d 100644 --- a/ATRI/log.py +++ b/ATRI/log.py @@ -13,7 +13,7 @@ LOGGER_WARNING_PATH = Path( ) / 'ATRI' / 'logs' / 'warning' / f"{datetime.now().strftime('%Y%m%d-%H%M%S')}-WARNING.log" LOGGER_DEBUG_PATH = Path( '.' -) / 'ATRI' / 'logs' / 'warning' / f"{datetime.now().strftime('%Y%m%d-%H%M%S')}-DEBUG.log" +) / 'ATRI' / 'logs' / 'debug' / f"{datetime.now().strftime('%Y%m%d-%H%M%S')}-DEBUG.log" logger.add(LOGGER_INFO_PATH, rotation='10 MB', diff --git a/ATRI/plugins/admin/__init__.py b/ATRI/plugins/admin/__init__.py index e69de29..dfc8f4b 100644 --- a/ATRI/plugins/admin/__init__.py +++ b/ATRI/plugins/admin/__init__.py @@ -0,0 +1,7 @@ +# ===========================[Begin Command Processing]=========================== + + + + + +# ===========================[End Command Processing]============================= diff --git a/ATRI/plugins/setu/__init__.py b/ATRI/plugins/setu/__init__.py index 68cb1d2..2d630de 100644 --- a/ATRI/plugins/setu/__init__.py +++ b/ATRI/plugins/setu/__init__.py @@ -1,13 +1,23 @@ -from nonebot.plugin import on_regex +import random + +from nonebot.plugin import on_regex, on_command +from nonebot.adapters.cqhttp.message import MessageSegment, Message from nonebot.adapters.cqhttp import Bot +from nonebot.permission import SUPERUSER from nonebot.adapters.cqhttp.event import MessageEvent from ATRI.service.plugin import Plugin from ATRI.config import SETU_CONFIG +from ATRI.utils import compress_image +from ATRI.request import Request from ATRI.rule import is_in_service, is_in_ban_list, is_in_dormant +from .data_source import setu_port + # ===========================[Begin Command Processing]=========================== +resolution = 1 + __plugin_name__ = 'setu' __doc__ = """ @@ -23,7 +33,45 @@ setu = on_regex('|'.join(SETU_CONFIG['setu']['command']), @setu.handle() async def _(bot: Bot, event: MessageEvent) -> None: await bot.send(event, SETU_CONFIG['setu']['repo']['waiting']) + rd = random.randint(1, 2) + + if rd == 1: + data = await setu_port() + else: + data = await setu_port() + + if resolution == 1: + img = compress_image(await Request.get_image(data['data'][0]['url'])) + else: + img = await Request.get_image(data['data'][0]['url']) + msg0 = ( + f"{data['data'][0]['title']}\n" + f"pid: {data['data'][0]['pid']}\n" + f"{MessageSegment.image(file=f'file:///{img}')}" + ) + + await setu.finish(Message(msg0)) + + +setu_resolution = on_command(SETU_CONFIG['admin']['command'][0], + aliases=set(SETU_CONFIG['admin']['command']), + permission=SUPERUSER) + +@setu_resolution.handle() +async def _(bot, event: MessageEvent, state: dict) -> None: + msg = str(event.get_message()).strip() + if msg: + state['msg'] = msg + +@setu_resolution.got('msg', prompt='请键入正确参数奥') +async def _(bot, event: MessageEvent, state: dict) -> None: + global resolution + resolution = int(state['msg']) + if resolution == 1: + await setu_resolution.finish('完成~!已启用涩图压缩') + else: + await setu_resolution.finish('完成~!已关闭涩图压缩') -# ===========================[End Command Processing]=============================
\ No newline at end of file +# ===========================[End Command Processing]============================= diff --git a/ATRI/plugins/setu/data_source.py b/ATRI/plugins/setu/data_source.py index c5fffce..521b716 100644 --- a/ATRI/plugins/setu/data_source.py +++ b/ATRI/plugins/setu/data_source.py @@ -1,14 +1,27 @@ +import json from pathlib import Path from ATRI.config import SETU_CONFIG +from ATRI.request import Request +from ATRI.exceptions import InvalidRequest + DATA_PATH = Path('.') / 'ATRI' / 'data' / 'database' -class Function: - @staticmethod - async def setu_port() -> str: - ... - - @staticmethod - async def setu_local() -> str: - ... +async def setu_port() -> dict: + url = SETU_CONFIG['setu']['link']['url'] + params = { + "apikey": SETU_CONFIG['setu']['link']['api_key'], + "r18": 0, + "num": 1 + } + data = {} + try: + data = json.loads(await Request.post_bytes(url, params)) + except InvalidRequest: + raise InvalidRequest('Request failed!') + return data + + +async def setu_local() -> str: + ... diff --git a/ATRI/request.py b/ATRI/request.py index 6b21800..78bad6e 100644 --- a/ATRI/request.py +++ b/ATRI/request.py @@ -37,21 +37,24 @@ class Request: return result @classmethod - async def get_image(cls, url: str) -> Path: + async def get_image(cls, url: str, params: Optional[dict] = None) -> str: ''' 异步方式下载图片 :return: img file ''' file_path = TEMP_PATH / 'img' + os.makedirs(file_path, exist_ok=True) file_name = ''.join(sample(string.ascii_letters + string.digits, 16)) - img_path = file_path / f'{file_name}.png' - image = await cls.get_bytes(url) - try: - async with aiofiles.open(image, 'wb') as f: - await f.write(image) - except InvalidWriteText: - raise InvalidWriteText('Writing file failed!') - return img_path + path = os.path.abspath(file_path) + path = path + f'\\{file_name}.png' + async with ClientSession() as session: + async with session.get(url) as res: + try: + async with aiofiles.open(path, 'wb') as f: + await f.write(await res.read()) + except InvalidWriteText: + raise InvalidWriteText('Writing file failed!') + return path @staticmethod async def post_bytes(url: str, params: Optional[dict] = None) -> bytes: diff --git a/ATRI/service/httppost.py b/ATRI/service/httppost.py index d9b7360..9469115 100644 --- a/ATRI/service/httppost.py +++ b/ATRI/service/httppost.py @@ -1,62 +1,112 @@ -from ATRI.config import RUNTIME_CONFIG +''' +获取更多帮助 >> https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md +''' +import json +from typing import Optional, Union, Dict, Any + +from ATRI.log import logger +from ATRI.config import config +from ATRI.request import Request URL = ( - f"http://{RUNTIME_CONFIG['http_post']['host']}" - f"{RUNTIME_CONFIG['http_post']['port']}" + f"http://{config['http_post']['host']}:" + f"{config['http_post']['port']}/" ) class HttpPost: + @classmethod - def send_private_msg(cls): - ... + async def send_private_msg(cls, + user_id: int, + message: str, + auto_escape: bool = False): # -> Dict[str, Any] + url = URL + "send_private_msg?" + params = { + "user_id": user_id, + "message": message, + "auto_escape": f"{auto_escape}" + } + result = json.loads(await Request.post_bytes(url, params)) + logger.debug(result) + return result @classmethod - def send_group_msg(cls): + def send_group_msg(cls, + group_id: int, + message: Union[str], + auto_escape: Optional[bool] = ...) -> Dict[str, Any]: ... @classmethod - def send_msg(cls): + def send_msg(cls, + message_type: Optional[str] = ..., + user_id: Optional[int] = ..., + group_id: Optional[int] = ..., + message = Union[str], + auto_escape: bool = ...) -> Dict[str, Any]: ... @classmethod - def delete_msg(cls): + def delete_msg(cls, + message_id: int): ... @classmethod - def get_msg(cls): + def get_msg(cls, + message_id: int) -> Dict[str, Any]: ... @classmethod - def get_forward_msg(cls): + def get_forward_msg(cls, + id: int): ... @classmethod - def send_like(cls): + def send_like(cls, + user_id: int, + times: int = ...): ... @classmethod - def set_group_kick(cls): + def set_group_kick(cls, + group_id: int, + user_id: int, + reject_add_request: bool = ...): ... @classmethod - def set_group_ban(cls): + def set_group_ban(cls, + group_id: int, + user_id: int, + duration: int = ...): ... @classmethod - def set_group_anonymous_ban(cls): + def set_group_anonymous_ban(cls, + group_id: int, + anonymous: Optional[Dict[str, Any]] = ..., + flag: Optional[str] = ..., + duration: int = ...): ... @classmethod - def set_group_whole_ban(cls): + def set_group_whole_ban(cls, + group_id: int, + enable: bool = ...): ... @classmethod - def set_group_admin(cls): + def set_group_admin(cls, + group_id: int, + user_id: int, + enable: bool = ...): ... @classmethod - def set_group_anonymous(cls): + def set_group_anonymous(cls, + group_id: int, + enable: bool = ...): ... @classmethod diff --git a/ATRI/service/temp.py b/ATRI/service/temp.py new file mode 100644 index 0000000..2b73fdc --- /dev/null +++ b/ATRI/service/temp.py @@ -0,0 +1,6 @@ +import os +from pathlib import Path + + +TEMP_PATH = Path('.') / 'ATRI' / 'data' / 'temp' / 'img' +os.makedirs(TEMP_PATH, exist_ok=True) diff --git a/ATRI/utils.py b/ATRI/utils.py index 7ded644..961487c 100644 --- a/ATRI/utils.py +++ b/ATRI/utils.py @@ -51,11 +51,12 @@ def now_time() -> float: now = hour + minute / 60 return now -def compress_image(out_file: str, kb=500, quality=85, k=0.9) -> str: +def compress_image(out_file, kb=500, quality=85, k=0.9) -> str: ''' 压缩图片 :return: img file ''' + print(1) o_size = os.path.getsize(out_file) // 1024 if o_size <= kb: return out_file @@ -69,6 +70,5 @@ def compress_image(out_file: str, kb=500, quality=85, k=0.9) -> str: out.save(out_file, quality=quality) except InvalidWriteText: raise InvalidWriteText('Writing file failed!') - break o_size = os.path.getsize(out_file) // 1024 return out_file @@ -3,11 +3,11 @@ ''' @File : main.py -@Time : 2021/01/23 12:49:54 +@Time : 2021/01/27 15:57:26 @Author : Kyomotoi @Contact : [email protected] @Github : https://github.com/Kyomotoi -@License : Copyright © 2018-2020 Kyomotoi, All Rights Reserved. +@License : Copyright © 2021 Kyomotoi, All Rights Reserved. ''' __author__ = 'kyomotoi' |