commit d425cf8685c00da8eecd5c0b0991e8ba8c202171 Author: snowy Date: Thu Feb 29 01:03:40 2024 +0800 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..1bc24b15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +plugin \ No newline at end of file diff --git a/datapack/liteyuki_default/metadata.json b/datapack/liteyuki_default/metadata.json new file mode 100644 index 00000000..37536cf1 --- /dev/null +++ b/datapack/liteyuki_default/metadata.json @@ -0,0 +1,4 @@ +{ + "name": "Liteyuki Default", + "version": "1.0" +} \ No newline at end of file diff --git a/datapack/liteyuki_language/metadata.json b/datapack/liteyuki_language/metadata.json new file mode 100644 index 00000000..928211d9 --- /dev/null +++ b/datapack/liteyuki_language/metadata.json @@ -0,0 +1,4 @@ +{ + "name": "Liteyuki Language Pack", + "version": "1.0" +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 00000000..b990d8d1 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +from src.liteyuki import * + +if __name__ == '__main__': + liteyuki = Liteyuki() + app = liteyuki.get_asgi() + liteyuki.run(app="main:app") \ No newline at end of file diff --git a/plugins/liteyuki_plugin_hello/__init__.py b/plugins/liteyuki_plugin_hello/__init__.py new file mode 100644 index 00000000..618de7df --- /dev/null +++ b/plugins/liteyuki_plugin_hello/__init__.py @@ -0,0 +1,9 @@ +from nonebot import on_command +from nonebot.rule import to_me + +hello = on_command('hello', aliases={'你好'}, rule=to_me()) + + +@hello.handle() +async def handle_first_receive(bot, event, state): + await hello.finish('Hello, world!') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..f45a37a8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ + +nonebot2[fastapi] +watchdog==2.1.6 \ No newline at end of file diff --git a/src/api/data.py b/src/api/data.py new file mode 100644 index 00000000..e69de29b diff --git a/src/api/utils.py b/src/api/utils.py new file mode 100644 index 00000000..b5c54a99 --- /dev/null +++ b/src/api/utils.py @@ -0,0 +1,33 @@ +import os +import yaml +from nonebot import logger + + +def load_config() -> dict[str, any]: + """ + Load config from config.yml + :return: + """ + config = { + 'host': '0.0.0.0', + 'port': 20216, + 'nickname': ['Liteyuki'], + 'command_start': [''], + } + + if not os.path.exists('config.yml'): + logger.warning('warn.config_file_not_found') + with open('config.yml', 'w', encoding='utf-8') as f: + f.write(yaml.dump(config, indent=4)) + else: + try: + with open('config.yml', 'r', encoding='utf-8') as f: + config.update(yaml.load(f, Loader=yaml.FullLoader)) + logger.success('success.config_loaded') + # 格式化后写入 + with open('config.yml', 'w', encoding='utf-8') as f: + f.write(yaml.dump(config, indent=4)) + except Exception as e: + logger.error(f'error.load_config: {e}') + + return config diff --git a/src/liteyuki.py b/src/liteyuki.py new file mode 100644 index 00000000..783ba760 --- /dev/null +++ b/src/liteyuki.py @@ -0,0 +1,46 @@ +from typing import Any, Optional +from nonebot import DOTENV_TYPE +import nonebot +from nonebot import logger +from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter +from nonebot.adapters.onebot.v12 import Adapter as OnebotV12Adapter + +from src.api.utils import load_config + +app = None +adapters = [ + OnebotV11Adapter, + OnebotV12Adapter +] + + +class Liteyuki: + def __init__(self, *, _env_file: Optional[DOTENV_TYPE] = None, **kwargs: Any): + print( + '\033[34m' + r''' __ ______ ________ ________ __ __ __ __ __ __ ______ +/ | / |/ |/ |/ \ / |/ | / |/ | / |/ | +$$ | $$$$$$/ $$$$$$$$/ $$$$$$$$/ $$ \ /$$/ $$ | $$ |$$ | /$$/ $$$$$$/ +$$ | $$ | $$ | $$ |__ $$ \/$$/ $$ | $$ |$$ |/$$/ $$ | +$$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |$$ $$< $$ | +$$ | $$ | $$ | $$$$$/ $$$$/ $$ | $$ |$$$$$ \ $$ | +$$ |_____ _$$ |_ $$ | $$ |_____ $$ | $$ \__$$ |$$ |$$ \ _$$ |_ +$$ |/ $$ | $$ | $$ | $$ | $$ $$/ $$ | $$ |/ $$ | +$$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ ''' + '\033[0m' + ) + + kwargs = load_config() + self.nonebot = nonebot + self.nonebot.init(_env_file=_env_file, **kwargs) + self.driver = self.nonebot.get_driver() + + def run(self, *args, **kwargs): + for adapter in adapters: + self.driver.register_adapter(adapter) + self.nonebot.load_plugin('src.liteyuki_main') + self.nonebot.load_plugins('src/builtin') + self.nonebot.load_plugins('plugins') + # Todo: load from database + self.nonebot.run(*args, **kwargs) + + def get_asgi(self): + return self.nonebot.get_asgi() diff --git a/src/liteyuki_main.py b/src/liteyuki_main.py new file mode 100644 index 00000000..cb2babec --- /dev/null +++ b/src/liteyuki_main.py @@ -0,0 +1,16 @@ +from nonebot import on_command +from nonebot.adapters.onebot.v11.event import MessageEvent +from nonebot.permission import SUPERUSER +import os +folders = ['plugins'] +for folder in folders: + if not os.path.exists(folder): + os.makedirs(folder) + +echo = on_command('echo', permission=SUPERUSER, priority=5) + + +@echo.handle() +async def _(event: MessageEvent): + print(event.get_message()) + await echo.finish(event.get_message())